Syntax error LocalTime truncatedTo() method in Java

LocalTime truncatedTo() method in Java



An immutable truncated LocalTime object can be obtained using the truncatedTo() method in the LocalTime in Java. This method requires a single parameter i.e. the TemporalUnit till which the LocalTime object is truncated and it returns the immutable truncated object.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
   public static void main(String[] args) {
      LocalTime lt = LocalTime.parse("23:15:30");
      System.out.println("The LocalTime is: " + lt.toString());
      LocalTime truncatedLocalTime = lt.truncatedTo(ChronoUnit.MINUTES);
      System.out.println("The truncated LocalTime is: " + truncatedLocalTime);
   }
}

Output

The LocalTime is: 23:15:30
The truncated LocalTime is: 23:15

Now let us understand the above program.

First the current LocalTime is displayed. Then the immutable truncated LocalTime is obtained using the truncatedTo() method and it is displayed. A code snippet that demonstrates this is as follows −

LocalTime lt = LocalTime.parse("23:15:30");
System.out.println("The LocalTime is: " + lt.toString());
LocalTime truncatedLocalTime = lt.truncatedTo(ChronoUnit.MINUTES);
System.out.println("The truncated LocalTime is: " + truncatedLocalTime);
Updated on: 2019-07-30T22:30:25+05:30

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements