Convert time value to format “hh:mm Am/Pm” using Android

Android learner picture Android learner · Apr 18, 2013 · Viewed 99.2k times · Source

I am getting date value from database like "2013-02-27 06:06:30" using StringTokenizer I will get time separately like below

String startTime = "2013-02-27 06:06:30";

StringTokenizer token = new StringTokenizer(startTime);
String date1 = token.nextToken();  
String time1 = token.nextToken(); 

and in time1 I am getting the result 06:06:30,

Can I re-store it in another variable of type String as follows?

String displayValue = "06:06 AM";

And if time1 variable has the value of

String time = 16:00:00;

then it should be converted to:

String displayValue = "04:00 PM";

Answer

Murugan picture Murugan · Apr 18, 2013

Try this..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);