Java UTC时间戳

Java 关于UTC时间戳

结论

		System.out.println(" --------LocalDateTime------");
        System.out.println(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli());
        System.out.println(" --------+0------");
        System.out.println(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.of("+0")).toEpochMilli());
        System.out.println(" --------utc时间------");
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(tz);
        System.out.println(cal.getTimeInMillis());// 返回的UTC时间戳
        System.out.println(" --------Date getTime------");
        System.out.println(new Date().getTime());
        System.out.println(" --------Date getTime------");
        System.out.println(Calendar.getInstance().getTimeInMillis());
  

java常用时间戳都是相对于utc 1970-01-01T00:00:00 时间戳,(这样说可能有些别扭,主要用来区别不同时区),通过以上方法获取的毫秒值都是一样的。猜想Date Calendar 等时间对象核心都是存了一个UTC时间戳,但是转化为可见时间字符串的时候计算了时区偏移量。而我们平时理解 new Date() 是当前系统时间,因为是可看懂的部分,已经做了时区偏移。
补 https://www.utctime.net/utc-timestamp 在线UTC 时间戳,实际获取的时间戳可以与此对比。

起因和论证过程

项目需要用到UTC时间戳,让不同客户端在不同时区使用时间戳转换为当地时间。
本是很基础常用的的功能,在网上查却是方法复杂要计算偏移量和众说纷纭。
这里先贴一下我的代码

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

class Time {

    public static void main(String[] args) {
        System.out.println(" --------LocalDateTime------");
        System.out.println(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli());
        System.out.println(" --------+8------");
        System.out.println(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(" --------+0------");
        System.out.println(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.of("+0")).toEpochMilli());
        System.out.println(" --------utc时间------");
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(tz);
        System.out.println(cal.getTimeInMillis());// 返回的UTC时间戳
        System.out.println(" --------Date getTime------");
        System.out.println(new Date().getTime());
        System.out.println(" --------Date getTime------");
        System.out.println(Calendar.getInstance().getTimeInMillis());

    }
}

运行即刻截图运行即刻截图和网上时间戳工具比

https://www.utctime.net/utc-timestamp 在线UTC 时间戳

Date 有方法获取时间戳

 /**
     * 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();
    }

根据官方源码注释可见,此方法是获取相对于utc 1970-01-01T00:00:00 的毫秒值。
但是网上还有些说法说这个毫秒值是有当前时区属性的,按常理说我们常用的new Date() 方法是带有时区概念,所以有人理解这个毫秒值是时区属性的。 但是多想几个问题:
1.有没有听过不同时区的标准时间的起始时间,比如北京时间的时间戳是new Date().getTime()的毫秒值是相对于 北京时区 1970-01-01T00:00:00?每个时区的都同理取自 自己时区时间的 1970-01-01T00:00:00 ?这样的时间戳有什么意义
2.java源码注释java1.1版本开始的注释 就有问题?源码应该是我们最大的凭证
3.https://stackoverflow.com/questions/2403109/how-to-make-date-gettime-returns-utc-time/2404790#2404790

java.util.Date has no concept of timezone. It simply holds time relative to epoch, which is Jan 1 1970 00:00:00 UTC. Date is a model, separate from the view. When you display the date, the concept of timezone then is applied. Date’s toString() displays a human readable date in the default timezone. You can either use a DateFormat to display a Date in a different timezone (such as UTC), or change the JVM’s default timezone.
这里贴一下有道翻译,
java.util。日期没有时区的概念。它只是保存相对于epoch的时间,即UTC时间1970年1月1日00:00:00。Date是一个模型,与视图分离。在显示日期时,将应用时区的概念。Date的toString()以默认时区显示人类可读的日期。可以使用DateFormat在不同时区(如UTC)显示日期,也可以更改JVM的默认时区。

综上所述: date的getTime() 就是获取相对于UTC的时间戳,(同理:cal.getTimeInMillis()),时间戳就应该是一个相对于一个时区的标准时间, 如果有多个就没有参考意义了。我们常见的Date 是存了这样一个时间戳,不过输出成可看懂的形式时候,都加了偏移量,或者在格式化的位置处理了时间偏移。
我做了论证:
切换了时区 用Date().getTime() 打印

黑德兰 UTC +3:30
1602668209818
首尔 UTC/GMT +9
1602668243630
圣诞岛 UTC/GMT +11:
1602668295623
Date().getTime()如果有时区概念,就会相差若干小时,但是上面我测试得到 时间戳是一致的(中间有切换系统时间和运行代码的时间)

补充:
Date 我猜想getSecond()、getYear()、 getTimezoneOffset() 等获取具体时间值的废弃,可能都是想让这个date 这个对象更纯粹一点 不牵扯具体系统时间 时区。

有问题欢迎讨论

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unit uTimeZonesMgr; interface uses Windows, SysUtils, Classes, Registry, DateUtils; type {* 用于读取时区注册表TZI(长度为44)的属性值,存储时区信息 *} PRegTZIInfo = ^TRegTZIInfo; TRegTZIInfo = record Bias: Longint; StandardBias: Longint; DaylightBias: Longint; StandardDate: TSystemTime; DaylightDate: TSystemTime; end; {* 单个时区管理对象 *} TTimeZone = class private FTimeZoneName: string; //时区的显示名 FDisplay: string; //夏令时的名字 FDlt: string; //时区标准名字 FStd: string; //是Time zone information的缩写,描述时区的一些重要信息 FTZI: PRegTZIInfo; function GetSelfTimeZoneInformation: TTimeZoneInformation; public constructor Create; destructor Destroy; override; function UTCToLocalDateTime(const AUTC: TDateTime; var ALocalDateTime: TDateTime): Boolean; function LocalDateTimeToUTC(const ALocalDateTime: TDateTime; var AUTC: TDateTime): Boolean; public property TimeZoneName: string read FTimeZoneName write FTimeZoneName; property Display: string read FDisplay write FDisplay; property Dlt: string read FDlt write FDlt; property Std: string read FStd write FStd; property TZI: PRegTZIInfo read FTZI write FTZI; end; {* 所有时区管理对象 *} TTimeZones = class private FTimeZoneKeyPath: string; FTimeZoneList: TStringList; FDefaultTimeZone: TTimeZone; procedure CollectTimeZone; procedure DestoryTimeZones; procedure CheckISDefaultTimeZone(ATimeZone: TTimeZone); public constructor Create; destructor Destroy; override; function FindTimeZone(const ADisplay: string): TTimeZone; public property TimeZoneList: TStringList read FTimeZoneList; property DefaultTimeZone: TTimeZone read FDefaultTimeZone; end; implementation { TTimeZones } procedure TTimeZones.CheckISDefaultTimeZone(ATimeZone: TTimeZone); var DefaultTimeZone: TTimeZoneInformation; begin GetTimeZoneInformation(DefaultTimeZone); if (ATimeZone.TZI.Bias = DefaultTimeZone.Bias) and (ATimeZone.Std = DefaultTimeZone.StandardName) then FDefaultTimeZone := ATimeZone; end; procedure TTimeZones.CollectTimeZone; var reg, tempReg: TRegistry; tempKeyPath: string; tempTimeZoneStrings: TStrings; iCir: Integer; tempTimeZone: TTimeZone; begin reg := TRegistry.Create; try reg.RootKey := HKEY_LOCAL_MACHINE; reg.OpenKey(FTimeZoneKeyPath, False); tempTimeZoneStrings := TStringList.Create; try reg.GetKeyNames(tempTimeZoneStrings); for iCir := 0 to tempTimeZoneStrings.Count - 1 do begin tempKeyPath := FTimeZoneKeyPath + '\' + tempTimeZoneStrings.Strings[iCir]; tempReg := TRegistry.Create; try tempReg.RootKey := HKEY_LOCAL_MACHINE; tempReg.OpenKey(tempKeyPath, False); tempTimeZone := TTimeZone.Create; tempTimeZone.TimeZoneName := tempTimeZoneStrings.Strings[iCir]; tempTimeZone.Display := tempReg.ReadString('Display'); tempTimeZone.Std := tempReg.ReadString('Std'); tempTimeZone.Dlt := tempReg.ReadString('Dlt'); tempReg.ReadBinaryData('TZI', tempTimeZone.TZI^, SizeOf(TRegTZIInfo)); FTimeZoneList.AddObject(tempTimeZone.Display, tempTimeZone); if FDefaultTimeZone = nil then CheckISDefaultTimeZone(tempTimeZone); finally tempReg.CloseKey; tempReg.Free; end; end; finally tempTimeZoneStrings.Free; end; finally reg.CloseKey; reg.Free; end; end; constructor TTimeZones.Create; begin FTimeZoneKeyPath := '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones'; FTimeZoneList := TStringList.Create; FTimeZoneList.Sorted := True; FDefaultTimeZone := nil; CollectTimeZone; end; procedure TTimeZones.DestoryTimeZones; var iCir: Integer; tempTimeZone: TTimeZone; begin for iCir := 0 to FTimeZoneList.Count - 1 do begin tempTimeZone := TTimeZone(FTimeZoneList.Objects[iCir]); tempTimeZone.Free; end; FTimeZoneList.Clear; FTimeZoneList.Free; end; destructor TTimeZones.Destroy; begin DestoryTimeZones(); inherited; end; function TTimeZones.FindTimeZone(const ADisplay: string): TTimeZone; var iIndex: Integer; begin Result := nil; FTimeZoneList.Sort; if FTimeZoneList.Find(ADisplay, iIndex) then begin Result := TTimeZone(FTimeZoneList.Objects[iIndex]); end; end; { TTimeZone } constructor TTimeZone.Create; begin FTZI := GetMemory(SizeOf(TRegTZIInfo)); FillMemory(FTZI, SizeOf(TRegTZIInfo), 0); end; destructor TTimeZone.Destroy; begin FreeMemory(FTZI); inherited; end; function TTimeZone.GetSelfTimeZoneInformation: TTimeZoneInformation; begin GetTimeZoneInformation(Result); Result.Bias := TZI^.Bias; Result.StandardBias := TZI^.StandardBias; Result.StandardDate := TZI^.StandardDate; Result.DaylightBias := TZI^.DaylightBias; Result.DaylightDate := TZI^.DaylightDate; end; function TTimeZone.LocalDateTimeToUTC(const ALocalDateTime: TDateTime; var AUTC: TDateTime): Boolean; var tempLocalToLocal: TDateTime; iMilliSecond: Int64; begin Result := UTCToLocalDateTime(ALocalDateTime, tempLocalToLocal); if Result then begin if tempLocalToLocal > ALocalDateTime then begin iMilliSecond := MilliSecondsBetween(tempLocalToLocal, ALocalDateTime); AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond * -1); end else begin iMilliSecond := MilliSecondsBetween(ALocalDateTime, tempLocalToLocal); AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond); end; Result := True; end; end; function TTimeZone.UTCToLocalDateTime(const AUTC: TDateTime; var ALocalDateTime: TDateTime): Boolean; var TimeZone: TTimeZoneInformation; stUTC, stLC: SYSTEMTIME; begin Result := False; TimeZone := GetSelfTimeZoneInformation; DateTimeToSystemTime(AUTC, stUTC); if SystemTimeToTzSpecificLocalTime(@TimeZone, stUTC, stLC) then begin ALocalDateTime := SystemTimeToDateTime(stLC); Result := True; end; end; end.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值