I have a String
"2014-11-04 10:30 PM"
, but I want to display as "2014-11-04 22:30
. How can I do this ?
Thanks in advance.
I have a String
"2014-11-04 10:30 PM"
, but I want to display as "2014-11-04 22:30
. How can I do this ?
Thanks in advance.
In Java
SimpleDateFormat you can use hh
for 12
hours representation and HH
for 24
hour representation.
Eg:
String str="2014-11-04 10:30 PM";
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date=df.parse(str);
df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(df.format(date));
Out put:
2014-11-04 22:30
Your answer is here. SimpleDateFormat 24h
Use HH instead of hh with SimpleDateFormat class.
Best regards.
When using Java 8 you can make use of the new Time API
final String input = "2014-11-04 10:30 PM";
final DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm a");
final DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
final LocalDateTime time = LocalDateTime.parse(input, inFormatter);
System.out.println(outFormatter.format(time));
Output:
2014-11-04 22:30