How do i convert the following String Date into Data format in Java?
"10/01/2012 06:45:23:245946"
I am using the following code
dateFormat = new SimpleDateFormat("MM/dd/yyyy hh24:mm:ss:SSS");
java.util.Date parsedDate = dateFormat.parse("10/01/2012 06:45:23:245946");
And i am getting the following error
java.text.ParseException: Unparseable date: "10/01/2012 06:45:23:245946"
SimpleDateFormat中没有hh24
,你应该使用HH
Your pattern is wrong. Try:
"MM/dd/yyyy HH:mm:ss:SSS"
There is no hh24
in date matching pattern.
The pattern for hour is as follows:
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
See the whole date pattern on SimpleDateFormat javadoc.
You're almost there.
Get rid of the 24 after hh and change it to HH, that should make it work.
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSS");
Date parsedDate = dateFormat.parse("10/02/2012 06:45:23:245946");
System.out.println(parsedDate);
This will give you an error in time but parse the date successfull, as will all of our answers. This is fixed by trimming the milliseconds down to 3 digits from 245946
to 245
If you do however want to use 6 digits I would suggest looking into the JodaTime API for more advanced datehandling as JodaTime handles microseconds. But as for java.util.Date
, you're out of luck I'm afraid.
Read this bugreport why: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4148168
EDIT: Thanks Jesper for pointing out my bad wording
The 24
in your date format is an invalid format specifier. Remove it. HH
is the equivalent of hours on a 24-hour scale.
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSS");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.