Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set AM/PM in a TimePicker?

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));
like image 905
AndroidDev Avatar asked Jul 08 '12 09:07

AndroidDev


People also ask

How to show AM PM in TimePicker in android?

The AM/PM mode is determined by either explicitly setting the current mode through setIs24Hour(boolean) or the widget attribute is24HourFormat (true for 24-hour mode, false for 12-hour mode). Otherwise, TimePicker retrieves the mode based on the current context.

How do I set time on TimePicker?

To provide a widget for selecting a time, use the Time Picker widget , you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.


4 Answers

You'll have to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR.

i.e., timePicker.setCurrentHour() always expects the argument in 24-hour format. Unfortunately, this fact is not documented properly in the API documentation.

like image 144
Dheeraj Vepakomma Avatar answered Sep 30 '22 17:09

Dheeraj Vepakomma


public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute) {
    if(hourOfDay>=0 && hourOfDay<12){
        time = hourOfDay + " : " + minute + " AM";
    } else {
        if(hourOfDay == 12){
            time = hourOfDay + " : " + minute + "PM";
        } else{
            hourOfDay = hourOfDay -12;
            time = hourOfDay + " : " + minute + "PM";
        }
    }

    deliveryTime.setText(time);
}
like image 33
Rishi Avatar answered Sep 30 '22 17:09

Rishi


I think you have to override the onTimeSet() method of the Timepicker. Check this link

TimePickerDialog and AM or PM

May it helps you..

like image 39
Arindam Mukherjee Avatar answered Sep 30 '22 16:09

Arindam Mukherjee


First, get an instance of the Calendar class, then use HOUR_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 than 0 and less than 12. If this condition is true then append AM or else PM. Because HOUR_OF_DAY is 24-hour format, the PM hours will be displayed as 13,14 so to handle this, just subtract the HOUR_OF_DAY by 12. Here is the code:

Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour < 12 && hour >= 0) {
    tv.setText(hour + " AM");
} else {
    hour -= 12;
    if(hour == 0) {
        hour = 12;
    }
    tv.setText(hour + " PM");
}
like image 43
rakshi059 Avatar answered Sep 30 '22 18:09

rakshi059