Java

Java

Made by DeepSource

Instant should not be passed unsupported temporal unit types JAVA-E1094

Bug risk
Major

java.time.Instant methods like plus(), minus() and until() should not be passed invalid temporal units in their second argument, as this could cause a crash.

Only pass one of NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS and DAYS as the second argument to these methods.

These three methods of Instant only accept time units at a maximum scale of ChronoUnit.DAYS, meaning that any units other than that will be rejected with an exception.

Bad Practice

Instant instant = Instant.now();
instant.plus(1, ChronoUnit.WEEKS); // You can't add weeks to an instant!

This code would trigger an UnsupportedTemporalTypeException at runtime.

Recommended

Use only ChronoUnit.DAYS and below to perform operations on Instant objects.

Instant instant = Instant.now();
instant.plus(1, ChronoUnit.HALF_DAYS);

References