Instant truncatedTo() method in Java with Examples

29 Nov, 2018
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.
Syntax:
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:
Program 1:
// Java program to demonstrate// Instant.truncatedTo() method  import java.time.*;import java.time.temporal.ChronoUnit;  public class GFG {    public static void main(String[] args)    {          // create a Instant object        Instant instant            = Instant.parse("2018-12-30T09:24:54.63Z");          // print instance        System.out.println("Instant before"                           + " truncate: "                           + instant);          // truncate to ChronoUnit.HOURS        // means unit smaller than Hour        // will be Zero        Instant returnvalue            = instant.truncatedTo(ChronoUnit.HOURS);          // print result        System.out.println("Instant after "                           + " truncate: "                           + returnvalue);    }}
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T09:00:00Z

Program 2:
// Java program to demonstrate// Instant.truncatedTo() method  import java.time.*;import java.time.temporal.ChronoUnit;  public class GFG {    public static void main(String[] args)    {          // create a Instant object        Instant instant            = Instant.parse("2018-12-30T09:24:54.63Z");          // print instance        System.out.println("Instant before"                           + " truncate: "                           + instant);          // truncate to ChronoUnit.DAYS        // means unit smaller than DAY        // will be Zero        Instant returnvalue            = instant.truncatedTo(ChronoUnit.DAYS);          // print result        System.out.println("Instant after "                           + " truncate: "                           + returnvalue);    }}
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T00:00:00Z

Program 3: To show Exception:
// Java program to demonstrate// Instant.truncatedTo() method  import java.time.*;import java.time.temporal.ChronoUnit;  public class GFG {    public static void main(String[] args)    {          // create a Instant object        Instant instant            = Instant.parse("2018-12-30T09:24:54.63Z");          try {            instant.truncatedTo(ChronoUnit.ERAS);        }        catch (Exception e) {              // print result            System.out.println("Exception: " + e);        }    }}
Exception:
 java.time.temporal.UnsupportedTemporalTypeException:
 Unit is too large to be used for truncation

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#truncatedTo(java.time.temporal.TemporalUnit)
Java-Functions
Java-Instant
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.