问题
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