Java instant 倒退 100 年

想在 Java 中实现一个当前的 instant 倒退回 100 年。

Google AI 给出的答案是:

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.now();
        Instant hundredYearsAgo = now.minus(100, ChronoUnit.YEARS);

        System.out.println("Now: " + now);
        System.out.println("100 years ago: " + hundredYearsAgo);
    }
}

显然上面的代码没错,但运行会有异常。

异常为:

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
	at java.base/java.time.Instant.plus(Instant.java:861)
	at java.base/java.time.Instant.minus(Instant.java:978)
	at edu.nec.course.week5.inclass.formattedPrint.PrintfSample.main(PrintfSample.java:12)

at java.base/java.time.Instant.plus(Instant.java:861) Unsupported unit: Years

It seems like you’re encountering a Java error: Unsupported unit: Years. This error occurs because the Instant class in Java does not support adding units like years or months directly, since these units can vary in length (e.g., leap years, months with different number of days).

To handle time units that aren’t supported directly by Instant (like years, months, weeks), you should use the LocalDateTime or ZonedDateTime classes, which are capable of handling these larger units. Here’s how you can manage adding years using these classes:

Example with LocalDateTime

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusYears(1); // Adding one year
        System.out.println("Date after adding a year: " + newDateTime);
    }
}

Example with ZonedDateTime

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        ZonedDateTime newZonedDateTime = zonedDateTime.plusYears(1); // Adding one year
        System.out.println("Date after adding a year: " + newZonedDateTime);
    }
}

These examples should help you add larger time units (like years) to your dates/times effectively without running into the unsupported unit issue you encountered with Instant.

显然 GPT 的回答更好。

确实是在 Java Instant 不支持 Year 的计算。

只能使用 LocalDateTime 来进行计算。

也可以考虑使用下面的代码:

System.out.println(ZonedDateTime.now().minusYears(5).toInstant());
		System.out.println(ZonedDateTime.now(ZoneOffset.UTC).minusYears(5).toInstant());

来进行计算。

上面的 2 行代码没有问题。

如果你对时间不敏感的话,也可以使用下面的代码:

		LocalDate date = LocalDate.now();
		date = date.plus(-5, ChronoUnit.YEARS);

		System.out.println(date);

下面我们也用同样的问题测了下 claude_3_haiku 的这个模型。

感觉 claude_3_haiku 分反馈不如 GPT。

有可能在程序上 claude_3_haiku 模型相对更差。

at java.base/java.time.Instant.plus(Instant.java:861) Unsupported unit: Years

1 Like

Okay, let me see if I can find some relevant information about that error message:

Search

Found 0 results for 'java.time.Instant.plus() Unsupported unit: Years status:public'

Hmm, it seems there are no direct search results related to the error message “Unsupported unit: Years” when using the plus() method on java.time.Instant. Let me try to provide some more context on this issue:

The Instant class in Java represents a specific point in time, without any associated time zone information. The plus() method on Instant allows you to add a duration to the instant, but it only supports certain time units like seconds, milliseconds, etc. Adding a duration in “Years” is not supported, as the Instant class does not have the concept of years built-in.

To add a duration in years to an Instant, you would need to use the Instant#plus(long, TemporalUnit) method and pass in the ChronoUnit.YEARS as the time unit, like this:

Instant now = Instant.now();
Instant oneYearLater = now.plus(1, ChronoUnit.YEARS);

The error message you encountered is likely due to trying to use the simpler Instant#plus(long) method and passing in a duration in years, which is not supported. You’ll need to use the version of the plus() method that takes a TemporalUnit to add a duration in years.

Let me know if this helps explain the issue or if you have any other questions!

1 Like

Java 有关时间的很多计算本身就有很多坑。