- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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);
Advertisements