user1889890
user1889890

Reputation:

Setting time in TimePicker and comparing it to calendar.hour and minute?

I have a TimePicker that I need to grab the hour and minute from (-after the user specifies a time and hits the done button) and then compare it to the current hour and minute, doing an action if those two times match up.

Currently I'm using:

int calendarHour = calendar.get(Calendar.HOUR);
    int calendarMinute = calendar.get(Calendar.MINUTE);

to get the current hour and minute. After doing a lot of searching, I'm confused on the format of time working compared to a string/int on android.

How do I grab the hour and minute from the TimePicker? How do I check IF the TimePicker time and Calendar time match?

Upvotes: 1

Views: 1275

Answers (1)

Tushar
Tushar

Reputation: 8049

As Calendar.HOUR is represented in the range [0,12), you need to use Calendar.HOUR_OF_DAY to get it in the [0,23) range that is similar to the range that TimePicker.getCurrentHour() returns.

int calendarHour = calendar.get(Calendar.HOUR_OF_DAY);
int calendarMinute = calendar.get(Calendar.MINUTE);

int timePickerHour = timePicker.getCurrentHour();
int timePickerMinute = timePicker.getCurrentMinute();

if (calendarHour == timePickerHour && calendarMinute == timePickerMinute) {
    // Same 
}

TimePicker Documentation

Upvotes: 2

Related Questions