-1

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.

ifloop
  • 8,079
  • 2
  • 26
  • 35

3 Answers3

1

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
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Your answer is here. SimpleDateFormat 24h

Use HH instead of hh with SimpleDateFormat class.

Best regards.

Community
  • 1
  • 1
Arturo
  • 261
  • 1
  • 4
  • 19
0

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
ifloop
  • 8,079
  • 2
  • 26
  • 35