Dunkey Dunkey - 1 year ago 121
Android Question

Display 12hh format in Android TimePicker Dialog Fragment

I'm doing an android app where I have to choose a specific time.

This what I've done so far:

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

public Dialog onCreateDialog(Bundle savedInstanceState)
// use the current time as the default values for the picker
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute, false
); // DateFormat.is24HourFormat(getActivity())

public void onTimeSet(TimePicker view, int hourOfDay, int minute)
String time;

if ( minute < 10 ) {
_minute = ":0" + minute;
} else {
_minute = ":" + minute;

Log.w("TimePicker", "Time = " + time);

EditText activityButton = (EditText)getActivity().findViewById(R.id.etTime);
activityButton.setText( time );


What I want to achieve is:

6:30 PM

But all I get is:


I want to 12 H format not the 24h. How can I do this? Help is much appreciated thanks.

Answer Source

Try changing

 int hour = c.get(Calendar.HOUR_OF_DAY);


int hour = c.get(Calendar.HOUR);

bcoz I see on

public static final int HOUR_OF_DAY

Added in API level 1

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download