如何从以毫秒为单位的长纪元时间创建 Java 8 LocalDate?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35183146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:24:28  来源:igfitidea点击:

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

javadatetimejava-8java-time

提问by Vihung

I have an external API that returns me dates as longs, represented as milliseconds since the beginning of the Epoch.

我有一个外部 API,它将日期返回为longs,表示为自纪元开始以来的毫秒数。

With the old style Java API, I would simply construct a Datefrom it with

使用旧式 Java API,我只需Date从它构造一个

Date myDate = new Date(startDateLong)

What is the equivalent in Java 8's LocalDate/LocalDateTimeclasses?

Java 8's LocalDate/ LocalDateTimeclasses 中的等价物是什么?

I am interested in converting the point in time represented by the longto a LocalDatein my current local timezone.

我感兴趣的所代表的时间点转换longLocalDate我目前的本地时区。

采纳答案by Holger

If you have the milliseconds since the Epoch and want to convert them to a local date using the current local timezone, you can use

如果您有自纪元以来的毫秒数并希望使用当前本地时区将它们转换为本地日期,则可以使用

LocalDate date =
    Instant.ofEpochMilli(longValue).atZone(ZoneId.systemDefault()).toLocalDate();

but keep in mind that even the system's default time zone may change, thus the same longvalue may produce different result in subsequent runs, even on the same machine.

但请记住,即使系统的默认时区也可能发生变化,因此long即使在同一台机器上,相同的值在后续运行中也可能产生不同的结果。

Further, keep in mind that LocalDate, unlike java.util.Date, really represents a date, not a date and time.

此外,请记住LocalDate,与 不同的是java.util.Date,它真正代表的是日期,而不是日期和时间。

Otherwise, you may use a LocalDateTime:

否则,您可以使用LocalDateTime

LocalDateTime date =
    LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());

回答by Meno Hochschild

You can start with Instant.ofEpochMilli(long):

你可以从Instant.ofEpochMilli(long) 开始

LocalDate date =
  Instant.ofEpochMilli(startDateLong)
  .atZone(ZoneId.systemDefault())
  .toLocalDate();

回答by Michael Piefel

Timezones and stuff aside, a very simple alternative to new Date(startDateLong)could be LocalDate.ofEpochDay(startDateLong / 86400000L)

除了时区和其他东西,一个非常简单的替代方法new Date(startDateLong)可能是LocalDate.ofEpochDay(startDateLong / 86400000L)

回答by Abhijeet

I think I have a better answer.

我想我有一个更好的答案。

new Timestamp(longEpochTime).toLocalDateTime();

回答by M. Prokhorov

In a specific case where your epoch seconds timestamp comes from SQL or is related to SQL somehow, you can obtain it like this:

在您的纪元秒时间戳来自 SQL 或以某种方式与 SQL 相关的特定情况下,您可以像这样获得它:

long startDateLong = <...>

LocalDate theDate = new java.sql.Date(startDateLong).toLocalDate();

回答by Santhosh Hirekerur

replace now.getTime() with your long value.

用你的长值替换 now.getTime() 。

//GET UTC time for current date
        Date now= new Date();
        //LocalDateTime utcDateTimeForCurrentDateTime = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDateTime();
        LocalDate localDate = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDate();
        DateTimeFormatter dTF2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        System.out.println(" formats as " + dTF2.format(utcDateTimeForCurrentDateTime));