Joda Time API

org.joda.time
Class DateTime

java.lang.Object
  |
  +--org.joda.time.AbstractInstant
        |
        +--org.joda.time.AbstractDateTime
              |
              +--org.joda.time.DateTime
All Implemented Interfaces:
java.lang.Comparable, ReadableDateTime, ReadableInstant, java.io.Serializable

public class DateTime
extends AbstractDateTime
implements ReadableDateTime

DateTime is the basic implementation of a datetime class supporting chronologies and time zones. It holds the time as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.

Each individual field can be queried in two ways:

The second technique also provides access to other useful methods on the field:

This class is immutable provided that the Chronology is immutable. All Chronology classes supplied are immutable.

Since:
1.0
Author:
Stephen Colebourne, Kandarp Shah, Brian S O'Neill
See Also:
Serialized Form

Constructor Summary
DateTime()
          Constructs a DateTime to the current datetime, as reported by the system clock.
DateTime(java.util.Calendar calendar)
          Constructs a DateTime from a Calendar, using its closest mapped chronology and time zone.
DateTime(java.util.Calendar calendar, Chronology chronology)
          Constructs a DateTime from a Calendar, using the supplied chronology.
DateTime(java.util.Calendar calendar, DateTimeZone zone)
          Constructs a DateTime from a Calendar, using its closest mapped chronology against a different time zone.
DateTime(Chronology chronology)
          Constructs a DateTime to the current datetime, as reported by the system clock.
DateTime(java.util.Date date)
          Constructs a DateTime from a Date, using the ISO chronology, in the default time zone.
DateTime(java.util.Date date, Chronology chronology)
          Constructs a DateTime from a Date, using the supplied chronology.
DateTime(java.util.Date date, DateTimeZone zone)
          Constructs a DateTime from a Date, using the ISO chronology, in the supplied time zone.
DateTime(DateTimeZone zone)
          Constructs a DateTime to the current datetime, as reported by the system clock.
DateTime(long millis)
          Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the ISO chronology, in the default time zone.
DateTime(long millis, Chronology chronology)
          Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the supplied chronology.
DateTime(long millis, DateTimeZone zone)
          Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the ISO chronology, in the supplied time zone.
DateTime(ReadableInstant instant)
          Constructs a DateTime from a ReadableInstant, using its chronology.
DateTime(ReadableInstant instant, Chronology chronology)
          Constructs a DateTime from a ReadableInstant, using the supplied chronology.
DateTime(ReadableInstant instant, DateTimeZone zone)
          Constructs a DateTime from a ReadableInstant, using its chronology against a different time zone.
DateTime(java.lang.String str)
          Constructs a DateTime from an ISO formatted String, using the ISO chronology, in the default time zone.
DateTime(java.lang.String str, Chronology chronology)
          Constructs a DateTime from an ISO formatted String, using the supplied chronology.
DateTime(java.lang.String str, DateTimeZone zone)
          Constructs a DateTime from an ISO formatted String, using the ISO chronology, in the supplied time zone.
 
Method Summary
 DateTimeFieldProperty centuryOfEra()
          Get the century of era property.
protected  ReadableInstant create(long millis, Chronology chrono)
          Creates a new instance of this class.
 DateTimeFieldProperty dayOfMonth()
          Get the day of month property.
 DateTimeFieldProperty dayOfWeek()
          Get the day of week property.
 DateTimeFieldProperty dayOfYear()
          Get the day of year property.
 DateTimeFieldProperty era()
          Get the era property.
 Chronology getChronology()
          Gets the chronology of the datetime.
 long getMillis()
          Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.
 DateTimeFieldProperty hourOfDay()
          Get the hour of day field property.
 DateTimeFieldProperty millisOfDay()
          Get the millis of day property.
 DateTimeFieldProperty millisOfSecond()
          Get the millis of second property.
 DateTimeFieldProperty minuteOfDay()
          Get the minute of day property.
 DateTimeFieldProperty minuteOfHour()
          Get the minute of hour field property.
 DateTimeFieldProperty monthOfYear()
          Get the month of year property.
 DateTimeFieldProperty secondOfDay()
          Get the second of day property.
 DateTimeFieldProperty secondOfMinute()
          Get the second of minute field property.
 java.lang.String toString()
          Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZ).
 DateTimeFieldProperty weekOfWeekyear()
          Get the week of a week based year property.
 DateTimeFieldProperty weekyear()
          Get the year of a week based year property.
 DateTimeFieldProperty year()
          Get the year property.
 DateTimeFieldProperty yearOfCentury()
          Get the year of century property.
 DateTimeFieldProperty yearOfEra()
          Get the year of era property.
 
Methods inherited from class org.joda.time.AbstractDateTime
getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toDateTime, toDateTime, toDateTime, toISODateTime, toISODateTime, toISOMutableDateTime, toISOMutableDateTime, toMutableDateTime, toMutableDateTime, toMutableDateTime, toString, toString
 
Methods inherited from class org.joda.time.AbstractInstant
compareTo, equals, get, getDateTimeZone, hashCode, isAfter, isBefore, isEqual, toCalendar, toCopy, toCopy, toDate, toGregorianCalendar, toInstant
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.joda.time.ReadableDateTime
getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toDateTime, toDateTime, toDateTime, toMutableDateTime, toMutableDateTime, toMutableDateTime, toString, toString
 
Methods inherited from interface org.joda.time.ReadableInstant
compareTo, equals, get, getDateTimeZone, hashCode, isAfter, isBefore, isEqual, toCopy, toCopy, toInstant
 

Constructor Detail

DateTime

public DateTime()
Constructs a DateTime to the current datetime, as reported by the system clock. The chronology used is ISO, in the default time zone.


DateTime

public DateTime(DateTimeZone zone)
Constructs a DateTime to the current datetime, as reported by the system clock. The chronology used is ISO, in the supplied time zone.

Parameters:
zone - the time zone, must not be null
Throws:
java.lang.IllegalArgumentException - if the zone is null

DateTime

public DateTime(Chronology chronology)
Constructs a DateTime to the current datetime, as reported by the system clock.

Parameters:
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the chronology is null

DateTime

public DateTime(long millis)
Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the ISO chronology, in the default time zone.

Parameters:
millis - the milliseconds

DateTime

public DateTime(long millis,
                DateTimeZone zone)
Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the ISO chronology, in the supplied time zone.

Parameters:
millis - the milliseconds
zone - the time zone, must not be null
Throws:
java.lang.IllegalArgumentException - if the zone is null

DateTime

public DateTime(long millis,
                Chronology chronology)
Constructs a DateTime set to the milliseconds from 1970-01-01T00:00:00Z, using the supplied chronology.

Parameters:
millis - the milliseconds
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the chronology is null

DateTime

public DateTime(ReadableInstant instant)
Constructs a DateTime from a ReadableInstant, using its chronology. If its chronology null, then the chronology is set to ISO, in the default time zone.

Parameters:
instant - the ReadableInstant, must not be null
Throws:
java.lang.IllegalArgumentException - if the instant is null

DateTime

public DateTime(ReadableInstant instant,
                DateTimeZone zone)
Constructs a DateTime from a ReadableInstant, using its chronology against a different time zone. If its chronology is null, then the chronology is set to ISO. If the selected chronology is not in the supplied time zone, a new chronology is created that is.

Parameters:
instant - the ReadableInstant, must not be null
zone - the time zone, must not be null
Throws:
java.lang.IllegalArgumentException - if the instant or zone is null

DateTime

public DateTime(ReadableInstant instant,
                Chronology chronology)
Constructs a DateTime from a ReadableInstant, using the supplied chronology.

Parameters:
instant - the ReadableInstant, must not be null
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the instant or chronology is null

DateTime

public DateTime(java.util.Date date)
Constructs a DateTime from a Date, using the ISO chronology, in the default time zone.

Parameters:
date - the Date, must not be null
Throws:
java.lang.IllegalArgumentException - if the date is null

DateTime

public DateTime(java.util.Date date,
                DateTimeZone zone)
Constructs a DateTime from a Date, using the ISO chronology, in the supplied time zone.

Parameters:
date - the Date, must not be null
zone - the time zone, must not be null
Throws:
java.lang.IllegalArgumentException - if the date or zone is null

DateTime

public DateTime(java.util.Date date,
                Chronology chronology)
Constructs a DateTime from a Date, using the supplied chronology.

Parameters:
date - the Date, must not be null
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the date or chronology is null

DateTime

public DateTime(java.util.Calendar calendar)
Constructs a DateTime from a Calendar, using its closest mapped chronology and time zone.

When converting calendars to chronologies, the constructor is aware of GregorianCalendar and BuddhistCalendar and maps them to the equivalent chronology. Other calendars map to ISOChronology.

Parameters:
calendar - the Calendar, must not be null
Throws:
java.lang.IllegalArgumentException - if the calendar is null

DateTime

public DateTime(java.util.Calendar calendar,
                DateTimeZone zone)
Constructs a DateTime from a Calendar, using its closest mapped chronology against a different time zone.

When converting calendars to chronologies, the constructor is aware of GregorianCalendar and BuddhistCalendar and maps them to the equivalent chronology. Other calendars map to ISOChronology.

Parameters:
calendar - the Calendar, must not be null
Throws:
java.lang.IllegalArgumentException - if the calendar or zone is null

DateTime

public DateTime(java.util.Calendar calendar,
                Chronology chronology)
Constructs a DateTime from a Calendar, using the supplied chronology.

Parameters:
calendar - the Calendar, must not be null
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the calendar or chronology is null

DateTime

public DateTime(java.lang.String str)
         throws java.text.ParseException
Constructs a DateTime from an ISO formatted String, using the ISO chronology, in the default time zone.

Parameters:
str - the string to parse, must not be null
Throws:
java.lang.IllegalArgumentException - if the string is null
java.text.ParseException - if parsing fails

DateTime

public DateTime(java.lang.String str,
                DateTimeZone zone)
         throws java.text.ParseException
Constructs a DateTime from an ISO formatted String, using the ISO chronology, in the supplied time zone.

Parameters:
str - the string to parse, must not be null
zone - the time zone, must not be null
Throws:
java.lang.IllegalArgumentException - if the string or zone is null
java.text.ParseException - if parsing fails

DateTime

public DateTime(java.lang.String str,
                Chronology chronology)
         throws java.text.ParseException
Constructs a DateTime from an ISO formatted String, using the supplied chronology.

Parameters:
str - the string to parse, must not be null
chronology - the chronology, must not be null
Throws:
java.lang.IllegalArgumentException - if the string or chronology is null
java.text.ParseException - if parsing fails
Method Detail

create

protected ReadableInstant create(long millis,
                                 Chronology chrono)
Creates a new instance of this class.

The returned object will be a new instance of the implementation. Immutable subclasses may return this if appropriate.

Specified by:
create in class AbstractInstant
Parameters:
millis - the new millis, from 1970-01-01T00:00:00Z
chrono - the new chronology
Returns:
a new instance of this class
Throws:
java.lang.IllegalArgumentException - if the chronology is null

getMillis

public final long getMillis()
Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.

Specified by:
getMillis in interface ReadableInstant
Returns:
the number of milliseconds since 1970-01-01T00:00:00Z

getChronology

public final Chronology getChronology()
Gets the chronology of the datetime.

Specified by:
getChronology in interface ReadableInstant
Returns:
the Chronology that the datetime is using

dayOfWeek

public final DateTimeFieldProperty dayOfWeek()
Get the day of week property.

The values for day of week are defined in DateTimeConstants.

Returns:
the day of week property

dayOfMonth

public final DateTimeFieldProperty dayOfMonth()
Get the day of month property.

Returns:
the day of month property

dayOfYear

public final DateTimeFieldProperty dayOfYear()
Get the day of year property.

Returns:
the day of year property

weekOfWeekyear

public final DateTimeFieldProperty weekOfWeekyear()
Get the week of a week based year property.

Returns:
the week of a week based year property

weekyear

public final DateTimeFieldProperty weekyear()
Get the year of a week based year property.

Returns:
the year of a week based year property

monthOfYear

public final DateTimeFieldProperty monthOfYear()
Get the month of year property.

Returns:
the month of year property

year

public final DateTimeFieldProperty year()
Get the year property.

Returns:
the year property

yearOfEra

public final DateTimeFieldProperty yearOfEra()
Get the year of era property.

Returns:
the year of era property

yearOfCentury

public final DateTimeFieldProperty yearOfCentury()
Get the year of century property.

Returns:
the year of era property

centuryOfEra

public final DateTimeFieldProperty centuryOfEra()
Get the century of era property.

Returns:
the year of era property

era

public final DateTimeFieldProperty era()
Get the era property.

Returns:
the era property

millisOfSecond

public final DateTimeFieldProperty millisOfSecond()
Get the millis of second property.

Returns:
the millis of second property

millisOfDay

public final DateTimeFieldProperty millisOfDay()
Get the millis of day property.

Returns:
the millis of day property

secondOfMinute

public final DateTimeFieldProperty secondOfMinute()
Get the second of minute field property.

Returns:
the second of minute property

secondOfDay

public final DateTimeFieldProperty secondOfDay()
Get the second of day property.

Returns:
the second of day property

minuteOfHour

public final DateTimeFieldProperty minuteOfHour()
Get the minute of hour field property.

Returns:
the minute of hour property

minuteOfDay

public final DateTimeFieldProperty minuteOfDay()
Get the minute of day property.

Returns:
the minute of day property

hourOfDay

public final DateTimeFieldProperty hourOfDay()
Get the hour of day field property.

Returns:
the hour of day property

toString

public final java.lang.String toString()
Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZ).

Specified by:
toString in interface ReadableInstant
Overrides:
toString in class AbstractDateTime
Returns:
ISO8601 time formatted string.

Joda Time API

Copyright © 2001-2003 Stephen Colebourne. All Rights Reserved.