TimePicker - how to get AM or PM?

sandalone picture sandalone · Sep 23, 2011 · Viewed 57.5k times · Source

I want to set an alarm based on a user's selection in TimePicker. TimePicker is set to AM/PM mode. In order to know if a user wants his alarm to set to 10 AM or 10 PM, how should I get the AM/PM value?

The listener TimePickerDialog.OnTimeSetListener passes only hours and minutes.

Answer

Aravin picture Aravin · Oct 11, 2013

Above answers are right i found simplest way to find AM PM.

TimePickerDialog.OnTimeSetListener onStartTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM ;
        if(hourOfDay < 12) {
            AM_PM = "AM";
        } else {
            AM_PM = "PM";
        }

        mStartTime.setText(hourOfDay + " : " + minute + " " + AM_PM );
    }
};