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

梦想与她 提交于 2020-01-11 05:42:26

问题


I am trying to parse a string to date, but the output look incorrect: 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 Output:
Formatted Date Sun Oct 07 12:19:24 IST 2012


回答1:


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

H Hour in day (0-23) Number 0

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

Yes, formatting characters are case sensitive.




回答2:


The hh is for when you're doing am / pm, you need HH



来源:https://stackoverflow.com/questions/13245944/java-simpledateformat-parses-121900-to-001900

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!