Instant truncatedTo() method in Java with Examples
The truncatedTo() method of an Instant class is used to get the value of this Instant in the specified unit. This method takes a parameter Unit, which is the Unit in which this Instant is to be truncated to. It returns a truncated immutable Instant with the value in the specified unit.
public Instant truncatedTo(TemporalUnit unit)
Parameter: This method takes a parameter unit which is the unit in which this Instant is to be truncated to. It should not be null.
Returns: This method returns an immutable truncated Instant with the value in the specified unit.
Exception: This method throws following Exceptions:
- DateTimeException: if the unit is invalid for truncation.
- UnsupportedTemporalTypeException: if the unit is not supported.
Below programs illustrate the Instant.truncatedTo() method:
Instant before truncate: 2018-12-30T09:24:54.630Z Instant after truncate: 2018-12-30T09:00:00Z
Instant before truncate: 2018-12-30T09:24:54.630Z Instant after truncate: 2018-12-30T00:00:00Z
Program 3: To show Exception:
Exception: java.time.temporal.UnsupportedTemporalTypeException: Unit is too large to be used for truncation
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.