Java中new Date().getTime()指定时区的时间戳问题小结
作者:自知自省
1. getTime()返回值
Java和JavaScript都支持时间类型Date,他们的getTime()方法返回的是毫秒数。默认返回的是13位数字,单位是毫秒。
2. 注意事项
/** * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this <tt>Date</tt> object. * * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this date. */ public long getTime() { return getTimeImpl(); }
容易造成误解的地方:
如果程序运行在东八区,它返回北京时间1970年01月01日08时00分00秒起至现在东八区时间的总毫秒数。如果运行在UTC时区则返回1970年01月01日00时00分00秒起至当前UTC时间的总毫秒数。看起来似乎getTime()方法获取的时间戳与程序所运行的时区有关。
Perdió的解释非常好,摘抄如下:
其实不是的,getTime()本身是没有问题,取到的timestamp就是从1970-01-01 00:00:00(UTC)起到当前的毫秒数。与程序真实运行的容器(服务器)所在的时区无关。东八区"北京时间1970年01月01日08时00分00秒"不就是UTC的1970年01月01日00时00分00秒吗。
3.Java获取指定时区的时间戳
public static long getTimeZoneTimeStr(String dateStr,String timeZone) { long result = 0L; int year; int month; int day; int hour; int minute; int second; Calendar calendarTime = Calendar.getInstance(); if(timeZone != null){ TimeZone tz = TimeZone.getTimeZone(timeZone); calendarTime.setTimeZone(tz); } if (null != dateStr && 14 == dateStr.length()) { year = Integer.parseInt(dateStr.substring(0, 4)); month = Integer.parseInt(dateStr.substring(4, 6)); day = Integer.parseInt(dateStr.substring(6, 8)); hour = Integer.parseInt(dateStr.substring(8, 10)); minute = Integer.parseInt(dateStr.substring(10, 12)); second = Integer.parseInt(dateStr.substring(12, 14)); calendarTime.set(1, year); calendarTime.set(2, month - 1); calendarTime.set(5, day); calendarTime.set(11, hour); calendarTime.set(12, minute); calendarTime.set(13, second); result = calendarTime.getTime().getTime(); }else if (null != dateStr && 19 == dateStr.length()) { year = Integer.parseInt(dateStr.substring(0, 4)); month = Integer.parseInt(dateStr.substring(5, 7)); day = Integer.parseInt(dateStr.substring(8, 10)); hour = Integer.parseInt(dateStr.substring(11, 13)); minute = Integer.parseInt(dateStr.substring(14, 16)); second = Integer.parseInt(dateStr.substring(17, 19)); calendarTime.set(1, year); calendarTime.set(2, month - 1); calendarTime.set(5, day); calendarTime.set(11, hour); calendarTime.set(12, minute); calendarTime.set(13, second); result = calendarTime.getTime().getTime(); } return result; }
调用示例:
public static void main(String[] args) { System.out.println("-------------------- 2019-09-24 00:00:00 -----------------------"); System.out.println("local: "+getTimeZoneTimeStr("2019-09-24 00:00:00",null)); System.out.println("Asia/Shanghai: "+getTimeZoneTimeStr("2019-09-24 00:00:00","Asia/Shanghai")); System.out.println("GMT+0800: "+getTimeZoneTimeStr("2019-09-24 00:00:00","GMT+0800")); System.out.println("GMT: "+getTimeZoneTimeStr("2019-09-24 00:00:00","GMT")); System.out.println("UTC: "+getTimeZoneTimeStr("2019-09-24 00:00:00","UTC")); System.out.println("-------------------- 2019-09-23 16:00:00 -----------------------"); System.out.println("local: "+getTimeZoneTimeStr("2019-09-23 16:00:00",null)); System.out.println("Asia/Shanghai: "+getTimeZoneTimeStr("2019-09-23 16:00:00","Asia/Shanghai")); System.out.println("GMT+0800: "+getTimeZoneTimeStr("2019-09-23 16:00:00","GMT+0800")); System.out.println("GMT: "+getTimeZoneTimeStr("2019-09-23 16:00:00","GMT")); System.out.println("UTC: "+getTimeZoneTimeStr("2019-09-23 16:00:00","UTC")); }
程序运行结果:
-------------------- 2019-09-24 00:00:00 -----------------------
local: 1569254400072
Asia/Shanghai: 1569254400115
GMT+0800: 1569254400115
GMT: 1569283200115
UTC: 1569283200115
-------------------- 2019-09-23 16:00:00 -----------------------
local: 1569225600116
Asia/Shanghai: 1569225600116
GMT+0800: 1569225600116
GMT: 1569254400116
UTC: 1569254400116
总结
运行结果可以看出,在Java中Date.getTime()获取到的时间戳其实是东8区的时间“2019-09-24 00:00:00”(即返回的是北京时间1970年01月1日0点0分0秒以来的毫秒数,对应UTC时间1970年01月1日8点0分0秒以来的毫秒数,其数值大小等于0时区的“2019-09-23 16:00:00”所对应的时间戳)所对应得时间戳。
到此这篇关于Java中new Date().getTime()时间戳问题小结的文章就介绍到这了,更多相关Java getTime()时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!