
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 8: Difference between two LocalDateTime in multiple units
Difference Between Two LocalDateTime
Java 8 introduced a powerful Date and Time API that provides developers with better control over date and time operations. One common requirement is to calculate the difference between two LocalDateTime
objects in various units such as days, hours, minutes, and seconds.
This article explains how to achieve this using Java 8's Duration
and ChronoUnit
classes.
Understanding LocalDateTime
LocalDateTime
is a class in Java 8 that represents a date-time without a time-zone. It is often used in applications where the time zone is not needed. Calculating differences between two LocalDateTime
instances is straightforward with the new API.
Using the Duration
Class
The Duration
class measures time in seconds and nanoseconds. You can use it to calculate the difference between two LocalDateTime
objects in multiple units. Here's an example:
import java.time.LocalDateTime; import java.time.Duration; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2023, 12, 1, 10, 0); LocalDateTime end = LocalDateTime.of(2023, 12, 2, 15, 30); Duration duration = Duration.between(start, end); System.out.println("Days: " + duration.toDays()); System.out.println("Hours: " + duration.toHours()); System.out.println("Minutes: " + duration.toMinutes()); System.out.println("Seconds: " + duration.getSeconds()); } }
Following is the output of the above code
Days: 1 Hours: 29 Minutes: 1770 Seconds: 106200
Using the ChronoUnit
Enum
The ChronoUnit
enum provides another way to calculate differences. It is more versatile as it supports units like weeks, months, and years:
import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2023, 12, 1, 10, 0); LocalDateTime end = LocalDateTime.of(2023, 12, 2, 15, 30); System.out.println("Days: " + ChronoUnit.DAYS.between(start, end)); System.out.println("Hours: " + ChronoUnit.HOURS.between(start, end)); System.out.println("Minutes: " + ChronoUnit.MINUTES.between(start, end)); System.out.println("Seconds: " + ChronoUnit.SECONDS.between(start, end)); } }
Following is the output of the above code
Days: 1 Hours: 29 Minutes: 1770 Seconds: 106200
Duration
Vs ChronoUnit
Following are the differences between Duration and ChronoUnit
Aspect | Duration | ChronoUnit |
---|---|---|
Units Supported | Seconds and nanoseconds. | Broad range including days, weeks, months, and years. |
API Usage | Used with Duration.between() . |
Used with ChronoUnit.between() . |
Simplicity | Best for short-duration calculations. | More flexible for diverse use cases. |
Conclusion
Both Duration
and ChronoUnit
offer effective ways to calculate differences between two LocalDateTime
instances. Choose Duration
for precise short-duration measurements and ChronoUnit
for a broader range of units. By leveraging these tools, you can handle complex date-time calculations with ease.