如何在Java中从时代(1970-01-01)获得毫秒? [英] How do I get milliseconds from epoch (1970-01-01) in Java?

查看:955
本文介绍了如何在Java中从时代(1970-01-01)获得毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我也希望得到数字,从1970-01-01 UTC到任何其他UTC日期时间的毫秒数。

解决方案

System.currentTimeMillis()



从JavaDoc:



返回: strong> 当前时间和1970年1月1日午夜之间的差异,以毫秒为单位测量UTC



Java 8 介绍了 java .time 框架,特别是 即时 类,其中 ...模拟...点e时间线...

  long now = Instant.now()toEpochMilli(); 

返回: 自1970年代以来的毫秒数-01-01T00:00:00Z - 即与上述几乎一样: - )



干杯,


I need to get the number of milliseconds from 1970-01-01 UTC until now UTC in Java.

I would also like to be able to get the number of milliseconds from 1970-01-01 UTC to any other UTC date time.

解决方案

How about System.currentTimeMillis()?

From the JavaDoc:

Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC

Java 8 introduces the java.time framework, particularly the Instant class which "...models a ... point on the time-line...":

long now = Instant.now().toEpochMilli();

Returns: the number of milliseconds since the epoch of 1970-01-01T00:00:00Z -- i.e. pretty much the same as above :-)

Cheers,

这篇关于如何在Java中从时代(1970-01-01)获得毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆