I am using a TimePicker in my app. Whenever the user opens the screen with the TimePicker, I initialize it with the current time. When I do this, the TimePicker shows AM instead of PM. Why does this happen? Is there anything I have to add in my code?
Date initDate = m_NoteManageObject.getDateTimeArray(position);
Calendar myCal = Calendar.getInstance();
myCal.setTime(initDate);
TimePicker timePicker = (TimePicker)layout.findViewById(R.id.time_picker);
// Initialise Time Picker
timePicker.setCurrentHour(myCal.get(Calendar.HOUR));
timePicker.setCurrentMinute(myCal.get(Calendar.MINUTE));
I think you have to override the onTimeSet() method of the Timepicker. Check this link
TimePickerDialog and AM or PM
May it helps you..
You'll have to use
Calendar.HOUR_OF_DAY
instead ofCalendar.HOUR
.i.e.,
timePicker.setCurrentHour()
always expects the argument in 24-hour format. Unfortunately, this fact is not documented properly in the API documentation.First, get an instance of the
Calendar
class, then useHOUR_OF_DAY
to get the hour.HOUR_OF_DAY
will be in 24-hour format so just assign that to a variable and then check whether that int is greater than0
and less than12
. If this condition is true then appendAM
or elsePM
. BecauseHOUR_OF_DAY
is 24-hour format, the PM hours will be displayed as13,14
so to handle this, just subtract theHOUR_OF_DAY
by12
. Here is the code:Try this :