Java - SimpleDateFormat parses 12:19:00 to 00:19:00

I am trying to parse a string to a date, but the result does not look right: Below is my code.

public static void main(String[] args){ Date startDate = new Date(); DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss"); try { startDate = (Date) formatter.parse("07.10.2012 12:19:24"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Formatted Date " + startDate.toString()); } 

Program output:
Formatted date Sun Oct 07 00:19:24 IST 2012

Expected Result:
Formatted date Sun Oct 07 12 : 19: 24 IST 2012

+4
source share
2 answers

You might want to use 24h instead of 12h ...

H Hour a day (0-23) Number 0

h Hour at am / pm (1-12) Number 12

Yes, formatting characters are case sensitive.

+8
source

Xh, when you do am / pm, you need hh

+3
source

Source: https://habr.com/ru/post/1444269/


All Articles