Calculate date/time difference in java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Calculating the difference between two dates or times in Java is a common task required in several applications ranging from simple scheduling tools to complex analytics systems. Java offers a vast array of classes and methods to deal with date and time, especially with enhancements introduced in Java 8. This article guides you through the process of calculating date/time differences in Java, making use of both pre-Java 8 and Java 8 (and later) libraries.
Pre-Java 8 Approach
Before Java 8, the most commonly used way to handle date and time was using the java.util.Date and java.util.Calendar classes. Although they have limited functionality and are more error-prone, understanding them is useful if you are maintaining legacy code.
Using Date and Calendar
In the pre-Java 8 world, you can compute the difference between two days using the Date class:
Shortcomings
- Immutability:
Dateis mutable and not thread-safe. - API Design: Methods like
getYear(),getMonth(), etc., are deprecated.
Java 8 and Later
From Java 8 onwards, the java.time package provides more powerful and flexible date/time classes. These solve many of the issues found in previous classes by offering immutable, thread-safe objects and a more comprehensive feature set.
Using LocalDate, LocalTime, and Duration
With Java 8, calculating date differences becomes more intuitive:
Between Local Dates
Between Local Times
Key Benefits
- Immutability: All classes are immutable and thread-safe.
- Rich API: Easy access to temporal units like
DurationandPeriod. - Flexibility: Can handle complex calculations with
TemporalAdjuster.
Summary Table
| Approach | Class Used | Benefits | Drawbacks |
| Pre-Java 8 | Date, Calendar | Simple to use | Mutable, non-thread-safe Limited API |
| Java 8 and later | LocalDate/ LocalTime, Period/ Duration | Immutability, Rich API | More complex for simple tasks |
Additional Subtopics
Time Zone Consideration
When dealing with time differences, it's crucial to consider time zones especially for global applications. The ZonedDateTime class can be used for this purpose:
Parsing and Formatting
Java 8's DateTimeFormatter offers a robust way to parse and format dates and times, crucial for input/output operations.
Conclusion
Calculating the date/time difference in Java can be efficiently handled using the java.time package from Java 8 onwards. These new classes provide a more robust, secure, and developer-friendly API. However, knowledge of pre-Java 8 approaches is still valuable for maintaining legacy systems. By understanding both, you can effectively handle a wide range of date and time operations in Java applications.
Related reading
- Calculate days between two Dates in Java 8
- Calculate size of Object in Java
- Calculating days between two dates with Java
- Calculating the difference between two Java date instances
- Calendar date to yyyy-MM-dd format in java
- Call another rest api from my server in Spring-Boot
- Call child method from parent
- Call one constructor from another

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.