JAVA中 System.currentTimeMillis() 与 new Date().getTime() 与 Calendar.getInstance().getTimeInMillis()区别

这是stackoverflow上的一个回答:

If you want to refer to a specific point in time, you can definitely rely on System.currentTimeInMillis(). The three methods you describe will mark the time the same way, the difference is just what kind of object it is wrapped in. Use the variant that you need in your program.

If you want to measure the difference between two points in time during a program run, useSystem.nanoTime() instead.

To get the time difference between two points in time in two separate program runs you will have to rely on external time sources if you are afraid that the user might play around with the system clock. For example, you can take a look at Java SNTP Client.

该回答说的是:其实System.currentTimeMillis() 与 new Date() 与 Calendar.getInstance().getTime()这三个方法得到的是同一个时间,唯一的区别就是他们被不同的包装器包装。
如果你想计算两个时间点的间隔,推荐采用System.nanoTime()。

官方文档:

public static long currentTimeMillis()

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

See the description of the class Date for a discussion of slight discrepancies that may arise between “computer time” and coordinated universal time (UTC).
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

返回的是当前服务器从1970.01.01 00:00:00(UTC) 开始经过的毫秒级时间,并且有些系统的最小时间单位为几十毫秒,精度会有不同。

public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
而new Date()是分配一个Date对象并以当前的自标准基准时间(称为“时期”)以来的指定毫秒数,即1970年1月1日00:00:00 GMT将其初始化,默认初始参数是System.currentTimeMillis()。getTime()返回的是当前服务器从1970.01.01 00:00:00(GMT) 开始经过的毫秒级时间。因为它与System.currentTimeMillis()计算时间的标准会有所差异,可能会存在一点细微差别,即在UTC中,每年或每两年大约有一次,称为“闰秒”。

public static Calendar getInstance()

Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.
Returns:
a Calendar.
使用默认时区和区域设置获取日历。将 Calendar基于与默认语言环境的时区当前时间返回。

public long getTimeInMillis()

Returns this Calendar’s time value in milliseconds.
Returns:
the current time as UTC milliseconds from the epoch.
返回的是当前日历从1970年1月1日00:00:00开始的UTC毫秒数。

拓展:(如果要计算两个时间点的时间差,通过以下api比 System.currentTimeMillis() 的精度要高(纳秒级),如果不高,则不需要)
public static long nanoTime()

Returns the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not correctly compute elapsed time due to numerical overflow.

The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.

For example, to measure how long some code takes to execute:

long startTime = System.nanoTime();
// … the code being measured …
long estimatedTime = System.nanoTime() - startTime;
To compare two nanoTime values

long t0 = System.nanoTime();

long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
Returns:
the current value of the running Java Virtual Machine’s high-resolution time source, in nanoseconds
Since:
1.5

总结:

JAVA中 System.currentTimeMillis() 与 new Date().getTime() 与 Calendar.getInstance().getTime() 3个API的结果是一致的,除了计时方式UTC、GMT,和包装器以外,其余没啥区别。

测试:

        long curTime2 = System.nanoTime();
        System.out.println("Calendar.getInstance().getTimeInMillis()    "+Calendar.getInstance().getTimeInMillis()+" 耗时(ns):"+ (System.nanoTime()-curTime2));
        long curTime = System.nanoTime();
        System.out.println("System.currentTimeMillis()  "+System.currentTimeMillis()+" 耗时(ns):"+ (System.nanoTime()-curTime));
        long curTime1 = System.nanoTime();
        System.out.println("new Date().getTime()    "+new Date().getTime()+" 耗时(ns):"+ (System.nanoTime()-curTime1));

结果:

Calendar.getInstance().getTimeInMillis()1563182388493    创建耗时(ns):26785903
System.currentTimeMillis()              1563182388507    创建耗时(ns):9439
new Date().getTime()                    1563182388507    创建耗时(ns):255603

结果的差异应是创建的耗时原因,api的对象创建耗时时间如上所示。从以上数据可以看出System.currentTimeMillis() 耗时很少,几乎不用创建对象,所以比较快。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值