Im creating an alarm apps for android. I dont really understand about time format so i go googling and got this post.
I followed that post, but a bug still remains : My apps cant tell the difference between (lets say) 12:17 AM and 12:17 PM. So the bug will occurred if the hour is 12.
What i did when taking the value form the TimePicker
(hh:m:aa) format :
String hour = "";
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
datetime.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
if (datetime.get(Calendar.AM_PM) == Calendar.AM && timeSchedule.getCurrentHour() == 0) //12 AM = 0
hour = "00"+ ":" + timeSchedule.getCurrentMinute();
else
hour = timeSchedule.getCurrentHour() + ":" + timeSchedule.getCurrentMinute();
With above code, 12:17 AM will become 00:17 while 12:17PM will become 12:17PM. This value is stored in the mysql Time format. Since i want to create an alarm apps, please kindly tell me whether i did is right or not.
And this is what i did to show them to the user, where 00:17 (taken from the result of above code) become 12:17AM (correct) but 12:17 become 12:17AM (wrong) :
String time = getItem(position).getTime(); //value from above code
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat newFormat = new SimpleDateFormat("hh:mm:aa");
String formatedTime = "";
try {
Date date = format.parse(time);
formatedTime = newFormat.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
To be honest, this is the first time i deal with somekind of alarm app, so please kindly tell me if i do something wrong. (beside my bug)
Thanks for your time.
UPDATE
This is what i did now :
I changed the mysql column from Time to Varchar, because Time wont let me insert "AM/PM".
After that :
String hour = "";
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
datetime.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
String am_pm = "";
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else
am_pm = "PM";
hour = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+"";
final String time = hour+ ":" + datetime.get(Calendar.MINUTE) + " " + am_pm ;
Above code return 12:17 AM and 12:17 PM. With this, i dont have to use SimpleDateFormat
to re-format the date again. Is this the correct way to set the time for alarm?