关于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(calendar.getTime());