I have an external API return me dates as long
s, represented as milliseconds since Epoch.
我有一个外部API返回我的日期为长,表示自Epoch以来的毫秒。
With the old style Java API, I would simply construct a Date
from it with
使用旧式Java API,我只需用它构造一个Date
Date myDate = new Date(startDateLong)
日期myDate =新日期(startDateLong)
What is the equivalent in Java 8's LocalDate
/LocalDateTime
classes?
Java 8的LocalDate / LocalDateTime类中的等价物是什么?
I am interested in converting the point in time represented by the long to a LocalDate
in my current local timezone.
我有兴趣将long表示的时间点转换为当前本地时区的LocalDate。
4 个解决方案
#1
189
If you have the milliseconds since the Epoch and want convert them to a local date using the current local timezone, you can use
如果您有自Epoch以来的毫秒数并希望使用当前本地时区将它们转换为本地日期,则可以使用
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 long
value may produce different result in subsequent runs, even on the same machine.
但请记住,即使系统的默认时区可能会发生变化,因此相同的长值可能会在后续运行中产生不同的结果,即使在同一台机器上也是如此。
Further, keep in mind that LocalDate
, unlike java.util.Date
, really represents a date, not a date and time.
此外,请记住,与java.util.Date不同,LocalDate实际上代表的是日期,而不是日期和时间。
Otherwise, you may use a LocalDateTime
:
否则,您可以使用LocalDateTime:
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());
#2
18
You can start with Instant.ofEpochMilli(long):
你可以从Instant.ofEpochMilli(长)开始:
LocalDate date =
Instant.ofEpochMilli(startDateLong)
.atZone(ZoneId.systemDefault())
.toLocalDate();
#3
2
Timezones and stuff aside, a very simple alternative to new Date(startDateLong)
could be LocalDate.ofEpochDay(startDateLong / 86400000L)
除了时区和东西,新Date(startDateLong)的一个非常简单的替代方案可能是LocalDate.ofEpochDay(startDateLong / 86400000L)
#4
2
I think I have a better answer.
我想我有更好的答案。
new Timestamp(longEpochTime).toLocalDateTime();
#1
189
If you have the milliseconds since the Epoch and want convert them to a local date using the current local timezone, you can use
如果您有自Epoch以来的毫秒数并希望使用当前本地时区将它们转换为本地日期,则可以使用
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 long
value may produce different result in subsequent runs, even on the same machine.
但请记住,即使系统的默认时区可能会发生变化,因此相同的长值可能会在后续运行中产生不同的结果,即使在同一台机器上也是如此。
Further, keep in mind that LocalDate
, unlike java.util.Date
, really represents a date, not a date and time.
此外,请记住,与java.util.Date不同,LocalDate实际上代表的是日期,而不是日期和时间。
Otherwise, you may use a LocalDateTime
:
否则,您可以使用LocalDateTime:
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());
#2
18
You can start with Instant.ofEpochMilli(long):
你可以从Instant.ofEpochMilli(长)开始:
LocalDate date =
Instant.ofEpochMilli(startDateLong)
.atZone(ZoneId.systemDefault())
.toLocalDate();
#3
2
Timezones and stuff aside, a very simple alternative to new Date(startDateLong)
could be LocalDate.ofEpochDay(startDateLong / 86400000L)
除了时区和东西,新Date(startDateLong)的一个非常简单的替代方案可能是LocalDate.ofEpochDay(startDateLong / 86400000L)
#4
2
I think I have a better answer.
我想我有更好的答案。
new Timestamp(longEpochTime).toLocalDateTime();