Duration.get(TemporalUnit) supports only ChronoUnit.SECONDS and ChronoUnit.NANOS. Passing any other
TemporalUnit value causes an UnsupportedTemporalTypeException at runtime.
In the java.time API, a Duration represents an amount of time modeled as a number of seconds and nanoseconds. Although
Duration implements the TemporalAmount interface, its get(TemporalUnit) method only supports
ChronoUnit.SECONDS and ChronoUnit.NANOS.
Unlike dedicated conversion methods, Duration.get(TemporalUnit) does not perform unit conversions. Passing any unit other than
SECONDS or NANOS, such as ChronoUnit.MINUTES or ChronoUnit.MILLIS, unconditionally throws an
UnsupportedTemporalTypeException at runtime.
This rule reports only direct enum constants implementing TemporalUnit. It does not track a constant stored in a variable or returned
by a method call.
Use dedicated conversion methods such as toMinutes(), toHours(), toDays(), toMillis(), or
toNanos() to obtain the total duration in the desired unit. In Java 9 and later, methods like toMinutesPart(),
toHoursPart(), or toMillisPart() can also be used to extract the corresponding time part. If access to seconds or
nanoseconds is required, pass ChronoUnit.SECONDS or ChronoUnit.NANOS, or call getSeconds() or
getNano().
import java.time.Duration;
import java.time.temporal.ChronoUnit;
void process(Duration timeout) {
long minutes = timeout.get(ChronoUnit.MINUTES); // Noncompliant: "Duration.get()" only supports "SECONDS" and "NANOS"; use dedicated conversion methods instead
long millis = timeout.get(ChronoUnit.MILLIS); // Noncompliant: "Duration.get()" only supports "SECONDS" and "NANOS"; use dedicated conversion methods instead
}
import java.time.Duration;
import java.time.temporal.ChronoUnit;
void process(Duration timeout) {
long minutes = timeout.toMinutes();
long millis = timeout.toMillis();
long seconds = timeout.get(ChronoUnit.SECONDS);
}
Duration.get(TemporalUnit)Duration.getUnits()