Reputation: 145
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"
Upvotes: 3
Views: 3318
Reputation: 47172
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: https://bugs.java.com/bugdatabase/view_bug?bug_id=4148168
EDIT: Thanks Jesper for pointing out my bad wording
Upvotes: 4
Reputation: 89169
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.
Upvotes: 5
Reputation: 28687
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");
Upvotes: 2
Reputation: 11120
There is no hh24
in SimpleDateFormat, You should be using HH
Upvotes: 7