Overview
Joda-Time
is a popular open source API for date and time manipulation.
Joda-Time is intuitive to use and is very elegantly designed. It has maps closely to the date-time domain concepts and has supports immutability. It has a wide variety of formatters which are thread safe. Support for TimeZones and multiple calendar systems is also provided.
Joda-Time API was a key driver in designing the
JSR 310
specification.
This is a How-To Guide for using the common features in Joda-Time API. Working code along with the expected output is described for each How-To.
The libraries used in the following examples are:
- joda-time-2.3.jar
- joda-convert-1.5.jar
The above libraries can be downloaded from Joda-Time
download page
- Date Concepts
- Time Concepts
- Joda-Time Specific Concepts
- Instantiate DateTime
- Get DateTime for current instant
- Get sub-elements of DateTime (e.g. year, month, hours, etc.)
- Perform arithmetic operations on DateTime like addition and substraction
- Update sub-elements of DateTime
- Get DateTime from String representation
- Compare two DateTime instances
- Convert DateTime to other representations like Milliseconds, JDK date, MutableDateTime, LocalDateTime and String
- Handling DateTime Exceptions
- Compare DateTime
- Compare Two DateTime instances considering only Date fields and ignoring TIme fields
- Compare Two DateTime instances considering only Time fields and ignoring Date fields
- Using Upper and Lower Limits for DateTime comparison
- Formatting Year Field
- Formatting Month Field
- Formatting Day Field
- Formatting Day Of Week Field
- Formatting Time Fields
- Using Formatting Styles: Short, Medium, Long, Full and Omit
- Define Styles Using Convenience Methods
- Custom Formatting of Date and Time Using DateTimeFormatterBuilder
- Constructing DateTimeZone
- Get DateTimeZone Fields
- Create Duration
- Perform Arithmetic Operations On Duration
- Convert Duration To Single Fields Periods like Days, Hours, Minutes and Seconds
- Creating Instant
- Comparing Two Instants
- Get Sub-Fields of Instant
- Perform Arithmentic Operations On Instant like Addition, Subtraction and Repeated Addition
- Convert Instant To Other Representations Like DateTime, MutableDateTime,JDK Date and String
- Construct An Interval
- Find Relationship Between Two Intervals (Gap, Adjoining, Overlap)
- Modify Interval Using Millis, Duration and Instant
- Calculate Number Of Days
- Perform Arithmetic Operations On Single Field Periods
- Parsing ISO 8601 Format String For Period
- Convert To Other Standard Representations Like Duration, Hour, Minute And Second
- Modify Fields
- Add Fields To MutableDateTime
- Perform Rounding Operations
- Modify Interval
- Modify Period
- Convert 2 Digit Year To 4 Digit Year Using Pivot Year
- Get List Of Default Time Zones
- Verify Whether A Partial DateTime Is Contiguous
Date-Time Key Concepts
Using DateTime
Using DateTimeComparator
Using DateTimeFormat
Using DateTimeZone
Using Duration
Using Instant
Using Interval
Using Single Field Periods
Using MutableDateTime
Using MutableInterval
Using MutablePeriod
Miscellaneous Topics
To start, click on the next arrow. If you find anything missing in the tutorials then please drop in your comments.