如何在Java 8中从LocalDateTime获得毫秒

时间:2022-10-11 12:02:12

I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate, LocalTime or LocalDateTime classes of Java 8.

我想知道是否有一种方法可以使用Java 8的新LocalDate、LocalTime或LocalDateTime类从1-1970 (epoch)开始获取当前毫秒数。

The known way is below:

已知的方法如下:

long currentMilliseconds = new Date().getTime();

or

long currentMilliseconds = System.currentTimeMillis();

5 个解决方案

#1


190  

I'm not entirely sure what you mean by "current milliseconds" but I'll assume it's the number of milliseconds since the "epoch," namely midnight, January 1, 1970 UTC.

我不完全确定你所说的“当前毫秒”是什么意思,但我假设它是自“纪元”(即1970年1月1日零时)以来的毫秒数。

If you want to find the number of milliseconds since the epoch right now, then use System.currentTimeMillis() as Anubian Noob has pointed out. If so, there's no reason to use any of the new java.time APIs to do this.

如果您想要找到从现在开始的毫秒数,那么使用System.currentTimeMillis(),正如Anubian Noob所指出的。如果是这样,就没有理由使用任何新的java。时间api可以做到这一点。

However, maybe you already have a LocalDateTime or similar object from somewhere and you want to convert it to milliseconds since the epoch. It's not possible to do that directly, since the LocalDateTime family of objects has no notion of what time zone they're in. Thus time zone information needs to be supplied to find the time relative to the epoch, which is in UTC.

但是,您可能已经有了一个LocalDateTime或类似的对象,并且您想要将它转换为纪元后的毫秒。直接这样做是不可能的,因为LocalDateTime对象族不知道它们在哪个时区。因此,需要提供时区信息来查找与UTC语言中的历元相关的时间。

Suppose you have a LocalDateTime like this:

假设您有一个LocalDateTime:

LocalDateTime ldt = LocalDateTime.of(2014, 5, 29, 18, 41, 16);

You need to apply the time zone information, giving a ZonedDateTime. I'm in the same time zone as Los Angeles, so I'd do something like this:

您需要应用时区信息,给出一个ZonedDateTime。我和洛杉矶在同一个时区,所以我会这样做:

ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/Los_Angeles"));

Of course, this makes assumptions about the time zone. And there are edge cases that can occur, for example, if the local time happens to name a time near the Daylight Saving Time (Summer Time) transition. Let's set these aside, but you should be aware that these cases exist.

当然,这是对时区的假设。还有一些边缘情况可能会发生,例如,如果本地时间碰巧指定了夏时制转换附近的时间。让我们把这些放在一边,但是你应该知道这些情况是存在的。

Anyway, if you can get a valid ZonedDateTime, you can convert this to the number of milliseconds since the epoch, like so:

无论如何,如果你能得到一个有效的ZonedDateTime,你可以将它转换为自纪元以来的毫秒数,比如:

long millis = zdt.toInstant().toEpochMilli();

#2


57  

What I do so I don't specify a time zone is,

我不指定时区,

System.out.println("ldt " + LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("ctm " + System.currentTimeMillis());

gives

给了

ldt 1424812121078 
ctm 1424812121281

Just in case you don't like System.current...., use Instant.now().toEpochMilli()

以防你不喜欢System.current ....,使用Instant.now().toEpochMilli()

#3


13  

To avoid ZoneId you can do:

你可以这样做:

LocalDateTime date = LocalDateTime.of(1970, 1, 1, 0, 0);

System.out.println("Initial Epoch (TimeInMillis): " + date.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli());

Getting 0 as value, that's right!

得到0作为值,没错!

#4


5  

To get the current time in milliseconds (since the epoch), use System.currentTimeMillis().

要获取当前时间(自纪元以来的毫秒数),请使用System.currentTimeMillis()。

#5


2  

Since Java 8 you can use

因为Java 8可以使用

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

which gives you the same results as

结果是什么

final long currentTimeJava1 = System.currentTimeMillis();

#1


190  

I'm not entirely sure what you mean by "current milliseconds" but I'll assume it's the number of milliseconds since the "epoch," namely midnight, January 1, 1970 UTC.

我不完全确定你所说的“当前毫秒”是什么意思,但我假设它是自“纪元”(即1970年1月1日零时)以来的毫秒数。

If you want to find the number of milliseconds since the epoch right now, then use System.currentTimeMillis() as Anubian Noob has pointed out. If so, there's no reason to use any of the new java.time APIs to do this.

如果您想要找到从现在开始的毫秒数,那么使用System.currentTimeMillis(),正如Anubian Noob所指出的。如果是这样,就没有理由使用任何新的java。时间api可以做到这一点。

However, maybe you already have a LocalDateTime or similar object from somewhere and you want to convert it to milliseconds since the epoch. It's not possible to do that directly, since the LocalDateTime family of objects has no notion of what time zone they're in. Thus time zone information needs to be supplied to find the time relative to the epoch, which is in UTC.

但是,您可能已经有了一个LocalDateTime或类似的对象,并且您想要将它转换为纪元后的毫秒。直接这样做是不可能的,因为LocalDateTime对象族不知道它们在哪个时区。因此,需要提供时区信息来查找与UTC语言中的历元相关的时间。

Suppose you have a LocalDateTime like this:

假设您有一个LocalDateTime:

LocalDateTime ldt = LocalDateTime.of(2014, 5, 29, 18, 41, 16);

You need to apply the time zone information, giving a ZonedDateTime. I'm in the same time zone as Los Angeles, so I'd do something like this:

您需要应用时区信息,给出一个ZonedDateTime。我和洛杉矶在同一个时区,所以我会这样做:

ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/Los_Angeles"));

Of course, this makes assumptions about the time zone. And there are edge cases that can occur, for example, if the local time happens to name a time near the Daylight Saving Time (Summer Time) transition. Let's set these aside, but you should be aware that these cases exist.

当然,这是对时区的假设。还有一些边缘情况可能会发生,例如,如果本地时间碰巧指定了夏时制转换附近的时间。让我们把这些放在一边,但是你应该知道这些情况是存在的。

Anyway, if you can get a valid ZonedDateTime, you can convert this to the number of milliseconds since the epoch, like so:

无论如何,如果你能得到一个有效的ZonedDateTime,你可以将它转换为自纪元以来的毫秒数,比如:

long millis = zdt.toInstant().toEpochMilli();

#2


57  

What I do so I don't specify a time zone is,

我不指定时区,

System.out.println("ldt " + LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("ctm " + System.currentTimeMillis());

gives

给了

ldt 1424812121078 
ctm 1424812121281

Just in case you don't like System.current...., use Instant.now().toEpochMilli()

以防你不喜欢System.current ....,使用Instant.now().toEpochMilli()

#3


13  

To avoid ZoneId you can do:

你可以这样做:

LocalDateTime date = LocalDateTime.of(1970, 1, 1, 0, 0);

System.out.println("Initial Epoch (TimeInMillis): " + date.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli());

Getting 0 as value, that's right!

得到0作为值,没错!

#4


5  

To get the current time in milliseconds (since the epoch), use System.currentTimeMillis().

要获取当前时间(自纪元以来的毫秒数),请使用System.currentTimeMillis()。

#5


2  

Since Java 8 you can use

因为Java 8可以使用

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

which gives you the same results as

结果是什么

final long currentTimeJava1 = System.currentTimeMillis();