Saturday, November 22, 2025
HomeLanguagesJavaLocalTime truncatedTo() method in Java with Examples

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


Output:

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


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)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS