Date: getDisplayName method adds point character to dayOfWeek only on CI

675 views Asked by At

Calling localDate.getDayOfWeek().getDisplayName in my UnitTest running on CircleCI returns a different value compared to running the UnitTest locally.

Here is the simplified sample code:

LocalDate localDate = LocalDate.of(2019, 12, 20);
String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.GERMANY);

assertEquals("Fr", dayOfWeek); // actual = "Fr."

dayOfWeek contains a '.' only on CI but I don't get why and how to fix it (properly).

This is the error log of the UnitTest:

junit.framework.ComparisonFailure: expected:<Fr[.]> but was:<Fr[]>
..

Hint: I'm using ThreeTen Android Backport

UPDATE As mentioned by @OleV.V.and @Arvind Kumar Avinash the reason for the different behaviour (local and CI) is the difference in the JDK versions (local 8.x and CI 11.x).

This leaves a part of my question open: "How to fix this properly?" This the correct/only way to change the JDK version on my CI docker image?

1

There are 1 answers

6
Arvind Kumar Avinash On

Update:

After looking into the updated question, the problem seems to be because of missing library of ThreeTen Android Backport on the machine where CircleCI is running. In the absence of this library, probably it is defaulting to java.time when the code is getting re-compiled on this machine. You should check a few things on this machine:

  1. If the library has been imported successfully.
  2. If there is any setting to import the most appropriate types automatically if some types are missing.
  3. If the JDK version is the same as that of your local machine.

Original answer:

You can use TextStyle.SHORT_STANDALONE

import java.util.Locale;

import org.threeten.bp.LocalDate;
import org.threeten.bp.format.TextStyle;

class Main {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2019, 12, 20);
        String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT_STANDALONE, Locale.GERMAN);
        System.out.println(dayOfWeek);
    }
}

Output:

Fr

I do not get a dot in the output for TextStyle.SHORT on my system though. Nevertheless, if you still want to use TextStyle.SHORT and not have the dot (or any punctuation mark) with it, you can replace every punctuation mark with a blank string.

import java.util.Locale;

import org.threeten.bp.LocalDate;
import org.threeten.bp.format.TextStyle;

class Main {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2019, 12, 20);
        String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.GERMANY);
        System.out.println(dayOfWeek);

        // Remove all punctuation mark
        dayOfWeek = dayOfWeek.replaceAll("\\p{Punct}", "");
        System.out.println(dayOfWeek);
    }
}

Output:

Fr
Fr

Note: The result for TextStyle.SHORT changes with java.time API as shown below:

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2019, 12, 20);

        String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT_STANDALONE, Locale.GERMANY);
        System.out.println(dayOfWeek);

        dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.GERMANY);
        System.out.println(dayOfWeek);

        // Remove all punctuation mark
        dayOfWeek = dayOfWeek.replaceAll("\\p{Punct}", "");
        System.out.println(dayOfWeek);
    }
}

Output:

Fr
Fr.
Fr