How to use the Java 8 Duration.of(amount, unit) method?

982 Views Asked by At

I tried this: Duration.of(3, SECONDS) as per the example, but what is the SECONDS constant coming from? It's not evaluating to anything.

2

There are 2 best solutions below

5
On BEST ANSWER

It's a value from the ChronoUnit enum, implementing TemporalUnit - your code can be written as:

Duration.of(3, ChronoUnit.SECONDS);

I'm not sure what you mean by "it's not evaluating to anything" - it's specifying what units you want to use.

Presumably you're using it without explicitly specifying ChronoUnit due to a staic import.

Personally I'd use Duration.ofSeconds(3) though, as I find that clearer.

0
On

The SECONDS constant, along with the other time constants, come from the ChronoUnit enum.