I am using a time picker dialogue in my app , so the problem is simple ., I want the time in the format 07:00 AM . Here is my code and I got output as 7:00 AM . Thanks in advance..
private TimePickerDialog.OnTimeSetListener TimePickerListener = new TimePickerDialog.OnTimeSetListener() {
// while dialog box is closed, below method is called.
public void onTimeSet(TimePicker view, int hour, int minute) {
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
if(flag==111)
from.setText(hour12format + ":" + minute + " "+ ampmStr);
else if(flag==222)
to.setText(hour12format + ":" + minute + " "+ ampmStr);
}
};
My suggestion.
private TimePickerDialog.OnTimeSetListener TimePickerListener = new TimePickerDialog.OnTimeSetListener() {
// while dialog box is closed, below method is called.
public void onTimeSet(TimePicker view, int hour, int minute) {
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr = (ampm == 0) ? "AM" : "PM";
//Format Date and/or Time see more: http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aaa");
String strDateFormat = "";
try {
strDateFormat = sdf.format(mCalen..getTime());
} catch (Exception e) {
// ...
}
// Set the Time String in Button
if(flag==111)
from.setText(strDateFormat);
else if(flag==222)
to.setText(strDateFormat);
}
};
Edit:
Sorry the correct mask "HH:mm aaa"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.