ZonedDateTime truncatedTo() method in Java with Examples

The truncatedTo() method of a ZonedDateTime class is used to return the value of this ZonedDateTime in the specified unit. This method takes a parameter Unit, which is the Unit in which this ZonedDateTime is to be truncated to. It returns a truncated immutable ZonedDateTime with the value in the specified unit.

Syntax:

public ZonedDateTime truncatedTo(TemporalUnit unit)

Parameters: This method accepts one single parameter unit which is the unit in which this ZonedDateTime is to be truncated to. It should not be null.

Return value: This method returns a immutable truncated ZonedDateTime with the value in the specified unit.

Exception: This method throws following Exceptions:



  • DateTimeException: if unable to truncate.
  • UnsupportedTemporalTypeException: if the unit is not supported.

Below programs illustrate the truncatedTo() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ZonedDateTime.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime
                  .parse(
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // print ZonedDateTime
        System.out.println("ZonedDateTime before"
                           + " truncate: "
                           + zonedDT);
  
        // truncate to ChronoUnit.HOURS
        // means unit smaller than Hour
        // will be Zero
        ZonedDateTime returnvalue
            = zonedDT.truncatedTo(ChronoUnit.HOURS);
  
        // print result
        System.out.println("ZonedDateTime after "
                           + " truncate: "
                           + returnvalue);
    }
}

chevron_right


Output:

ZonedDateTime before truncate: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after  truncate: 2018-12-06T19:00+05:30[Asia/Calcutta]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ZonedDateTime.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime
                  .parse(
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // print ZonedDateTime
        System.out.println("ZonedDateTime before"
                           + " truncate: "
                           + zonedDT);
  
        // truncate to ChronoUnit.DAYS
        // means unit smaller than DAY
        // will be Zero
        ZonedDateTime returnvalue
            = zonedDT.truncatedTo(ChronoUnit.DAYS);
  
        // print result
        System.out.println("ZonedDateTime after "
                           + " truncate: "
                           + returnvalue);
    }
}

chevron_right


Output:

ZonedDateTime before truncate: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after  truncate: 2018-12-06T00:00+05:30[Asia/Calcutta]

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#withZoneSameInstant(java.time.ZoneId)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections 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.