0

I have recently changed from org.joda.time package to java.time package and I am wondering how the two differ in using a default locale. Locale is used when parsing dates; the java.time seems to use the system language and country as the default locale while the joda.time uses null, which I can see using

java.time.format.DateTimeFormatter.ISO_DATE.getLocale
org.joda.time.format.DateTimeFormat.fullDateTime().getLocale
en_US
null

The Joda documentation on the getLocale() states

    /**
     * Gets the locale that will be used for printing and parsing.
     * 
     * @return the locale to use; if null, formatter uses default locale at
     * invocation time
     */
    public Locale getLocale() {
        return iLocale;
    }

Which says it's using the default locale at invocation time. Is this default always English "en"? If I change the language on my system, java time wont parse English dates, while Joda time still accepts it.

DwB
  • 37,124
  • 11
  • 56
  • 82
Stumpp
  • 279
  • 4
  • 7
  • 16
  • 1
    [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jun 18 '21 at 15:13
  • I cannot reproduce what you say about Joda-Time accepting dates in English when the system language is different. On my Danish Mac a Joda-Time `DateTimeFormat.longDate()` parses `19. juni 2021` but throws an exception for the English counterpart: `java.lang.IllegalArgumentException: Invalid format: "June 19, 2021"`. – Ole V.V. Jun 19 '21 at 14:57

2 Answers2

0

Short answer, No.

The Jvm uses its own locale which is dependent on the underlying system locale configuration if you want to have a stable default locale configuration you should provide the jvm the arguments related to the locale user.country user.variant, user.variant. It is important also to provide user.timezone

Javier Toja
  • 1,630
  • 1
  • 14
  • 31
0

You should not care. Instead specify desired locale for your parsing and formatting explicitly. If you want the default locale of the JVM to be used, specify Locale.getDefault(Locale.Category.FORMAT).

    DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.GERMAN);
    DateTimeFormatter frenchFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.FRENCH);
    
    LocalDate sample = LocalDate.of(2021, Month.OCTOBER, 23);
    
    System.out.println(sample.format(germanFormatter));
    System.out.println(LocalDate.parse("31. März 2022", germanFormatter));
    
    System.out.println(sample.format(frenchFormatter));
    System.out.println(LocalDate.parse("30 août 2021", frenchFormatter));

Output:

23. Oktober 2021
2022-03-31
23 octobre 2021
2021-08-30
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161