3

I'm the doing the following

Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSSSSSSSS");
System.out.println(dateFormat.format(date));

The output is weird date patterns such as

2013-10-28 04:10:43.000000222 (its June, not October)

While the time, year and day seems to be fine, the month is playing games. Everytime I run the code, its coming out to be a different month, sometimes even going out of range, like

2013-58-28 04:15:43.000000654

Also, if I'm printing the date without formatting it, it seems to be fine, concluding (I think so) that my system date is fine.

Any idea what's the problem here?

qbr
  • 197
  • 2
  • 2
  • 10

2 Answers2

4

Use (capital) MM to indicate month; mm is for minutes only. The SimpleDateFormat javadoc page has all the date format symbols.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

tl;dr

LocalDateTime.parse( 
    "2013-10-28 04:10:43.000000222".replace( " " , "T" ) 
)

Using java.time

The Answer by rgettman is correct. But now both the Question and that Answer are outdated.

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Also, your input string has nanoseconds but those old classes are limited to milliseconds. Fortunately the java.time classes handle nanoseconds.

ISO 8601

Your input string nearly complies with the standard ISO 8601 formats. To comply fully, replace the SPACE in the middle with a T.

String input = "2013-10-28 04:10:43.000000222".replace( " " , "T" ) ;

The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.

LocalDateTime

Your input string lacks any indication of an offset-from-UTC or a time zone. So we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

ldt.toString(): 2013-10-28T04:10:43.000000222

As a LocalDateTime, this does not represent a moment on the timeline. Is that 4 AM in India, 4 AM in France, or 4 AM in Québec? Those would all be different points on the timeline with the 4 AM in India happening several hours earlier in history than the 4 AM times in France or Québec.

ZonedDateTime

If you know for certain the intended time zone, apply it.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z );

zdt.toString(): 2013-10-28T04:10:43.000000222-04:00[America/Montreal]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154