Duration get(TemporalUnit) method in Java with Examples
The get(TemporalUnit) method of Duration Class in java.time package is used to get the value of the unit passed as the parameter. Only units SECONDS and NANOS ate supported by this method and the rest result in exception.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public long get(TemporalUnit unit)
Parameters: This method accepts a parameter unit which is the TemporalUnit for which the value is to be fetched.
Return Value: This method returns a long value of the unit passed as the parameter.
Exception: This method throws:
- DateTimeException: if the unit is not supported.
- UnsupportedTemporalTypeException: if the unit is not supported.
Below examples illustrate the Duration.get() method:
Seconds: 183840 Nanos: 0
Exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Minutes
Reference: Oracle Doc