Java – simpledateformat cannot handle 12 months correctly
•
Java
I'm trying to parse the string "2 / 20 / 2012 12:00:00 am" using simpledateformat. It seems that it will come out at 12 p.m Replace
Date fromFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aa") .parse("2/20/2012 12:00:00 AM"); // Calendar months are 0-indexed Date fromCal = new Date(new GregorianCalendar(2012,1,20,0) .getTimeInMillis()); System.out.println(fromFmt); System.out.println(fromCal);
Output:
Mon Feb 20 12:00:00 PST 2012 Mon Feb 20 00:00:00 PST 2012
I hope they both export the latter Is there a problem with my format string?
(and please don't say 'use jodatime'.)
Solution
Use instead:
new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa")
Please note that I am using HH instead of HH The former does so:
HH do this:
You told simpledateformat that you would deliver within an hour in the range of 0-23, but you didn't actually do so That's why you have this problem
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码