Reputation: 93
From web services, receiving time as 23:30:00. I want to convert it to the format of 12 hours. I want output something like 11:30 PM (after converting 23:30:00).
Upvotes: 3
Views: 11736
Reputation: 554
I got answer just doing like this , i have tested it (y)
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 1
Reputation: 1
One Line Code
String time = null;
try{
time = new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("HH:mm:ss").parse("your_time");
}catch{
}
in your_time
you have to give input which you want to convert.
Upvotes: 0
Reputation: 4092
Use SimpleDateFormat to convert from One Time/Date Format to Another.
Log.v("12HRS Time", getFormatedDateTime("23:30:00", "HH:mm:ss", "hh:mm a"))
public static String getFormatedDateTime(String dateStr, String strReadFormat, String strWriteFormat) {
String formattedDate = dateStr;
DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault());
DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault());
Date date = null;
try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
}
if (date != null) {
formattedDate = writeFormat.format(date);
}
return formattedDate;
}
For SimpleDateFormat Reference:- https://developer.android.com/reference/java/text/SimpleDateFormat.html
Upvotes: 7
Reputation: 43
public static void convert(String time) throws Exception {
try {
SimpleDateFormat t24 = new SimpleDateFormat("HH:mm");
SimpleDateFormat t12 = new SimpleDateFormat("hh:mm a");
Date date = t24.parse(time);
System.out.println(t12.format(date));
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 29
android.text.format.DateFormat myDate = new android.text.format.DateFormat();
myDate.format("hh:mm a", new java.util.Date());
Upvotes: 0
Reputation: 2533
Just use SimpleDateFormat
like this.....
public String GetTimeWithAMPMFromTime(String dt) {
try {
SimpleDateFormat inFormat = new SimpleDateFormat("HH:mm:ss");
Date date = inFormat.parse(dt);
SimpleDateFormat outFormat = new SimpleDateFormat("hh:mm a");
String goal = outFormat.format(date);
return goal;
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
call the method...
String YOUR_TIME = GetTimeWithAMPMFromTime(WEB_SERVICE_TIME);
Upvotes: 2
Reputation: 486
Try this.
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Upvotes: 0
Reputation: 17146
Try SimpleDateFormat
Example
Date dateToFormat = new Date(someDate);
SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a");
String formattedDate = dateFormatExpression.format(dateToFormat);
Upvotes: 1
Reputation: 1782
Easiest way to get it by using date pattern - h:mm a, where
h - Hour in am/pm (1-12)
m - Minute in hour
a - Am/pm marker
Code snippet :
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
for more information see this link
Upvotes: 3