How to set TimePicker show with format 24h
I've created a TimePicker in layout and I want it to show time with format 24h.
Can you help me? Thanks.
Solution 1:
TimePicker
is a view for selecting the time of day, in either 24 hour or AM/PM mode.
You can use setIs24HourView(true)
method with TimePicker.
Solution 2:
Setting TimePicker
by default to 24 hour mode is not always a good idea since many countrys have diffrent convention. For this case use DateFormat.is24HourFormat(getActivity())
.
Your final code migth look like picker.setIs24HourView(DateFormat.is24HourFormat(this));
.
Further details see:
- http://developer.android.com/guide/topics/ui/controls/pickers.html
- http://developer.android.com/reference/android/widget/TimePicker.html
- http://developer.android.com/reference/android/text/format/DateFormat.html
Solution 3:
If you want the TimePicker to be correctly initialized with the current time in 24h format use the following:
import java.util.Calendar;
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
Otherwise, due to Android bug, the picker will start with an incorrect hour (2 instead of 14 etc).