LocalTime truncatedTo() method in Java with Examples

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

Syntax:

public LocalTime truncatedTo(TemporalUnit unit)

Parameters: This method accepts a single parameter unit which represents the unit to truncate to, It should not be null.



Return value: This method returns a immutable truncated LocalTime based on this time with the time truncated, not null.

Exception: This method throws following two exception:

  • 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
// LocalTime.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("21:45:36.13");
  
        // print instance
        System.out.println("LocalTime before"
                           + " truncate: "
                           + time);
  
        // truncate to ChronoUnit.MINUTES
        // means unit smaller than Minute
        // will be Zero
        LocalTime returnvalue
            = time.truncatedTo(ChronoUnit.MINUTES);
  
        // print result
        System.out.println("LocalTime after "
                           + " truncate: "
                           + returnvalue);
    }
}

chevron_right


Output:

LocalTime before truncate: 21:45:36.130
LocalTime after  truncate: 21:45

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalTime.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("01:21:30.13");
  
        // print instance
        System.out.println("LocalTime before"
                           + " truncate: "
                           + time);
  
        // truncate to ChronoUnit.HOURS
        // means unit smaller than Hour
        // will be Zero
        LocalTime returnvalue
            = time.truncatedTo(ChronoUnit.HOURS);
  
        // print result
        System.out.println("LocalTime after "
                           + " truncate: "
                           + returnvalue);
    }
}

chevron_right


Output:

LocalTime before truncate: 01:21:30.130
LocalTime after  truncate: 01:00

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#truncatedTo(java.time.temporal.TemporalUnit)




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.