在我的 Android 应用程序中,我有 3 个用于小时/分钟/以及下午或上午的微调器。我想添加 7.5 小时以获得用户 sleep 所需的时间。我的这个 Android 应用程序是一个用户 sleep 计算器。例如,我想在早上 7:00 起床。到晚上 11:15 您应该已经 sleep 了。请给我一些关于如何执行此操作的指示。我急需帮助。谢谢。感谢您的帮助。
像这样:
/**
* 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
最佳答案
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);
关于java - Android 将 7.5 小时添加到特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873235/