1. Overview
1.概述
In this tutorial, we’ll demonstrate the conversion from Java’s LocalDate to Epoch and vice versa.
在本教程中,我们将演示从 Java 的 LocalDate 到 Epoch 的转换,反之亦然 。
2. Epoch vs. LocalDate
2. 时间与本地日期
To do the conversion, it is important to understand the concept behind Epoch and LocalDate. The ‘Epoch‘ in Java refers to the time instant of 1970-01-01T00:00:00Z. Time instants after the Epoch will have positive values. Similarly, any time instants before the Epoch will have negative values.
要进行转换,了解 Epoch 和 LocalDate 背后的概念非常重要。Java 中的”Epoch“指的是 1970-01-01T00:00:00Z 的时间瞬间。在 “纪元 “之后的时间瞬时将具有正值。同样,在 “纪元 “之前的任何时间瞬时值将为负值。
All instances of Epoch, LocalDate, and LocalDateTime are timezone-dependent, hence, when converting from one to another, we have to know the timezone. In Java, a timezone can be represented through the ZoneId class. ZoneId can be the system’s timezone obtained through the method ZoneId.systemDefault(). Alternatively, ZoneId can be computed by passing a String for a known timezone such as Europe/Amsterdam.
Epoch、LocalDate 的所有实例、和 LocalDateTime 都与时区有关, 因此,当从一个时区转换到另一个时区时,我们必须知道时区。在 Java 中,时区可以通过 ZoneId 类来表示。ZoneId 可以是通过方法 ZoneId.systemDefault() 获得的系统时区。或者、ZoneId 可以通过传递 String 的已知时区(如 Europe/Amsterdam )来计算。</em
3. Epoch to Date/Time
3.从纪元到日期/时间
We can compute LocalDate or LocalDateTime given the number of milliseconds since the epoch. Alternatively, the count can be in seconds or seconds with nanoseconds adjustment. The Java data type for this count is Long. Finally, we also need to know the timezone. Let’s see how to do the conversion:
我们可以计算 LocalDate 或 LocalDateTime 从纪元开始的毫秒数。或者,计数单位可以是秒 或 秒(带纳秒调整)。该计数的 Java 数据类型是长。最后,我们还需要知道时区。让我们看看如何进行转换:</span
long milliSecondsSinceEpoch = 2131242L;
ZoneId zoneId = ZoneId.of("Europe/Amsterdam");
LocalDate date = Instant.ofEpochMilli(milliSecondsSinceEpoch).atZone(zoneId).toLocalDate();
In the above code snippet, we have the number of milliseconds since the Epoch in the timezone of Amsterdam, so we can use the ofEpochMilli() method of class Instant to get the LocalDate value. Otherwise, if we want to have the time instead of the date, then we’d write:
在上述代码片段中,我们获得了自阿姆斯特丹时区的Epoch起的毫秒数,因此我们可以使用类Instant的ofEpochMilli()方法来获取LocalDate值。否则,如果我们想要的是时间而不是日期,那么我们可以这样写:
LocalDateTime time = Instant.ofEpochMilli(milliSecondsSinceEpoch).atZone(zoneId).toLocalDateTime();
In the above code snippet, we used the same approach but used the toLocalDateTime method.
在上述代码片段中,我们使用了相同的方法,但使用了 toLocalDateTime 方法。
4. Date/Time to Epoch
4.日期/时间到纪元
If we have a date available as LocalDate in a given timezone, then we can get the Epoch in seconds. Let’s see how:
如果我们有一个特定时区的LocalDate日期,那么我们就可以得到以秒为单位的纪元。让我们看看如何操作:</span
ZoneId zoneId = ZoneId.of("Europe/Tallinn");
LocalDate date = LocalDate.now();
long EpochMilliSecondsAtDate = date.atStartOfDay(zoneId).toInstant().toEpochMilli();
In the above example, we get the Epoch seconds for today’s date and the timezone in which the system is currently in. Note that we can only get the Epoch count for the start of the day. This is because LocalDate does not have any time value information. Alternatively, if we do have the time component, we can get the exact Epoch count at a given instant:
在上面的示例中,我们将获得今天日期和系统当前所在时区的 Epoch 秒数。注意,我们只能获得一天开始时的 Epoch 计数 。这是因为 LocalDate 没有任何时间值信息。或者,如果我们确实有时间组件,我们就可以在给定的瞬间获得精确的 Epoch 计数:</span
LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
long epochMilliSecondsAtTime = localDateTime.atZone(zoneId).toInstant().toEpochMilli();
5. Conclusion
5.结论
In this article, we explored how to convert from Epoch to LocalDate and LocalDateTime. We also demonstrated how to convert LocalDate or LocalDateTime to Epoch.
在本文中,我们探讨了如何将Epoch转换为LocalDate和LocalDateTime。我们还演示了如何将 LocalDate 或 LocalDateTime 转换为 Epoch 。
As always, we can find the complete code over on GitHub.