在我的Android应用程序中,我有3个用于小时/分钟/和PM或AM的微调器。我想增加7.5小时,以便让用户有足够的时间睡觉。我的这个Android应用程序是用户睡眠的计算器。例如,我想在早上7点醒来。到了晚上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
答案 0 :(得分: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);
答案 1 :(得分: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级别。