Duration truncatedTo(TemporalUnit) method in Java with Examples
The truncatedTo(Temporal) method of Duration Class in java.time package is used get the value of this duration in the specified unit.
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 Duration truncatedTo(TemporalUnit unit)
Parameters: This method accepts a parameter unit which is the unit to which this duration value is to be converted to.
Return Value: This method returns a duration with the value truncated to the specified unit.
Exception: This method throws:
- DateTimeException: if the unit is invalid.
- UnsupportedTemporalTypeException: if the unit is not supported.
Below examples illustrate the Duration.truncatedTo() method:
Original duration: PT51H4M PT51H4M
Original duration: PT120H PT120H