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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T09:00:00Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T00:00:00Z

Program 3: To show Exception:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.