GMT和UTC
GMT,即格林尼治标准时间,也就是世界时。GMT的正午是指当太阳横穿格林尼治子午线(本初子午线)时的时间。但由于地球自转不均匀不规则,导致GMT不精确,现在已经不再作为世界标准时间使用。
UTC,即协调世界时。UTC是以原子时秒长为基础,在时刻上尽量接近于GMT的一种时间计量系统。为确保UTC与GMT相差不会超过0.9秒,在有需要的情况下会在UTC内加上正或负闰秒。UTC现在作为世界标准时间使用。
所以,UTC与GMT基本上等同,误差不超过0.9秒。
时区
地球自西向东旋转,东边比西边先看到太阳,东边的时间也比西边的早。为了统一世界的时间,1884年的国际经度会议规规定将全球划分为24个时区(东、西各12个时区)。规定英国(格林尼治天文台旧址)为零时区(GMT+00),东1-12区,西1-12区,中国北京处于东8区(GMT+08)。
若英国时间为6点整,则GMT时间为6点整,则北京时间为14点整。
UNIX时间戳
计算机中的UNIX时间戳,是以GMT/UTC时间「1970-01-01T00:00:00」为起点,到具体时间的秒数,不考虑闰秒。这么做当然是为了简化计算机对时间操作的复杂度。
比如我的电脑现在的系统时间为2015年2月27日15点43分0秒,因为我的电脑默认时区为东8区,则0时区的时间为2015年2月27日7点43分0秒,则UNIX时间戳为1425022980秒。
时间中的T
那个T 是 UTC时间
java中形如yyyy-MM-dd’T’HH:mm:ss.SSSZ 转化成各种格式的问题
Z代表0时区,北京时间差8小时
string类型时间转换成date(线程安全)
public static Date parseSync8(String dateStr, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = DATE_PATTERN;
}
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
java.time.LocalDateTime date = java.time.LocalDateTime.parse(dateStr, formatter);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = date.atZone(zoneId);
return Date.from(zdt.toInstant());
}
date类型时间转换成string(线程安全)
public static String formatSync8(Date date, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = DATE_PATTERN;
}
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime now = date.toInstant().atZone(zoneId).toLocalDateTime();
return now.format(formatter);
}
获取默认时区
public static ZoneOffset getZone(){
return OffsetDateTime.now().getOffset();
}
参考资料
1、代码
https://zhuanlan.zhihu.com/p/149302250