2

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);
Jayabal
  • 3,619
  • 3
  • 24
  • 32
Asad Iqbal
  • 304
  • 4
  • 16
  • @Philipp Reichart thanks for the reply. i got the understanding from the below post . now i am not talking abt AM PM values. i got the ans that android convert it to 24 hour format when user select. but after setting the Hour_Of_Day and minutes parameters of my "time" object (above code). i call the set method and call the time.getTimeInMillis(). but problem is that when i set the time for the next day (say my current system time is 9:00 pm and i set 8:00 pm that is of next day) but alarmManager trigger the alarm immediately. can u guide me regarding this ? – Asad Iqbal Dec 23 '11 at 20:09

3 Answers3

10

Create a new instance of the calendar within your listener, as like follows

Calendar datetime = Calendar.getInstance();
     c.set(Calendar.HOUR, mHour);
    c.set(Calendar.MINUTE, mMinute);

    if (datetime.get(Calendar.AM_PM) == Calendar.AM)
        am_pm = "AM";
    else if (datetime.get(Calendar.AM_PM) == Calendar.PM) {
        am_pm = "PM";
         if (hourOfDay > 12)
              hourOfDay = hourOfDay - 12;
    }

Updated:

Calendar mCalendar = Calendar.getInstance();

 mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
 mMinute = mCalendar.get(Calendar.MINUTE);


 TimePickerDialog timePickerDialog = new TimePickerDialog(mActivity, mTimeSetListener, mHour, mMinute, false);

 private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

         mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
         mCalendar.set(Calendar.MINUTE, mMinute);
         SimpleDateFormat mSDF = new SimpleDateFormat("hh:mm a");
         String time = mSDF.format(mCalendar.getTime());
     }
 }
Parth Patel
  • 859
  • 2
  • 11
  • 28
Jayabal
  • 3,619
  • 3
  • 24
  • 32
  • Thanks it saves my day – SAndroidD Nov 19 '17 at 08:11
  • @Jayabal Thanks. Here is the Kotlin version. val calendar: Calendar = Calendar.getInstance() calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) calendar.set(Calendar.MINUTE, minute) val mSDF = SimpleDateFormat("hh:mm a") val time = mSDF.format(calendar.getTime()) – TechBee Sep 23 '22 at 01:32
2

Maybe this answers your question.

TimePickerDialog and AM or PM

Community
  • 1
  • 1
R.daneel.olivaw
  • 2,681
  • 21
  • 31
  • No, i am unable to get the AM/PM field. My system is showing 11:00 PM time when i set the alarm via timepicerdialog at 11:05 AM then alarm invoke at 11:05 PM instead of AM – Asad Iqbal Dec 22 '11 at 17:33
0

Try This:

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String am_pm = "";

    Calendar datetime = Calendar.getInstance();
    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
    datetime.set(Calendar.MINUTE, minute);

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

    String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+""; 

    ((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
}
Adil Malik
  • 6,279
  • 7
  • 48
  • 77