Date轉換時間地區

2021-10-07 04:08:30 字數 406 閱讀 7091

當在不同的時區使用相同程式,時間的值只會為當地時間,這樣就會造成時間混亂,為了避免這個問題,可以將不同時區的時間先轉換為同一時區時間後儲存。

轉換為utc時間

date now =

newdate()

;******dateformat sdf =

new******dateformat

("yyyy-mm-dd hh-mm-ss");

sdf.

settimezone

(timezone.

gettimezone

("utc");

string date = sdf.

format

(now)

;

這樣便能將現在的時間轉為utc時間了

時間戳 和date 轉換

1.檢視指定時間的時間戳 1.檢視當前時間 date s 檢視指定時間 date d 2008 01 01 s 1199116800 date d 20080101 s 1199116800 2.將時間戳轉換成date date d 1970 01 01 utc 1199116800 seconds...

Date轉換,獲取時間相差值

date和string型別互相轉化 dateformat dateformat new dateformat yyyy mm dd hh mm ss date currenttime dateformat.parse 2020 4 12 14 15 00 system.out.println cur...

日期與時間戳轉換 Date物件轉換方法

let time 1540956458000 var date new date time 時間戳為10位需 1000,時間戳為13位的話不需乘10001.1 定義和用法 tolocalestring 方法可根據本地時間把 date 物件轉換為字串,並返回結果。date.tolocalestring...