# Java instant 倒退 100 年

**URL:** <https://www.isharkfly.com/t/java-instant-100/16472>\
**Category:** 程序语言\
**Tags:** java\
**Created:** [October 25, 2024, 2:33am UTC](https://www.isharkfly.com/t/java-instant-100/16472 "2024-10-25T02:33:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 25, 2024, 2:33am UTC](https://www.isharkfly.com/t/java-instant-100/16472/1 "2024-10-25T02:33:41Z")

</div>

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

Google AI 给出的答案是：

```auto
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);
    }
}

```

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

 ![2024-10-24_22-32-52](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/d/de30de523152374677d602e6e6e226cd04ac2a82.png)

异常为：

```auto
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)

```

 ![2024-10-24_22-33-28](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/b616cbecc1ca39ef2392dc007f0172e82902ee90.png)

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 25, 2024, 2:36am UTC](https://www.isharkfly.com/t/java-instant-100/16472/2 "2024-10-25T02:36:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gpt4t\_bot](https://www.isharkfly.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gpt4t\_bot](https://www.isharkfly.com/u/gpt4t_bot)\
**Post date:** [October 25, 2024, 2:36am UTC](https://www.isharkfly.com/t/java-instant-100/16472/3 "2024-10-25T02:36:38Z")

</div>

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`

```java
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`

```java
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`.

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 25, 2024, 2:40am UTC](https://www.isharkfly.com/t/java-instant-100/16472/4 "2024-10-25T02:40:56Z")

</div>

显然 GPT 的回答更好。

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

只能使用 LocalDateTime 来进行计算。

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

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

```

来进行计算。

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

 ![2024-10-24_22-43-33](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/1/181076a867bf7c1938af555d88aa466fca156e2e.png)

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

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

		System.out.println(date);

```

 ![2024-10-24_22-44-48](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/1/16f76158d4d8d90d0f5c7f2d0cbe447ee3ee8f68.png)

下面我们也用同样的问题测了下 claude\_3\_haiku 的这个模型。

感觉 claude\_3\_haiku 分反馈不如 GPT。

有可能在程序上 claude\_3\_haiku 模型相对更差。

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 25, 2024, 2:36am UTC](https://www.isharkfly.com/t/java-instant-100/16472/5 "2024-10-25T02:36:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![claude\_3\_haiku\_bot](https://www.isharkfly.com/user_avatar/www.isharkfly.com/claude_3_haiku_bot/32/15774_2.png) [@claude\_3\_haiku\_bot](https://www.isharkfly.com/u/claude_3_haiku_bot)\
**Post date:** [October 25, 2024, 2:36am UTC](https://www.isharkfly.com/t/java-instant-100/16472/6 "2024-10-25T02:36:09Z")

</div>

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

> **Search**
>
> Found 0 [results](https://www.isharkfly.com/search?q=java.time.Instant.plus%28%29+Unsupported+unit%3A+Years+status%3Apublic) 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:

```java
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!

---

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [October 25, 2024, 3:14am UTC](https://www.isharkfly.com/t/java-instant-100/16472/7 "2024-10-25T03:14:00Z")

</div>

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