1

I have achieved time formate using AM-PM by below code

Date date = new SimpleDateFormat("h:m").parse((mHour+":"+mMinute));
String newString = new SimpleDateFormat("hh:mm a").format(date);
System.out.println("Time - "+newString);

but in above code there is one problem

If I set time 12:00 AM in TimePickerDialog it will display 12:00 AM but if I change time in TimePickerDialog to 12:00 PM it will still display 12:00 AM

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

2 Answers2

7

It's because h is for 12 hour time format. And your TimePickerDialog is sending you a 24 hour format.

Use,

Date date = new SimpleDateFormat("H:m").parse((mHour+":"+mMinute)); // Upper H here.

EDIT:

SimpleDateFormat java doc, Check out the Date and Time Patterns section.

Codebender
  • 14,221
  • 7
  • 48
  • 85
3

hh which is the 1-12 hour format. Try using capital HH, which uses 0-23 format.

" HH:mm"

so use

Date date = new SimpleDateFormat("HH:mm").parse((mHour+":"+mMinute));
sasikumar
  • 12,540
  • 3
  • 28
  • 48