3

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

Bill B
  • 51
  • 2
  • How are u creating alarmTime each time? And in the parenthesis is the number the process id? why is it the same for the case 1 and case 2 if you have re-executed your code again? – Vahid Oct 04 '17 at 19:50
  • That the process id is the same is the basic problem I am seeing. When the process goes away and a new one starts (with locale set to German), I am getting the desired result (case 3). – Bill B Oct 04 '17 at 21:39
  • could u share the code where you are creating the alarmTime?And, are u developing for Android? – Vahid Oct 05 '17 at 14:36

1 Answers1

0

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.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks for this, Andreas, I will investigate a bit more based on what you show here. I am using JDK 1.7.0_151, not sure what JodaTime version (is there a way to discover this programmatically?) – Bill B Oct 04 '17 at 21:40
  • Don't know, but the jar file name usually has the version number. – Andreas Oct 04 '17 at 23:40
  • @BillB You can try `DateTime.class.getPackage().getSpecificationVersion()` or `DateTime.class.getPackage().getImplementationVersion()`. Anyway, I also tried to reproduce the problem (JDK 1.7.0_80 in Eclipse), without success. Maybe it's something specific to Android. –  Oct 05 '17 at 13:18