1

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?

scaedu
  • 13
  • 2
  • 1
    You could better `Assert.assertEquals("2000-01-02",result);` to gain more informative failure information. – Selaron Oct 25 '18 at 08:15
  • Have you tried a format of `"yyyy-MM-dd"`? Maybe the capital `"Y"` is the problem... I tried printing it and without capital `"Y"` it works. – deHaar Oct 25 '18 at 08:16
  • yyyy is working, but the thing is why 'YYYY' is working in the older version? – scaedu Oct 25 '18 at 08:24
  • For the result that you want, you don’t need a formatter. Just use `LocalDate.of(2000,1,2).toString()`. – Ole V.V. Oct 25 '18 at 15:53
  • Apart from the wrong case (`Y` instead of `y`) there’s a locale difference playing tricks on you. The JVM where you get the error has a default locale that uses ISO 8601 weeks. In ISO 8601, Sunday Januar 1, 2000 belongs to the last week of 1999, so you get 1999. The JVM where it seems to work, uses US (or similar) locale and week scheme. In the US that day belongs to new week year (I even believe it belongs to week 2 of 2000). For other dates you will get incorrect results on this computer too as long as you use `YYYY`. – Ole V.V. Oct 25 '18 at 16:01

2 Answers2

2

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.

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

When I just print the toString() method of the result, it becomes pretty obvious that the formatting passed to the DateTimeFormatter is the problem:

public static void main(String[] args) {
    String result = LocalDate.of(2000,1,2)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    System.out.println(result.toString());

    String wrongResult = LocalDate.of(2000,1,2)
            .format(DateTimeFormatter.ofPattern("YYYY-MM-dd"));
    System.out.println(wrongResult.toString());
}

This prints

2000-01-02
1999-01-02

So maybe the older Java version did not recognize the difference, but the newer one does. For an explanation, have a look at the answer by @ThomasKläger.

deHaar
  • 17,687
  • 10
  • 38
  • 51