I have two instances of the Instant
class from java.time
such as this:
我有两个来自java.time的Instant类实例,例如:
Instant instant1 = Instant.now();
Instant instant2 = Instant.now().plus(5, ChronoUnit.HOURS);
Now I would like to check if the two instances of Instant
are actually on the same date (day, month and year match). Easy I thought, let's just use the shiny new LocalDate
and the universal from
static method:
现在我想检查一下Instant的两个实例是否实际上在同一天(日,月和年匹配)。我想,让我们只使用闪亮的新LocalDate和静态方法:
LocalDate localdate1 = LocalDate.from(instant1);
LocalDate localdate2 = LocalDate.from(instant2);
if (localdate1.equals(localdate2)) {
// All the awesome
}
Except that universal from
method is not so universal and Java complains at runtime with an exception:
除了来自方法的universal不是那么普遍,Java在运行时抱怨异常:
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 2014-11-04T18:18:12Z of type java.time.Instant
Which leaves me back at square 1.
这让我回到了1号广场。
What is the recommended/fastest way check if two instances of Instant
actually have the same date (have the same day, month, and year)?
如果两个Instant实例具有相同的日期(具有相同的日,月和年),建议/最快的方法是什么?
1 个解决方案
#1
11
The Instant class does not work with human units of time, such as years, months, or days. If you want to perform calculations in those units, you can convert an Instant to another class, such as LocalDateTime or ZonedDateTime, by binding the Instant with a time zone. You can then access the value in the desired units.
Instant类不适用于人类时间单位,例如年,月或日。如果要以这些单位执行计算,可以通过将Instant与时区绑定,将Instant转换为另一个类,例如LocalDateTime或ZonedDateTime。然后,您可以按所需的单位访问该值。
http://docs.oracle.com/javase/tutorial/datetime/iso/instant.html
http://docs.oracle.com/javase/tutorial/datetime/iso/instant.html
Therefore I suggest the following code:
因此我建议使用以下代码:
LocalDate ld1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault()).toLocalDate();
LocalDate ld2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault()).toLocalDate();
if (ld1.isEqual(ld2)) {
System.out.println("blubb");
}
Alternatively you could use
或者你可以使用
instant.atOffset(ZoneOffset.UTC).toLocalDate();
#1
11
The Instant class does not work with human units of time, such as years, months, or days. If you want to perform calculations in those units, you can convert an Instant to another class, such as LocalDateTime or ZonedDateTime, by binding the Instant with a time zone. You can then access the value in the desired units.
Instant类不适用于人类时间单位,例如年,月或日。如果要以这些单位执行计算,可以通过将Instant与时区绑定,将Instant转换为另一个类,例如LocalDateTime或ZonedDateTime。然后,您可以按所需的单位访问该值。
http://docs.oracle.com/javase/tutorial/datetime/iso/instant.html
http://docs.oracle.com/javase/tutorial/datetime/iso/instant.html
Therefore I suggest the following code:
因此我建议使用以下代码:
LocalDate ld1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault()).toLocalDate();
LocalDate ld2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault()).toLocalDate();
if (ld1.isEqual(ld2)) {
System.out.println("blubb");
}
Alternatively you could use
或者你可以使用
instant.atOffset(ZoneOffset.UTC).toLocalDate();