How do I set AM/PM in a TimePicker?
Asked Answered
A

5

27

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));
Anthropophagy answered 8/7, 2012 at 9:18 Comment(1)
I had the same issue and I have figured it out. If you use Calendar.HOUR, it means you are ignoring value of AM/PM. Instead, while setting current hour, use Calendar.HOUR_OF_DAY to take AM/PM into consideration.Ashurbanipal
S
51

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.

Saida answered 8/7, 2012 at 10:38 Comment(1)
I set the time in textView and use same textView to pick time. When I set time with timePicker, it becomes visible to textView but when I select textView again to pick time, timePicker sets AM/PM to AM. Please give a solution.Probst
B
4
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);
}
Brahms answered 28/6, 2017 at 7:31 Comment(0)
P
1

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

TimePickerDialog and AM or PM

May it helps you..

Pence answered 8/7, 2012 at 9:23 Comment(4)
I am not using a TimePickerDialog, I simple use the TimePicker widget i my xml layout.Anthropophagy
But in the settings, is it in 24 Hours format?? Just check it and try using 12 hours format.Pence
No its in 12 hour format..actually in the timepicker i want to show am or pm depending on current timeAnthropophagy
mkyong.com/android/android-time-picker-example try with this..Pence
S
1

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");
}
Sami answered 1/9, 2015 at 6:56 Comment(0)
S
0

Try this :

            Calendar calendar = Calendar.getInstance();
            calendar.set(0, 0, 0, hourOfDay, minute, 0);
            long timeInMillis = calendar.getTimeInMillis();
            java.text.DateFormat dateFormatter = new SimpleDateFormat("hh:mm a");
            Date date = new Date();
            date.setTime(timeInMillis);
            time.setText(dateFormatter.format(date));
Sympathy answered 1/9, 2015 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.