Calendar TimeZone SimpleDateFormat
阿新 • • 發佈:2019-05-22
關於Calendar類的使用可參考:Java Calendar類的使用總結
獲取日曆(Calendar):java.util.Calendar#getInstance()
獲取時區TimeZone:
TimeZone.getTimeZone("GMT+00:00"); 或:TimeZone.getTimeZone("UTC"); TimeZone.getTimeZone("GMT+08:00"); 或:TimeZone.getTimeZone("CTT"); 或:TimeZone.getTimeZone("Asia/Shanghai");
ZoneID可參考:java.time.ZoneId#SHORT_IDS
default TimeZone設定:
1、java.util.TimeZone#setDefault(TimeZone zone)
2、VM options:-Duser.timezone=UTC
語言環境Locale:java.util.Locale#SIMPLIFIED_CHINESE(中國大陸)
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance("UTC"); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai")); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CTT")); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CTT"), Locale.SIMPLIFIED_CHINESE); calendar.set(Calendar.YEAR, 2099); calendar.get(Calendar.HOUR_OF_DAY); //可以獲取傳入TimeZone對應的時間 calendar.getTime(); //獲取的依然是defaultTimeZone對應的時間
java.util.Calendar#getTime 原始碼: public final Date getTime() { return new Date(getTimeInMillis()); } 因此,通過calendar.getTime()獲取的依然是defaultTimeZone對應的時間
SimpleDateFormat轉變時區:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); format.setTimeZone(TimeZone.getTimeZone("CTT")); format.format(new Date()); format.format(c