I need to get the number of milliseconds from 1970-01-01 UTC until now UTC in Java.
我需要从1970-01-01 UTC到现在的UTC中获得UTC的毫秒数。
I would also like to be able to get the number of milliseconds from 1970-01-01 UTC to any other UTC date time.
我还希望能够获得从1970-01-01 UTC到任何其他UTC日期时间的毫秒数。
3 个解决方案
#1
106
How about System.currentTimeMillis()
?
System.currentTimeMillis()怎么样?
From the JavaDoc:
来自JavaDoc:
Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC
返回:当前时间与UTC时间1970年1月1日午夜之间的差异(以毫秒为单位)
Java 8 introduces the java.time
framework, particularly the Instant
class which "...models a ... point on the time-line...":
Java 8引入了java.time框架,特别是“......在时间线上模拟...点......”的Instant类:
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 :-)
返回:自1970-01-01T00:00:00Z时代以来的毫秒数 - 即与上述几乎相同:-)
Cheers,
干杯,
#2
30
java.time
Using the java.time
framework built into Java 8 and later.
使用Java 8及更高版本中内置的java.time框架。
import java.time.Instant;
Instant.now().toEpochMilli(); //Long = 1450879900184
Instant.now().getEpochSecond(); //Long = 1450879900
This works in UTC because Instant.now()
is really call to Clock.systemUTC().instant()
这在UTC中有效,因为Instant.now()实际上是对Clock.systemUTC()的调用。
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
#3
11
Also try System.currentTimeMillis()
也试试System.currentTimeMillis()
#1
106
How about System.currentTimeMillis()
?
System.currentTimeMillis()怎么样?
From the JavaDoc:
来自JavaDoc:
Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC
返回:当前时间与UTC时间1970年1月1日午夜之间的差异(以毫秒为单位)
Java 8 introduces the java.time
framework, particularly the Instant
class which "...models a ... point on the time-line...":
Java 8引入了java.time框架,特别是“......在时间线上模拟...点......”的Instant类:
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 :-)
返回:自1970-01-01T00:00:00Z时代以来的毫秒数 - 即与上述几乎相同:-)
Cheers,
干杯,
#2
30
java.time
Using the java.time
framework built into Java 8 and later.
使用Java 8及更高版本中内置的java.time框架。
import java.time.Instant;
Instant.now().toEpochMilli(); //Long = 1450879900184
Instant.now().getEpochSecond(); //Long = 1450879900
This works in UTC because Instant.now()
is really call to Clock.systemUTC().instant()
这在UTC中有效,因为Instant.now()实际上是对Clock.systemUTC()的调用。
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
#3
11
Also try System.currentTimeMillis()
也试试System.currentTimeMillis()