DateTimeFormatter giving wrong format for edge cases

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"

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 wee years

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


final String DATE_FORMAT = "YYYYMM";


final String DATE_FORMAT = "yyyyMM";