JDK-11 introducing comma in date format
Asked Answered
H

1

6

I have following method that generates the date in Jan 3, 2022 9:06:16 PM format when runs on JDK-8. However, as soon as I changed my jdk to JDK-11, it generates the date in format Jan 3, 2022, 9:12:28 PM. So there is a difference of comma after 2022 in the output. My test case is failing due to this mismatch. I need to keep JDK-11 generating the same format in my service method as show below -

    private DateFormat getLocaleDateFormat(@Nullable final Locale locale, @Nonnull final TimeZone timeZone) {
        final DateFormat localDf;
        if (locale == null) {
            localDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        } else {
            localDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
        }

        final String timeZoneOffsetString = DateUtils.getTimeZoneOffset(timeZone.getRawOffset(), true);
        final String localTimeZoneStr = "GMT" + timeZoneOffsetString;
        final TimeZone localTz = TimeZone.getTimeZone(localTimeZoneStr);
        localDf.setTimeZone(localTz);

        return localDf;
    }

My Test Method looks like this -

@Test
  public void canGetAuditReportWithTimestampLocalized() throws ParseException {
    /* All parse operations will fail in case of wrong value */
      String localDateValue = aGetAuditReportRequest().withHeader(HttpHeaders.ACCEPT_LANGUAGE.toString(), "en-US,en")
              .getNow(ReportsTable.class).getData().get(0).get(0);
      SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US)
              .parse(localDateValue);
}

The parse() fails at index 11 which is exact position of comma in the date format. To my surprise, manually removing the character at index 11 using string builder or string concatenation introduced some non-english value like this - "2022年1月3日 下午11:35:57".

    public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
                pos.errorIndex);
        return result;
    }

How do I make jdk-11 generate a same formatted date?

Hex answered 4/1, 2022 at 0:21 Comment(5)
This looks like a change to one of the default date formats for your locale. What locale is your Java application using?Whiffet
If you want a specific date format you should better define it using the pattern, then it will not change: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/…Mendiola
Thou should’st not regard that human language ne’r changes. These locale sensitive formats are not supposed to be used as data exchange format between different systems.Lw
Not that it will solve your issue, I strongly recommend you don’t use DateFormat, TimeZone and Date. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use DateTimeFormatter, ZoneId and ZonedDateTime, all from java.time, the modern Java date and time API.Linettelineup
Why your requirement to avoid that comma? They put it in there because users like it (simplified explanation but true).Linettelineup
C
4

The difference in the date format is caused by the default locale provider change starting JDK 9, more details regarding that can be found here.

You can enable JDK 8-compatible behavior in JDK 11 by setting the next option:

-Djava.locale.providers=COMPAT
Cotemporary answered 18/10, 2022 at 21:18 Comment(2)
For the benefit of readers in the future, do you know whether this is a temporary opt out that is likely to be deprecated (or already has been in a version newer than 11)?Diadromous
Nothing I know, I expect it to stay. They renamed from JRE to COMPAT from Java 8 to Java 9, to me it doesn’t sound like a plan to abandon it.Linettelineup

© 2022 - 2025 — McMap. All rights reserved.