Search code examples
androidtimepicker

how to get the TimePickerDialog AM PM values


I want to get the AM/PM values through the timePickerDialog. my code is such, it is taking input through user via timepickerdialog and set the alarm. here I am getting user input

datetime = Calendar.getInstance();
new TimePickerDialog(this,this,datetime.get(Calendar.HOUR),datetime.get(Calendar.MINUTE),false).show();

now when the onTimeSet function call i can get the user selected hour and minutes but not able to get the AM/PM value that user selected. here is other code

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { try { Calendar time = Calendar.getInstance();
String am_pm;

if (datetime.get(Calendar.AM_PM) == Calendar.AM)
    am_pm = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
    am_pm = "PM";

time.set(Calendar.HOUR, hourOfDay);
time.set(Calendar.MINUTE, minute);
//time.set(Calendar.SECOND, 0);
//time.set(Calendar.MILLISECOND, 0);
//time.set(Calendar.AM_PM,datetime.PM);

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);      
alarmMgr.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_long);

Solution

  • Maybe this answers your question.

    TimePickerDialog and AM or PM