Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Calendar Set Am or Pm time not 24 Hour

Tags:

java

android

I have a script at the moment that is always being passed the time in an AM PM format. But I do not know how to set the calendar with this time as it only accepts 24 hour format the way I have the code written. How would I convert the time to 24 hour or input a 12 hour format?

         calendar.set(Calendar.HOUR_OF_DAY, HourValue);
         calendar.set(Calendar.MINUTE, MinuteValue);
         calendar.set(Calendar.SECOND, 0);
         calendar.add(Calendar.MINUTE, -356);
like image 364
Somk Avatar asked May 15 '12 21:05

Somk


3 Answers

http://developer.android.com/reference/java/util/Calendar.html#HOUR

calendar.set(calendar.HOUR,HourValue) , instead of HOUR_OF_DAY so if you give a 24 hour input it converts it into 12 hour format

like image 162
Shrikanth Avatar answered Oct 23 '22 17:10

Shrikanth


0 for am and 1 for pm Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.AM_PM, 1); c.set(Calendar.HOUR, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 00); c.set(Calendar.MILLISECOND, 00);

like image 28
deepan Avatar answered Oct 23 '22 19:10

deepan


int am_pm=c.get(Calendar.AM_PM);
String time=c.HOUR + ((am_pm==Calendar.AM)?"am":"pm"))

you can try the above.

like image 31
j2emanue Avatar answered Oct 23 '22 19:10

j2emanue