Search code examples
javadatetime-formatlocaldate

Java LocalDate Formatting of 2000-1-2 error


Today some tests on a new build machine failed, where on other machines thes where ok. Looking after the problem it shows that

@Test
public void testDateTimeFormater()
{
    String result = LocalDate.of(2000,1,2)
            .format(DateTimeFormatter.ofPattern("YYYY-MM-dd"));
    Assert.assertTrue(result,result.equals("2000-01-02"));
}

Results in 'java.lang.AssertionError: 1999-01-02'

The jave version not working is

root@build02:~# java -version 
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

(OS Debian 9, Ubuntu 18.04)

where as on the developer machine the working java version is

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

(OS Ubuntu 14.04)

Whats the problem? Is there something I can check?


Solution

  • You most probably do not want the format "YYYY-MM-dd", but instead "yyyy-MM-dd".

    "Y" is the week-based year, which is locale-dependent. January 2, 2000 may belong to the week-based year 1999 in some locales and to the week-based year 2000 in some other locales.

    "y" is the year-of-era, that is what is normally used as calendar year.