Reputation:
In my Android application, I have 3 spinners for Hour/Minute/ and PM or AM. I'd like to add 7.5 hours in that to get the needed time for the user to sleep. This android application of mine is a calculator of a user's sleep. For example, I'd like to wake up at 7:00 AM. BY 11:15 PM you should now be sleeping. Please give me some directions on how to do this. I'm in dire need of help. Thanks. Appreciate your help.
like this:
/**
* 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
Upvotes: 0
Views: 1947
Reputation: 923
You would be using the TimePicker component. There are the methods
and
which you could use to add the additional time.
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);
You would have to solve the AM/PM problem yourself.
For the future: Please provide the Android SDK level your are developing for.
Upvotes: 0
Reputation: 14472
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();
}
but better not to hard code the 7.5 hours.
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();
}
use like this:
getSleepingTime(wakingTime, 7, 30);
Upvotes: 1