-2

I'm adding an event, the event displays starting hour (hh:mm) and ending hour (hh:mm), there are 2 tiemPickers, one to pick starting time and one for duration.

Problem 1:

If the user picks the starting time to be at 10:30h and duration of 1:40h, the ending time should be 12:10h instead of 11:70h, how do I do this?

Problem 2:

If the user picks starting time of 23:00h and duration of 02:00h, ending time should be 01:00h instead of 25:00h.

What I have:

// getting the selected time
int endingHourInt = prefs.getInt("mDurationHour", 0) + prefs.getInt("mStartingHour",0);
int endingMinInt = prefs.getInt("mDurationMin", 0) + prefs.getInt("mStartingMin",0);

// displaying the text
endingTime.setText(endingHourInt + ":" + endingMinInt );
John Sardinha
  • 3,566
  • 6
  • 25
  • 55

1 Answers1

4
Calendar calendar = Calendar.getInstance();
calendar.set(endingHourInt,Calendar.HOUR);
calendar.set(endingMinInt,Calendaur.MINTUE);
calendar.add(Calendar.HOUR_OF_DAY, 2);
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
format.format(calendar.getTime());
Itzik Samara
  • 2,278
  • 1
  • 14
  • 18
  • 2
    You forget that OP wants to add a time to his current time. So I would add a `calendar.add(Calendar.HOUR_OF_DAY, 2);` to make your answer more complete. – Kevin Cruijssen May 20 '16 at 11:02