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
  • 13,634
  • 15
  • 82
  • 142

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
  • 13,886
  • 6
  • 42
  • 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
  • 11,797
  • 2
  • 26
  • 44