Java Date operation
有關Java Date 相關操作,工作遇到時區轉換的問題,透過此次機會一起統整經過及方法。
前情提要
Java Date 物件底層都是透過TimeZone 來去做時區判斷,但是這會依賴系統時間,即使透過TimeZone setDefaultTimeZone 來去做完時區轉換的方式,但是這無法一勞永逸且不具有彈性的設計,所以才會有這篇文章的誕生。 我這邊也不賣關子,我自己測試下來的結論,Date物件不好操作,日後有關時區問題,我是會棄用他的。
人狠,話不多,上扣!!
/**
* convert date String with time zone to LocalDateTime Object
*
* @param dateStr
* @param formatterPattern
* @param dateStrTimeZone
* @param toTimeZon
* @return
* @throws ParseException
*/
private static LocalDateTime convertDateStrWithTimeZone(String dateStr, String formatterPattern, TimeZone dateStrTimeZone, ZoneId toTimeZon) throws ParseException {
/**
* 這邊會發生兩件事情
* 1.SimpleDateFormat 認定字串 為 GMT+0
* 2.Date Object 將轉換的GMT+0 加上系統預設時區
* Ex. "2023-03-23 12:00:00" -> 是GMT+0 時區的時間
* 當下系統 default 時區是 GMT+8
* 轉換結果就會變成 2023-03-23 20:00:00[GMT+8]
*/
// Create a SimpleDateFormat object for parsing the date string
SimpleDateFormat sdf = new SimpleDateFormat(formatterPattern);
// Set the time zone of the SimpleDateFormat object to GMT+0
sdf.setTimeZone(dateStrTimeZone);
// Parse the date string into an Instant object and transfer to LocalDataTime
return sdf.parse(dateStr).toInstant().atZone(toTimeZon).toLocalDateTime();
}
/**
* @param dateTime
* @param fromZone
* @param toZone
* @return
*/
private static LocalDateTime convertTimeZoneUseZoneId(LocalDateTime dateTime, ZoneId fromZone, ZoneId toZone) {
ZonedDateTime originZoneDateTime = ZonedDateTime.of(dateTime, fromZone);
ZonedDateTime transZoned = originZoneDateTime.withZoneSameInstant(toZone);
return transZoned.toLocalDateTime();
}
/**
* @param dateTime
* @param fromOffset
* @param toOffset
* @return
*/
public static LocalDateTime convertTimeZoneUseZoneOffset(LocalDateTime dateTime, ZoneOffset fromOffset, ZoneOffset toOffset) {
long timeDifference = toOffset.getTotalSeconds() - fromOffset.getTotalSeconds();
return dateTime.plusSeconds(timeDifference);
}其實 convertTimeZoneUseZoneId跟 convertTimeZoneUseZoneOffset 背後是繼承關係,所以基本上是一樣的東西,有可能差別在於效能上,但這部分我就沒有花太多時間著墨了。
測試!
結論
不斷的測試及驗證,最後發現Date 物件對於時區操作,並不是這麼靈活且適合,但我本身就不是那麼愛用Date,Java 8 過後新增的LocalDateTime 等等新增的時間類別,相較於Date 比較好用,但是呢~~~ 永遠都有但是工作上既有產品還是使用Date 所以你還是無法避免這狀況,只能將狀況變成最適合自己的操作方式,盡可能提高自己工作的效率。🔥
Last updated