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:
// 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); } } |
LocalTime before truncate: 21:45:36.130 LocalTime after truncate: 21:45
Program 2:
// 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); } } |
LocalTime before truncate: 01:21:30.130 LocalTime after truncate: 01:00
Recommended Posts:
- Instant truncatedTo() method in Java with Examples
- ZonedDateTime truncatedTo() method in Java with Examples
- Duration truncatedTo(TemporalUnit) method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime with() Method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime getNano() method in Java with Examples
- LocalTime ofSecondOfDay() method in Java with Examples
- LocalTime minusHours() method in Java with Examples
- LocalTime minusNanos() method in Java with Examples
- LocalTime minus() method in Java with Examples
- LocalTime parse() method in Java with Examples
- LocalTime minusMinutes() method in Java with Examples
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.


