2017-10-04 133 views
2

明確使用withLocale將語言環境設置爲德語會導致am/pm字符串在時間值中進行本地化,但不會轉換爲適用於該語言環境的24小時時間格式。使用Joda DateTimeFormatter.withLocale()僅影響am/pm的字符串值,而不是時間格式

當創建新進程時,Locale.setLocale(Locale.GERMAN);已完成,格式正確。

不應該withLocale()影響有問題的語言環境的所有方面?

// 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); 

當變量alarmTime具有下午11時00的值(2300小時):41.492(23609)::

10-04 14:17 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

現在切換到德語語言環境並重新執行相同的代碼,請注意 字符串「PM」更改爲德語,但不是時間格式(後綴 應該被抑制,時間值應爲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。

等待的過程中消失,重新啓動,使語言爲 德國,現在正在返回德國在正確的時間格式:

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

+0

你是如何每次創建alarmTime的?在括號中是進程ID號?爲什麼情況1和情況2相同,如果你再次執行你的代碼? – Vahid

+0

進程ID相同是我看到的基本問題。當進程消失並開始一個新的(語言環境設置爲德語)時,我得到所需的結果(情況3)。 –

+0

你可以分享你創建alarmTime的代碼嗎?而且,你是在爲Android開發嗎? – Vahid

回答

0

免責聲明:不是一個答案,因爲我無法重現,但寫作回答顯示我試圖重現問題的細節。

我與區域en_US在時區America/New_York運行,使用JDK 1.8.0_91和喬達時間2.9.9

測試

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))); 

輸出

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

shortTime()做工精細,有和沒有變化的區域。

JVM默認語言環境從未更改過。

+0

謝謝你,安德烈亞斯,我會根據你在這裏展示的內容調查一下。我正在使用JDK 1.7.0_151,不知道什麼是JodaTime版本(有沒有一種方法可以通過編程來發現它) –

+0

不知道,但jar文件名通常有版本號。 – Andreas

+0

@BillB您可以嘗試'DateTime.class.getPackage()。getSpecificationVersion()'或'DateTime.class.getPackage()。getImplementationVersion()'。無論如何,我也嘗試重現這個問題(Eclipse中的JDK 1.7.0_80),但沒有成功。也許這是Android的特定情況。 – 2017-10-05 13:18:02