7

DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet.

final String DATE_FORMAT = "YYYYMM";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DATE_FORMAT);
LocalDateTime startDate = LocalDateTime.of(2018,12,29,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201812
LocalDateTime startDate = LocalDateTime.of(2018,12,30,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201912 <------ should be 201812
LocalDateTime startDate = LocalDateTime.of(2018,12,31,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201912 <------ should be 201812

Is this the expected behavior or is there a bug with DateTimeFormatter?

veben
  • 19,637
  • 14
  • 60
  • 80
Aman Gupta
  • 3,627
  • 4
  • 40
  • 64
  • did you recompile after you changed the code? – Stultuske Jan 07 '19 at 08:02
  • 4
    YYYYis week year, yyyy is year – Jens Jan 07 '19 at 08:02
  • Please read [SimpleTimeFormatter javadoc](https://docs.oracle.com/javase/10/docs/api/java/time/format/DateTimeFormatter.html) carefully. Note that this is for Java 10. – Adam Jan 07 '19 at 08:03
  • you need to understand mechanism of YYYY & yyyy. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar – Kandy Jan 07 '19 at 08:03
  • And to make this even more confusing, there are other formatters that use different patterns, most notably [JodaTime](https://www.joda.org/joda-time/key_format.html) – Hulk Jan 07 '19 at 08:21
  • @HULK I agree it may differ to language to language but there are some standard set in every language to avoid any conflict. Please read this URL-https://www.w3.org/TR/NOTE-datetime – Kandy Jan 07 '19 at 08:33
  • 1
    @Kandy that's why I linked to the hugely popular JodaTime library which "is the de facto standard date and time library for Java prior to Java SE 8", and which handles `Y` as `year of era (>=0)` - don't assume anything about which parser/formatter implements which (parts of the) standard unless it explicitly documents compliance. – Hulk Jan 07 '19 at 09:42

3 Answers3

10

This is expected behaviour. YYYY stands for "week-based-year", which is not the same as calendar year (see JavaDoc)

You most probably want to use yyyy, which means "year-of-era"

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
5

YYYY is week year, yyyy is year

So Change final String DATE_FORMAT = "YYYYMM"; ro final String DATE_FORMAT = "yyyyMM"; should give you the correct result. For more informations about the patterns see the javadoc of DateTimeFormatter.

The first week of 2019 starts at Dec 30 of 2018. See this link for more informations about the week years

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
Jens
  • 67,715
  • 15
  • 98
  • 113
  • @OleV.V. The documentation of DateFormatter was not wo good until Java Version 7 so i liked to use the SimpleDateFormat documentation. I have seen it was changed in Version Java 8+ Thnks for the hint – Jens Jan 08 '19 at 12:24
2

y is for "year-of-era" while Y is for week-based-year

Replace:

final String DATE_FORMAT = "YYYYMM";

to:

final String DATE_FORMAT = "yyyyMM";
veben
  • 19,637
  • 14
  • 60
  • 80