Java中new Date().getTime()指定時(shí)區(qū)的時(shí)間戳問題小結(jié)
1. getTime()返回值
Java和JavaScript都支持時(shí)間類型Date,他們的getTime()方法返回的是毫秒數(shù)。默認(rèn)返回的是13位數(shù)字,單位是毫秒。
2. 注意事項(xiàng)
/**
* 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();
}容易造成誤解的地方:
如果程序運(yùn)行在東八區(qū),它返回北京時(shí)間1970年01月01日08時(shí)00分00秒起至現(xiàn)在東八區(qū)時(shí)間的總毫秒數(shù)。如果運(yùn)行在UTC時(shí)區(qū)則返回1970年01月01日00時(shí)00分00秒起至當(dāng)前UTC時(shí)間的總毫秒數(shù)??雌饋硭坪鮣etTime()方法獲取的時(shí)間戳與程序所運(yùn)行的時(shí)區(qū)有關(guān)。
Perdió的解釋非常好,摘抄如下:
其實(shí)不是的,getTime()本身是沒有問題,取到的timestamp就是從1970-01-01 00:00:00(UTC)起到當(dāng)前的毫秒數(shù)。與程序真實(shí)運(yùn)行的容器(服務(wù)器)所在的時(shí)區(qū)無關(guān)。東八區(qū)"北京時(shí)間1970年01月01日08時(shí)00分00秒"不就是UTC的1970年01月01日00時(shí)00分00秒嗎。
3.Java獲取指定時(shí)區(qū)的時(shí)間戳
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;
}調(diào)用示例:
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"));
? ? }程序運(yùn)行結(jié)果:
-------------------- 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
總結(jié)
運(yùn)行結(jié)果可以看出,在Java中Date.getTime()獲取到的時(shí)間戳其實(shí)是東8區(qū)的時(shí)間“2019-09-24 00:00:00”(即返回的是北京時(shí)間1970年01月1日0點(diǎn)0分0秒以來的毫秒數(shù),對(duì)應(yīng)UTC時(shí)間1970年01月1日8點(diǎn)0分0秒以來的毫秒數(shù),其數(shù)值大小等于0時(shí)區(qū)的“2019-09-23 16:00:00”所對(duì)應(yīng)的時(shí)間戳)所對(duì)應(yīng)得時(shí)間戳。
到此這篇關(guān)于Java中new Date().getTime()時(shí)間戳問題小結(jié)的文章就介紹到這了,更多相關(guān)Java getTime()時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?Optional優(yōu)雅處理空值與鏈?zhǔn)睫D(zhuǎn)換的實(shí)戰(zhàn)指南
Optional?是?Java?8?引入的一個(gè)容器類,這篇文章主要介紹了Java8中Optional類的使用場(chǎng)景和方法,包括創(chuàng)建Optional、處理null、鏈?zhǔn)秸{(diào)用以及與Stream的配合等,希望對(duì)大家有所幫助2026-06-06
Liquibase 在 Spring Boot 中的使用詳細(xì)介紹
Liquibase 提供了靈活的變更集機(jī)制,支持創(chuàng)建表、修改列、填充數(shù)據(jù)、回滾變更等多種操作,本文將通過多個(gè)豐富的示例,詳細(xì)講解如何在 Spring Boot項(xiàng)目中使用 Liquibase,感興趣的朋友跟隨小編一起看看吧2024-12-12
springboot?使用websocket技術(shù)主動(dòng)給前端發(fā)送消息的實(shí)現(xiàn)
這篇文章主要介紹了springboot?使用websocket技術(shù)主動(dòng)給前端發(fā)送消息的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java GUI事件處理及添加對(duì)話框?qū)崿F(xiàn)方式
文章介紹了Java中事件處理的基本概念和實(shí)現(xiàn)方法,包括事件源、事件對(duì)象、監(jiān)聽器以及不同類型的事件處理,如按鈕點(diǎn)擊、鼠標(biāo)、鍵盤和窗口事件,還提供了使用JOptionPane進(jìn)行對(duì)話框操作的示例2026-02-02
springboot 通過代碼自動(dòng)生成pid的方法
這篇文章主要介紹了springboot 通過代碼自動(dòng)生成pid的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07

