Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimePicker - how to get AM or PM? [duplicate]

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.

like image 578
sandalone Avatar asked Sep 23 '11 09:09

sandalone


People also ask

How do you get AM PM on TimePicker?

The AM/PM mode is determined by either explicitly setting the current mode through setIs24Hour 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.


2 Answers

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 );
    }
};
like image 51
Aravin Avatar answered Oct 20 '22 07:10

Aravin


On callback from

public void onTimeSet(TimePicker view, int hourOfDay, int minute) 

call below function

private String getTime(int hr,int min) {
        Time tme = new Time(hr,min,0);//seconds by default set to zero
        Format formatter;
        formatter = new SimpleDateFormat("h:mm a");
        return formatter.format(tme);
    }

The functions return something like this 1:12 PM

like image 21
Basavaraj Hampali Avatar answered Oct 20 '22 06:10

Basavaraj Hampali