I have problem with converting 24-hour format to 12 hour format. This is my code.
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
ViewGroup vg=(ViewGroup) view.getChildAt(0);
hour = hourOfDay;
minutes = minute;
String am_pm = (hourOfDay < 12) ? "AM" : "PM";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
String tme = hour +":"+ minutes+am_pm;
et1.setText(tme);
}
};
I shift to timepicker dialog from alert dialog because i don't know how to change 24-hour to 12-hour format
et1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false).show();
}
});
}
This is the proof that i already turn the boolean impression to false.
Try this-
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
minutes = minute;
String timeSet = "";
if (hour > 12) {
hour -= 12;
timeSet = "PM";
} else if (hour == 0) {
hour += 12;
timeSet = "AM";
} else if (hour == 12){
timeSet = "PM";
}else{
timeSet = "AM";
}
String min = "";
if (minutes < 10)
min = "0" + minutes ;
else
min = String.valueOf(minutes);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hour).append(':')
.append(min ).append(" ").append(timeSet).toString();
et1.setText(aTime);
}
};