TimeZone以及時區有關的操作Date/Calendar/SimpleDateFormat
TimeZone
1. System.getProperty("user.timezone") System.setProperty("user.timezone","GMT+8")
2. TimeZone.getDefault().getID() TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"))
Date date = new Date(); System.out.println("date.toString():"+date.toString()); System.out.println("date.getTime()"+date.getTime()); Calendar cal = Calendar.getInstance(); System.out.println("Calendar.getInstance().getTime():"+cal.getTime()); System.out.println("Calendar.getInstance().getTimeInMillis():"+cal.getTimeInMillis()); System.out.println("Calendar.getInstance().getTimeZone():"+cal.getTimeZone()); System.out.println("system user.timezone:" + System.getProperty("user.timezone")); System.out.println("system user.country:" + System.getProperty("user.country")); System.out.println("預設時區:" + TimeZone.getDefault().getID()); System.out.println("--------------------------------------"); System.out.println("After setTimeZone0:"); TimeZone.setDefault(TimeZone.getTimeZone("GMT+2")); date = new Date(); System.out.println("date.toString():"+date); System.out.println("date.getTime()"+date.getTime()); cal = Calendar.getInstance(); System.out.println("Calendar.getInstance().getTime():"+cal.getTime()); System.out.println("Calendar.getInstance().getTimeInMillis():"+cal.getTimeInMillis()); System.out.println("Calendar.getInstance().getTimeZone():"+cal.getTimeZone()); System.out.println("system user.timezone:" + System.getProperty("user.timezone")); System.out.println("system user.country:" + System.getProperty("user.country")); System.out.println("預設時區:" + TimeZone.getDefault().getID()); System.out.println("--------------------------------------"); TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); System.setProperty("user.timezone", "GMT+2"); date = new Date(); System.out.println("date.toString():"+date); System.out.println("date.getTime()"+date.getTime()); cal = Calendar.getInstance(); System.out.println("Calendar.getInstance().getTime():"+cal.getTime()); System.out.println("Calendar.getInstance().getTimeInMillis():"+cal.getTimeInMillis()); System.out.println("Calendar.getInstance().getTimeZone():"+cal.getTimeZone()); System.out.println("system user.timezone:" + System.getProperty("user.timezone")); System.out.println("system user.country:" + System.getProperty("user.country")); System.out.println("預設時區:" + TimeZone.getDefault().getID()); System.out.println("--------------------------------------");
結果:
date.toString():Wed Nov 23 14:41:32 CST 2016 date.getTime()1479883292507 Calendar.getInstance().getTime():Wed Nov 23 14:41:32 CST 2016 Calendar.getInstance().getTimeInMillis():1479883292535 Calendar.getInstance().getTimeZone():sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null] system user.timezone:Asia/Shanghai system user.country:CN 預設時區:Asia/Shanghai -------------------------------------- After setTimeZone0: date.toString():Wed Nov 23 08:41:32 GMT+02:00 2016 date.getTime()1479883292538 Calendar.getInstance().getTime():Wed Nov 23 08:41:32 GMT+02:00 2016 Calendar.getInstance().getTimeInMillis():1479883292538 Calendar.getInstance().getTimeZone():sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] system user.timezone:Asia/Shanghai system user.country:CN 預設時區:GMT+02:00 -------------------------------------- date.toString():Wed Nov 23 14:41:32 CST 2016 date.getTime()1479883292539 Calendar.getInstance().getTime():Wed Nov 23 14:41:32 CST 2016 Calendar.getInstance().getTimeInMillis():1479883292539 Calendar.getInstance().getTimeZone():sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null] system user.timezone:GMT+2 system user.country:CN 預設時區:Asia/Shanghai --------------------------------------
由此可見:
1)Date.toString()依賴於TimeZone中defaultTimezone;Calendar同樣;TimeZone同樣;
2)System.property 中 “user.timezone”與TimeZone中defaultTimezone的關係:
TimeZone取預設值,先取該物件已經設定好的預設值,如果沒有則取system.property中的"user.timezone",再沒有的話才根據“java.home”和“user.country”來獲取。。。。(從TimeZone.getDefault()原始碼可知);
System.property中user.timezone會在TimeZone執行由“java.home”和“user.country”獲取timezone的方法後獲得初值;
link:http://2005713.blog.51cto.com/1995713/698032
3)System.property中 “user.timezone"與 "user.country"無關;
4)Date.getTime() 和 Calendar.getTimeInMillis()相差無幾,此毫秒數都是相對於1970.1.1 00:00:00 GMT.
SimpleDateFormat
public class CalendarTest0 {
public static void main(String[] args) {
String dateStr = null;
SimpleDateFormat format = null;
Date date = null;
System.out.println("system user.timezone:" + System.getProperty("user.timezone"));
System.out.println("system user.country:" + System.getProperty("user.country"));
System.out.println("預設時區:" + TimeZone.getDefault().getID());
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
date = new Date();
dateStr = format.format(date);
System.out.println(dateStr);
System.out.println("Set system.user.timezone=GMT+2:");
System.setProperty("user.timezone", "GMT+2");
Calendar cal = Calendar.getInstance();
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
dateStr = format.format(cal.getTime());
System.out.println(dateStr);
System.out.println("system user.timezone:" + System.getProperty("user.timezone"));
System.out.println("system user.country:" + System.getProperty("user.country"));
System.out.println("預設時區:" + TimeZone.getDefault().getID());
System.out.println("------------------------------");
System.out.println("Set system.user.timezone to GMT+2:");
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
date = new Date();
dateStr = format.format(date);
System.out.println(dateStr);
System.out.println("system user.timezone:" + System.getProperty("user.timezone"));
System.out.println("system user.country:" + System.getProperty("user.country"));
System.out.println("預設時區:" + TimeZone.getDefault().getID());
System.out.println("------------------------------");
System.out.println("Set format.settimezone to GMT+4:");
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
date = new Date();
format.setTimeZone(TimeZone.getTimeZone("GMT+4"));
dateStr = format.format(date);
System.out.println(dateStr);
System.out.println("system user.timezone:" + System.getProperty("user.timezone"));
System.out.println("system user.country:" + System.getProperty("user.country"));
System.out.println("預設時區:" + TimeZone.getDefault().getID());
System.out.println("------------------------------");
System.out.println("Set format.settimezone to GMT+6 and TimeZone.setTimeZone to GMT+8 :");
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
date = new Date();
format.setTimeZone(TimeZone.getTimeZone("GMT+6"));
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
dateStr = format.format(date);
System.out.println(dateStr);
System.out.println("system user.timezone:" + System.getProperty("user.timezone"));
System.out.println("system user.country:" + System.getProperty("user.country"));
System.out.println("預設時區:" + TimeZone.getDefault().getID());
System.out.println("------------------------------");
}
}
Console:
system user.timezone:
system user.country:CN
預設時區:Asia/Shanghai
2016-11-24 18:34:43 +0800
Set system.user.timezone=GMT+2:
2016-11-24 18:34:43 +0800
system user.timezone:GMT+2
system user.country:CN
預設時區:Asia/Shanghai
------------------------------
Set system.user.timezone to GMT+2:
2016-11-24 18:34:43 +0800
system user.timezone:GMT+2
system user.country:CN
預設時區:Asia/Shanghai
------------------------------
Set format.settimezone to GMT+4:
2016-11-24 14:34:43 +0400
system user.timezone:GMT+2
system user.country:CN
預設時區:Asia/Shanghai
------------------------------
Set format.settimezone to GMT+6 and TimeZone.setTimeZone to GMT+8 :
2016-11-24 16:34:43 +0600
system user.timezone:GMT+2
system user.country:CN
預設時區:GMT+08:00
------------------------------
結論:
a.SimpleDateFormat使用的預設時區是TimeZone.getDefault()的時區而非System.getProperty("user.timezone")的時區;
b.SimpleDateFormat.setTimeZone()並不會改變TimeZone的值;
c.當既SimpleDateFormat.setTimeZone()又TimeZone.setDefault()時,SimpleDateFormat取SimpleDateFormat.setTimeZone()設定的值;