Displaying the Time in AM/PM format in android

后端 未结 8 627
醉梦人生
醉梦人生 2020-12-10 13:27

I am getting the time using a time picker and displaying the time in the text view using following code...

private TimePickerDialog.OnTimeSetListener mTimeSe         


        
8条回答
  •  不知归路
    2020-12-10 13:52

    int am_pm=mycalendar.get(Calendar.AM_PM);
    String amOrpm=((am_pm==Calendar.AM)?"am":"pm");
    

    But as per my opinion simpleDateFormat would be the best use, like this:

    final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("America/Chicago"));
    SimpleDateFormat format = new SimpleDateFormat("h:mm a");
    format.setTimeZone(c.getTimeZone());
    String myFormatted time = format.format(c.getTime());
    

    It's better as the calendar by default will return 00 for 12 pm, which you might don't want.

提交回复
热议问题