0

在我的 Android 应用程序中,我有 3 个用于 Hour/Minute/ 和 PM 或 AM 的微调器。我想在其中添加 7.5 小时以获得用户睡眠所需的时间。我的这个 android 应用程序是用户睡眠的计算器。例如,我想在早上 7:00 起床。到晚上 11:15,您现在应该睡觉了。请给我一些关于如何做到这一点的指导。我急需帮助。谢谢。感谢你的帮助。

像这样:

  /**
   * Get sleeping times corresponding to a local time
   * 
   * @param wakingTime
   *            time to wake up !
   * @return list of times one should go to bed to
   */

  public static Set<Date> getSleepingTimes(Date wakingTime) {

    Set<Date> result = new TreeSet<Date>();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime( wakingTime );
    calendar.add( Calendar.MINUTE, -14 );
    calendar.add( Calendar.MINUTE, -( 2 * 90 ) );
    for ( int i = 3; i <= 6; i++ ) {
      calendar.add( Calendar.MINUTE, -90 );
      result.add( calendar.getTime() );
    }
    return result;

  } // end of getSleepingTimes method
4

2 回答 2

1
public static Date getSleepingTimes(Date wakingTime) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime( wakingTime );
    calendar.add(Calendar.HOUR,-7);
    calendar.add(Calendar.MINUTE,-30);

    return calendar.getTime();

}

但最好不要硬编码 7.5 小时。

public static Date getSleepingTimes(Date wakingTime, int hoursToSleep, int minutesToSleep) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime( wakingTime );
    calendar.add(Calendar.HOUR,-hoursToSleep);
    calendar.add(Calendar.MINUTE,-minutesToSleep);

    return calendar.getTime();

}

像这样使用:

   getSleepingTime(wakingTime, 7, 30);
于 2012-10-13T13:18:16.550 回答
0

您将使用TimePicker组件。有方法

  • 获取当前时间
  • 获取当前分钟

  • 设置当前小时
  • 设置当前分钟

您可以使用它来添加额外的时间。

final TimePicker timePicker = (TimePicker) findViewById(R.id.a_time_picker);
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
timePicker.setCurrentHour(hour + 7);
timePicker.setCurrentMinute(minute + 30);

您必须自己解决 AM/PM 问题。

未来:请提供您正在开发的 Android SDK 级别。

于 2012-10-13T12:54:18.103 回答