adoptium / adoptium-support

For end-user problems reported with our binary distributions
Apache License 2.0
43 stars 15 forks source link

SimpleDateFormat AM/PM indicator case inconsistent #1086

Open LetsSamba opened 2 weeks ago

LetsSamba commented 2 weeks ago

Please provide a brief summary of the bug

Adoptium Java formats the AM/PM indicator to lower case but Java 8 formats it to upper case.

Did you test with the latest update version?

Please provide steps to reproduce where possible

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class AmPmDemo {
    public static void main(String[] args) {
        System.out.println(Locale.getDefault());
        SimpleDateFormat sdf = new SimpleDateFormat("a hh:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Calendar calendar = Calendar.getInstance();
        System.out.println(sdf.format(calendar.getTime()));
    }
}

Expected Results

Java 8 outputs the following:

en_GB PM 01:50

Therefore that is what I expected from Adoptium Java.

Actual Results

I got this:

en_GB pm 01:50

What Java Version are you using?

openjdk 21.0.3 2024-04-16 LTS OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)

What is your operating system and platform?

Windows 11 22H2 on x86-64

How did you install Java?

Using the MSI.

Did it work before?

Yes, the output is the same as Java 8 but the case of the AM/PM indicator has changed.

Did you test with other Java versions?

java version "1.8.0_411"
Java(TM) SE Runtime Environment (build 1.8.0_411-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.411-b09, mixed mode)

Relevant log output

No response

jerboaa commented 2 weeks ago

This seems locale dependent and en_GB provides different output between JDK versions. Likely due to changes in Locale providers. Specifying -Djava.locale.providers=compat results in upper case formatting in JDK 21.

If you are relying on the upper case spelling there is a SimpleDateFormatter constructor taking a DateFormatSymbols instance, which you can configure to use upper-case AM/PM indicators using setAmPmStrings() which is more reliable.

jerboaa commented 2 weeks ago
import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class AmPmDemo {
    public static void main(String[] args) {
        System.out.println(Locale.getDefault());
    DateFormatSymbols dfs = new DateFormatSymbols();
    dfs.setAmPmStrings(new String[] { "AM", "PM" });
        SimpleDateFormat sdf = new SimpleDateFormat("a hh:mm", dfs);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Calendar calendar = Calendar.getInstance();
        System.out.println(sdf.format(calendar.getTime()));
    }
}
LetsSamba commented 2 weeks ago

@jerboaa Thanks for your workaround. My expectation was that upgrading from Java 8 to Java 21 should be a painless experience, as long as I wasn't trying to use something that has been deprecated. In my case, some JUnit tests had failed and I had to spend time investigating.