Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"; 
like image 928
Android learner Avatar asked Apr 18 '13 13:04

Android learner


People also ask

How do I display AM PM in date format?

There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.

How do you convert HH MM to HH MM SS?

Tips: To convert hh:mm:ss time format to minutes: =((HOUR(A2)*60)+MINUTE(A2)+(SECOND(A2)/60)); To convert hh:mm:ss time format to seconds: =HOUR(A2)*3600 + MINUTE(A2)*60 + SECOND(A2).


2 Answers

Try this..

Date dt = new Date(date1); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa"); String time1 = sdf.format(dt); 
like image 69
Murugan Avatar answered Oct 02 '22 23:10

Murugan


I got answer just doing like this.

startTime = "2013-02-27 21:06:30"; StringTokenizer tk = new StringTokenizer(startTime); String date = tk.nextToken();   String time = tk.nextToken();  SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a"); Date dt; try {         dt = sdf.parse(time);     System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here } catch (ParseException e) {     e.printStackTrace(); } 
like image 40
Android learner Avatar answered Oct 03 '22 00:10

Android learner