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.
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.
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 );
}
};
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With