Using Joda DateTimeFormatter.withLocale() only affects the string value for am/pm, not the time format

Viewed 641

Explicitly using withLocale to set the locale to German results in the am/pm string being localized in the time value, but does not convert to the 24 hour time format which is correct for that locale.

When a new process is created, with the Locale.setLocale(Locale.GERMAN); having been done, the format is correct.

Shouldn't withLocale() affect all aspects of the locale in question?

// Code snippet where call is being made:
Log.d(TAG, "XYZZY getDefault(): " + Locale.getDefault().toString());
DateTimeFormatter timeFormat = DateTimeFormat.shortTime()
          .withLocale(Locale.getDefault());
Log.d(TAG, "XYZZY timeFormat.locale: " +
           timeFormat.getLocale().toString());
dateString = alarmTime.toString(timeFormat);
Log.d(TAG, "XYZZY dateString: "+ dateString);

When the variable alarmTime has a value of 11:00pm (2300 hours):

10-04 14:17:41.492 (23609): XYZZY getDefault(): en_US
10-04 14:17:41.493 (23609): XYZZY timeFormat.locale: en_US
10-04 14:17:41.495 (23609): XYZZY dateString: 11:00 PM

Now switch over to German locale and reexecute the same code, note the string for 'PM' changes to German, but not the time format (the suffix should be suppressed and the time value should be 23:00):

10-04 14:18:15.066 (23609): XYZZY getDefault(): de_DE
10-04 14:18:15.066 (23609): XYZZY timeFormat.locale: de_DE
10-04 14:18:15.067 (23609): XYZZY dateString: 11:00 nachm.

Wait for the process to go away and be restarted, leaving the locale as German, and now the correct time format for German is returned:

10-04 14:18:54.497 (23881): XYZZY getDefault(): de_DE
10-04 14:18:54.497 (23881): XYZZY timeFormat.locale: de_DE
10-04 14:18:54.498 (23881): XYZZY dateString: 23:00

1 Answers

Disclaimer: Not an answer, since I'm unable to reproduce, but written as answer to show detail of how I tried to reproduce problem.

I'm running with locale en_US in time zone America/New_York, using JDK 1.8.0_91 and Joda-Time 2.9.9.

Test

LocalTime time = LocalTime.parse("23:00");
System.out.println(time);
System.out.println(time.toString(DateTimeFormat.shortTime()));
System.out.println(time.toString(DateTimeFormat.shortTime().withLocale(Locale.GERMANY)));

Output

23:00:00.000
11:00 PM
23:00

shortTime() works fine, with and without changing locale.

The JVM default locale was never changed.

Related