Joda-Time what's the difference between Period, Interval and Duration?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Joda-Time: Period, Interval, and Duration
Joda-Time is a popular date and time handling library for Java, offering a more intuitive approach compared to the legacy java.util.Date and java.util.Calendar. While Java 8 introduced the java.time package inspired by Joda-Time, Joda-Time remains relevant for legacy systems or projects not using Java 8+. This article focuses on three critical concepts in Joda-Time: Period, Interval, and Duration.
Period
A Period in Joda-Time represents a heterogeneous, human-readable span of time, such as "2 years, 3 months, and 5 days." Unlike Duration, which refers to a fixed amount of time in milliseconds, a Period respects the complexity of the calendar system, varying month lengths, leap years, etc.
Key Features:
- Heterogeneous Units: Represents multiple time units (years, months, days, hours).
- Human-Readable: Meant for expressing durations in human terms.
- Calendar-Aware: Adjusts according to calendar variations.
Example:
Interval
An Interval represents a time interval between two instants on the timeline. It's defined by a start and an end Instant and includes all "real" time between them.
Key Features:
- Fixed Start and End: Defined by two specific points in time.
- Continuous: Represents chronological continuity.
- Usage: Often used for scheduling or timeline views.
Example:
Duration
A Duration is a fixed span of time measured in milliseconds. Unlike Period, which may account for periods like "months" and "years," a Duration is absolute and not tied to the calendar system.
Key Features:
- Fixed Measurement: Defined in terms of milliseconds.
- Calendar-Ignorant: Ignores variations like daylight saving time.
- Precision Over-readability: More akin to stopwatch times than calendar intervals.
Example:
Differences at a Glance
Let's summarize the differences between Period, Interval, and Duration using a table for clarity:
| Aspect | Period | Interval | Duration |
| Definition | Human-readable span of time | Time between two fixed points | Fixed length of time in ms |
| Measurement Unit | Years, months, days etc. | Days, hours, minutes, seconds between instants | Milliseconds |
| Calendar-Aware | Yes | Yes | No |
| Use Case | Readable date/times | Tracking time between events Like calendar intervals | Stopwatch-like timing |
| Variability | Adjusts with calendar | Depends on start/end instants (may vary in length) | Constant duration |
Subtopics for Expanded Understanding
- Conversion and Compatibility:
- A
Periodcan be converted to aDurationif you wish to consider a precise length, but the conversion requires a reference point (like startingDateTime) due to variability in period lengths.
- Common Utilities:
Periodhas utilities likePeriod.between(start, end)to generate periods from two dates.- An
Intervalcan be useful for methods likecontains()which checks if a givenInstantlies within the interval.
- Comparison with java.time:
- Java's
java.timeAPI includes similar concepts:java.time.Period,java.time.Duration, andjava.time.LocalDateTime, providing a more modern foundation but inspired by Joda-Time's robust design.
- Migration Considerations:
- Projects using Joda-Time planning to move to Java 8+ can map
Period,Interval, andDurationto nativejava.timecounterparts.
Understanding these concepts equips developers to handle temporal data more efficiently, catering for both human-readable and machine-recognizable representations. Choose Period when handling human-friendly time descriptions, Interval when working with timelines, and Duration for fixed time spans without calendar considerations.
Related reading
- JPA and Hibernate - Criteria vs. JPQL or HQL
- JPA JoinColumn vs mappedBy
- K8S Read config map via go API
- Kafka - Consumer group creation with specific offset?
- Kafka - How to use filter and filternot at the same time?
- kafka AdminClient API Timed out waiting for node assignment
- Kafka AND REST for communication between microservices?
- kafka bootstrap.servers as DNS A-Record with multiple IPs

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.