Java中new Date().getTime()指定時區(qū)的時間戳問題小結
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(); }
容易造成誤解的地方:
如果程序運行在東八區(qū),它返回北京時間1970年01月01日08時00分00秒起至現在東八區(qū)時間的總毫秒數。如果運行在UTC時區(qū)則返回1970年01月01日00時00分00秒起至當前UTC時間的總毫秒數??雌饋硭坪鮣etTime()方法獲取的時間戳與程序所運行的時區(qū)有關。
Perdió的解釋非常好,摘抄如下:
其實不是的,getTime()本身是沒有問題,取到的timestamp就是從1970-01-01 00:00:00(UTC)起到當前的毫秒數。與程序真實運行的容器(服務器)所在的時區(qū)無關。東八區(qū)"北京時間1970年01月01日08時00分00秒"不就是UTC的1970年01月01日00時00分00秒嗎。
3.Java獲取指定時區(qū)的時間戳
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區(qū)的時間“2019-09-24 00:00:00”(即返回的是北京時間1970年01月1日0點0分0秒以來的毫秒數,對應UTC時間1970年01月1日8點0分0秒以來的毫秒數,其數值大小等于0時區(qū)的“2019-09-23 16:00:00”所對應的時間戳)所對應得時間戳。
到此這篇關于Java中new Date().getTime()時間戳問題小結的文章就介紹到這了,更多相關Java getTime()時間戳內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot項目中idea的pom.xml文件的引用標簽全部爆紅問題解決
這篇文章主要介紹了springboot項目中idea的pom.xml文件的引用標簽全部爆紅問題解決,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12java中Hashtable和HashMap的區(qū)別分析
java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下2013-04-04SpringBoot整合RabbitMQ的5種模式的注解綁定詳解
這篇文章主要介紹了SpringBoot整合RabbitMQ的5種模式的注解綁定詳解,RabbitMQ 是一個消息中間件,它接收消息并且轉發(fā),是"消費-生產者模型"的一個典型的代表,一端往消息隊列中不斷的寫入消息,而另一端則可以讀取或者訂閱隊列中的消息,需要的朋友可以參考下2024-01-01