2
如何將Java時間庫中的UTC時間轉換爲CET時間?如何在java中將UTC時間轉換爲CET時間
我triing機智這一點,但它看起來像我做錯了什麼
DateTime dateTime = new LocalDateTime(utdDate.getTime()).toDateTime(DateTimeZone.forID("CET"));
如果我用這個,我出去的同時,我插嘴說。
如何將Java時間庫中的UTC時間轉換爲CET時間?如何在java中將UTC時間轉換爲CET時間
我triing機智這一點,但它看起來像我做錯了什麼
DateTime dateTime = new LocalDateTime(utdDate.getTime()).toDateTime(DateTimeZone.forID("CET"));
如果我用這個,我出去的同時,我插嘴說。
我強烈建議你由於固有的本地化性質,避免使用像「CET」這樣的時區名稱。這可能只適用於最終用戶的格式化輸出,而不適用於內部編碼。
CET代表了許多不同的時區的ID,如IANA-ID的Europe/Berlin
,Europe/Paris
等。在我的時區「歐洲/柏林」你的代碼就像如下:
DateTime dateTime =
new LocalDateTime(utdDate.getTime()) // attention: implicit timezone conversion
.toDateTime(DateTimeZone.forID("CET"));
System.out.println(dateTime.getZone()); // CET
System.out.println(dateTime); // 2014-04-16T18:39:06.976+02:00
記住表達new LocalDateTime(utdDate.getTime())
implicitly uses the system timezone for conversion和意志因此如果您的CET區域在內部用您的系統時區與您的系統時區相比具有相同的時區偏移量,則不會改變任何內容爲了迫使JodaTime識別的UTC-輸入你應該像這樣指定它:
Date utdDate = new Date();
DateTime dateTime = new DateTime(utdDate, DateTimeZone.UTC);
System.out.println(dateTime); // 2014-04-16T16:51:31.195Z
dateTime = dateTime.withZone(DateTimeZone.forID("Europe/Berlin"));
System.out.println(dateTime); // 2014-04-16T18:51:31.195+02:00
這個例子保留了即時是在UNIX以來毫秒爲單位的絕對時間。如果您想保留字段,從而改變瞬間,那麼你可以使用的方法withZoneRetainFields
:
Date utdDate = new Date();
dateTime = new DateTime(utdDate, DateTimeZone.UTC);
System.out.println(dateTime); // 2014-04-16T16:49:08.394Z
dateTime = dateTime.withZoneRetainFields(DateTimeZone.forID("Europe/Berlin"));
System.out.println(dateTime); // 2014-04-16T16:49:08.394+02:00
http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-另一時區,在Java的 – Jay