Trong Joda, chúng tôi có thể tính toán năm giữa 2 Datetime sử dụngJava năm giữa 2 khoảnh
Years.between(dateTime1, dateTime2);
Có cách nào dễ dàng để tìm thấy năm giữa 2 khoảnh khắc bằng cách sử dụng java.time
API thay vì không có nhiều logic?
ChronoUnit.YEARS.between(instant1, instant2)
thất bại:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
at java.time.Instant.until(Instant.java:1157)
at java.time.temporal.ChronoUnit.between(ChronoUnit.java:272)
...
Loại 'Instance1' là gì? –
Chỉ cần cập nhật. Nó là java.time.Instant. – user1578872
Bản sao có thể có của [Làm thế nào tôi có thể tìm thấy số năm giữa hai ngày trong android?] (Https://stackoverflow.com/questions/7906301/how-can-i-find-the-number-of-years-between -two-date-in-android) –