Package org.joda.time
Provides support for dates, times, time zones, durations, intervals, and
partials. This package aims to fully replace the Java Date
,
Calendar
, and TimeZone
classes. This implementation
covers both the Gregorian/Julian calendar system and the ISO8601
standard. Additional calendar systems and extensions can be created as well.
The ISO8601 standard is the international standard for dates, times, durations, and intervals. It defines text representations, the first day of the week as Monday, and the first week in a year as having a Thursday in it. This standard is being increasingly used in computer interchange and is the agreed format for XML. For most uses, the ISO standard is the same as Gregorian, and is thus the preferred format.
Interfaces
The main API concepts are defined by interfaces:
ReadableInstant
- an instant in timeReadableDateTime
- an instant in time with field accessors such as dayOfWeekReadablePartial
- a definition for local times that are not defined to the millisecond, such as the time of dayReadableDuration
- a duration defined in millisecondsReadablePeriod
- a time period defined in fields such as hours and minutesReadableInterval
- a period of time between two instants
ReadWritableInstant
- an instant that can be modifiedReadWritableDateTime
- a datetime that can be modifiedReadWritablePeriod
- a time period that can be modifiedReadWritableInterval
- an interval that can be modified
These define the public interface to dates, times, periods, intervals and durations.
As with java.util.Date
and Calendar
, the design is
millisecond based with an epoch of 1970-01-01.
This should enable easy conversions.
Implementations
The basic implementation of the ReadableInstant
interface is
Instant
. This is a simple immutable class that stores the
millisecond value and integrates with Java Date and Calendar.
The class follows the definition of the millisecond instant fully, thus
it references the ISO8601 calendar system and UTC time zone.
If you are dealing with an instant in time but do not know, or do not want to specify,
which calendar system it refers to, then you should use this class.
The main implementation class for datetimes is the DateTime
class.
This implements the ReadableDateTime
interface, providing
convenient methods to access the fields of the datetime. Conversion methods
allow integration with the Java Date and Calendar classes.
Like Instant
, DateTime
is immutable, and it
can be used safely in a multi-threaded environment.
In order to be fully immutable, key classes are declared as final.
Abstract superclasses are provided should you need to define your own implementations.
The concrete implementations of the ReadWritable...
interfaces are
named the same as their immutable counterparts, but with a "Mutable" prefix.
For example, MutableDateTime
implements
ReadWritableDateTime
, making datetime editing easy.
Note that it is possible to use the immutable DateTime for modifying datetimes,
however each modification method returns a new instance of DateTime.
Interface usage
The interfaces in Joda-Time are not designed to operate in the same way as those in the Java Collections Framework (List/Map/Set etc). The Joda-Time interfaces represent a core subset of the functionality available via the actual classes. Thus, much of the work of an application will probably use methods on the class, not on the interface. Your application must determine whether it should define dates in terms of the interfaces, or in terms of the classes.
The interfaces provide simple methods to access an instance of the immutable class,
which is implemented either via typecast or object creation.
Thus, if you hold a reference to a ReadableInstant, and you call the method
toDateTime()
, the same instance will be returned (typecast) if it
already was a DateTime.
Chronologies and Fields
In order to enable the package to be easily extended, each field of the
datetime, such as the month, is calculated by an implementation of
DateTimeField
. Likewise, duration fields are calculated by
specialized DurationField
instances. If desired, users can write
their own implementations to retrieve an unusual field from the millisecond
value.
The datetime and duration fields that together represent a calendar system are
grouped into a Chronology
. The chronology represents all the
information to convert from a millisecond value to human understandable fields
in a specific calendar system. Chronologies are provided for ISO,
Gregorian/Julian (GJ), Buddhist, Coptic and Ethiopic.
More implementations are sought from the community.
The chronology and field classes are singletons.
This design results in a low overhead on the date and time classes.
The Java Calendar
class performs poorly because it has many internal
fields that are constantly kept in sync.
This design only calculates fields when required, resulting in lightweight and
simple date time classes.
When reviewing the library for the first time, it is easy to mistake the number of classes with complexity. The library is in fact clearly divided between user packages and implementation packages in the javadoc. Most users will should not need to be concerned with the back-end implementation.
Partials
Partials are like instants, except they do not completely specify a point in
time. The main interface is ReadablePartial
.
The main implementations are:
LocalTime
- A class storing a local time without a dateLocalDate
- A class storing a local date without a timeLocalDateTime
- A class storing a local datetimePartial
- A class storing any combination of datetime fields, such as dayOfMonth and dayOfWeek
All partial implementations represent a local time, in other words without a time zone. Thus, to convert a partial to an instant (which does contain a time zone) requires adding a zone.
Formatting
Formatting is provided by the format
subpackage. Comprehensive
support is provided for outputting dates and times in multiple formats. A
pattern similar to Java SimpleDateFormat
can be used, but a more
advanced programmatic technique is available via the builder classes.
-
Interface Summary Interface Description DateTimeUtils.MillisProvider A millisecond provider, allowing control of the system clock.ReadableDateTime Defines an instant in time that can be queried using datetime fields.ReadableDuration Defines an exact duration of time in milliseconds.ReadableInstant Defines an instant in the datetime continuum.ReadableInterval Readable interface for an interval of time between two instants.ReadablePartial Defines a partial time that does not support every datetime field, and is thus a local time.ReadablePeriod Defines a time period specified in terms of individual duration fields such as years and days.ReadWritableDateTime Defines an instant in time that can be queried and modified using datetime fields.ReadWritableInstant Defines an instant in the datetime continuum that can be queried and modified.ReadWritableInterval Writable interface for an interval.ReadWritablePeriod Defines a duration of time that can be queried and modified using datetime fields. -
Class Summary Class Description Chronology Chronology provides access to the individual date time fields for a chronological calendar system.DateMidnight Deprecated. The time of midnight does not exist in some time zones where the daylight saving time forward shift skips the midnight hour.DateMidnight.Property DateMidnight.Property binds a DateMidnight to a DateTimeField allowing powerful datetime functionality to be easily accessed.DateTime DateTime is the standard implementation of an unmodifiable datetime class.DateTime.Property DateTime.Property binds a DateTime to a DateTimeField allowing powerful datetime functionality to be easily accessed.DateTimeComparator DateTimeComparator provides comparators to compare one date with another.DateTimeConstants DateTimeConstants is a non-instantiable class of constants used in the date time system.DateTimeField Defines the calculation engine for date and time fields.DateTimeFieldType Identifies a field, such as year or minuteOfHour, in a chronology-neutral way.DateTimeUtils DateTimeUtils provide public utility methods for the date-time library.DateTimeZone DateTimeZone represents a time zone.Days An immutable time period representing a number of days.Duration An immutable duration specifying a length of time in milliseconds.DurationField Defines the calculation engine for duration fields.DurationFieldType Identifies a duration field, such as years or minutes, in a chronology-neutral way.Hours An immutable time period representing a number of hours.Instant Instant is the standard implementation of a fully immutable instant in time.Interval Interval is the standard implementation of an immutable time interval.JodaTimePermission JodaTimePermission is used for securing global method calls in the Joda-Time library.LocalDate LocalDate is an immutable datetime class representing a date without a time zone.LocalDate.Property LocalDate.Property binds a LocalDate to a DateTimeField allowing powerful datetime functionality to be easily accessed.LocalDateTime LocalDateTime is an unmodifiable datetime class representing a datetime without a time zone.LocalDateTime.Property LocalDateTime.Property binds a LocalDateTime to a DateTimeField allowing powerful datetime functionality to be easily accessed.LocalTime LocalTime is an immutable time class representing a time without a time zone.LocalTime.Property LocalTime.Property binds a LocalTime to a DateTimeField allowing powerful datetime functionality to be easily accessed.Minutes An immutable time period representing a number of minutes.MonthDay MonthDay is an immutable partial supporting the monthOfYear and dayOfMonth fields.MonthDay.Property The property class forMonthDay
.Months An immutable time period representing a number of months.MutableDateTime MutableDateTime is the standard implementation of a modifiable datetime class.MutableDateTime.Property MutableDateTime.Property binds a MutableDateTime to a DateTimeField allowing powerful datetime functionality to be easily accessed.MutableInterval MutableInterval is the standard implementation of a mutable time interval.MutablePeriod Standard mutable time period implementation.Partial Partial is an immutable partial datetime supporting any set of datetime fields.Partial.Property The property class forPartial
.Period An immutable time period specifying a set of duration field values.PeriodType Controls a period implementation by specifying which duration fields are to be used.Seconds An immutable time period representing a number of seconds.TimeOfDay Deprecated. Use LocalTime which has a much better internal implementation and has been available since 1.3TimeOfDay.Property Deprecated. Use LocalTime which has a much better internal implementationWeeks An immutable time period representing a number of weeks.YearMonth YearMonth is an immutable partial supporting the year and monthOfYear fields.YearMonth.Property The property class forYearMonth
.YearMonthDay Deprecated. Use LocalDate which has a much better internal implementation and has been available since 1.3YearMonthDay.Property Deprecated. Use LocalDate which has a much better internal implementationYears An immutable time period representing a number of years. -
Exception Summary Exception Description IllegalFieldValueException Exception thrown when attempting to set a field outside its supported range.IllegalInstantException Exception thrown when attempting to create an instant or date-time that cannot exist.