sa_nyc
sa_nyc

Reputation: 981

Cannot Parse Date and get Milliseconds

I am trying to parse a String into Date.

I am using

new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS").parse("20140923-14:32:34.456")

My output is: (Date object)

Tue Sep 23 14:32:34 EDT 2014

I dont understand why I do not get the milliseconds. I need the date along with milliseconds and not just the milliseconds.

Expected Output: (Date object)

Tue Sep 23 14:32:34.456 EDT 2014

thanks

PS: I dont want a string object in the end but a Date.

Upvotes: 0

Views: 996

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338326

The answer by Jon Skeet is correct.

Joda-Time

I will add some example code using the Joda-Time library.

The java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat classes bundled with Java are notoriously troublesome, flawed in both design and implementation. Avoid them. Use either Joda-Time or the java.time package in Java 8 (inspired by Joda-Time, defined by JSR 310).

In both Joda-Time and java.time, a date-time object knows its own assigned time zone, unlike in java.util.Date. Another difference is that both those good libraries use immutable objects where we instantiate fresh objects based on original object rather than alter ("mutate") the original.

DateTimeZone timeZone = DateTimeZone.UTC; // Or DateTimeZone.forID( "America/Montreal" )
DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyyMMdd-HH:mm:ss.SSS" ).withZone( timeZone );
DateTime dateTime = formatter.parseDateTime("20140923-14:32:34.456"); // Parsed *and* assigned the specified time zone.
String output = dateTime.toString();

You can convert to another time zone.

DateTime dateTimeKolkata = dateTime.withZone( DateTimeZone.forID( "Asia/Kolkata" ) );

If you really need a java.util.Date, perhaps required by other classes, convert from Joda-Time.

java.util.Date date = dateTime.toDate();

Upvotes: 0

RedCondor
RedCondor

Reputation: 11

Your Date object does already contain the correct milliseconds. It's just that, when you are writing the Date object back out again, the default String representation of a Date does not include milliseconds.

You presumably have:

DateFormat myFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
Date myDate = myFormat.parse("20140923-14:32:34.456");

To output the date with milliseconds, you should use:

System.out.println(myFormat.format(myDate));

Instead of reusing myFormat, you could use a different DateFormat that also includes milliseconds in its specification.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500055

You're just printing out the result of calling Date.toString() which happens not to include the milliseconds. If you print out the result of calling getTime() on the date, you'll see that it ends in "456", showing that it has parsed the milliseconds.

Also, don't be fooled by the "EDT" part of the output of Date.toString(). It doesn't mean that this is a Date object "in" EDT... a Date doesn't have a time zone at all; it's just a number of milliseconds since the unix epoch.

I'd advise you to explicitly set the time zone of your SimpleDateFormat, however - even if you deliberately set it to the system default time zone. By doing it explicitly, you make it clear to the reader that you meant to use that time zone, rather than just that you failed to think about it. You should take the same approach to the locale, too - in this case I'd probably specify Locale.US, which is usually a good bet for formats which are designed more for machine-parsing than humans.

Upvotes: 5

Jean Logeart
Jean Logeart

Reputation: 53809

You need to use the format when printing the date you just read too:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS")
Date date = sdf.parse("20140923-14:32:34.456");
System.out.println(date);               // Tue Sep 23 14:32:34 EDT 2014
System.out.println(sdf.format(date));   // 20140923-14:32:34.456

Upvotes: 2

Related Questions