public final class DateTime extends BaseDateTime implements ReadableDateTime, java.io.Serializable
DateTime
is the most widely used implementation of
ReadableInstant
. As with all instants, it represents an exact
point on the time-line, but limited to the precision of milliseconds.
A DateTime
calculates its fields with respect to a
time zone
.
Internally, the class holds two pieces of data. Firstly, it holds the
datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.
Secondly, it holds a Chronology
which determines how the
millisecond instant value is converted into the date time fields.
The default Chronology is ISOChronology
which is the agreed
international standard and compatible with the modern Gregorian calendar.
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
DateTime is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.
MutableDateTime
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
DateTime.Property
DateTime.Property binds a DateTime to a DateTimeField allowing powerful
datetime functionality to be easily accessed.
|
Constructor and Description |
---|
DateTime()
Constructs an instance set to the current system millisecond time
using
ISOChronology in the default time zone. |
DateTime(Chronology chronology)
Constructs an instance set to the current system millisecond time
using the specified chronology.
|
DateTime(DateTimeZone zone)
Constructs an instance set to the current system millisecond time
using
ISOChronology in the specified time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour)
Constructs an instance from datetime field values
using
ISOChronology in the default time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
Chronology chronology)
Constructs an instance from datetime field values
using the specified chronology.
|
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
DateTimeZone zone)
Constructs an instance from datetime field values
using
ISOChronology in the specified time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute)
Constructs an instance from datetime field values
using
ISOChronology in the default time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
Chronology chronology)
Constructs an instance from datetime field values
using the specified chronology.
|
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
DateTimeZone zone)
Constructs an instance from datetime field values
using
ISOChronology in the specified time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Constructs an instance from datetime field values
using
ISOChronology in the default time zone. |
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
Chronology chronology)
Constructs an instance from datetime field values
using the specified chronology.
|
DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
DateTimeZone zone)
Constructs an instance from datetime field values
using
ISOChronology in the specified time zone. |
DateTime(long instant)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using
ISOChronology in the default time zone. |
DateTime(long instant,
Chronology chronology)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using the specified chronology.
|
DateTime(long instant,
DateTimeZone zone)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using
ISOChronology in the specified time zone. |
DateTime(java.lang.Object instant)
Constructs an instance from an Object that represents a datetime.
|
DateTime(java.lang.Object instant,
Chronology chronology)
Constructs an instance from an Object that represents a datetime,
using the specified chronology.
|
DateTime(java.lang.Object instant,
DateTimeZone zone)
Constructs an instance from an Object that represents a datetime,
forcing the time zone to that specified.
|
Modifier and Type | Method and Description |
---|---|
DateTime.Property |
centuryOfEra()
Get the century of era property which provides access to advanced functionality.
|
DateTime.Property |
dayOfMonth()
Get the day of month property which provides access to advanced functionality.
|
DateTime.Property |
dayOfWeek()
Get the day of week property which provides access to advanced functionality.
|
DateTime.Property |
dayOfYear()
Get the day of year property which provides access to advanced functionality.
|
DateTime.Property |
era()
Get the era property which provides access to advanced functionality.
|
DateTime.Property |
hourOfDay()
Get the hour of day field property which provides access to advanced functionality.
|
DateTime.Property |
millisOfDay()
Get the millis of day property which provides access to advanced functionality.
|
DateTime.Property |
millisOfSecond()
Get the millis of second property which provides access to advanced functionality.
|
DateTime |
minus(long duration)
Returns a copy of this datetime with the specified duration taken away.
|
DateTime |
minus(ReadableDuration duration)
Returns a copy of this datetime with the specified duration taken away.
|
DateTime |
minus(ReadablePeriod period)
Returns a copy of this datetime with the specified period taken away.
|
DateTime |
minusDays(int days)
Returns a copy of this datetime minus the specified number of days.
|
DateTime |
minusHours(int hours)
Returns a copy of this datetime minus the specified number of hours.
|
DateTime |
minusMillis(int millis)
Returns a copy of this datetime minus the specified number of millis.
|
DateTime |
minusMinutes(int minutes)
Returns a copy of this datetime minus the specified number of minutes.
|
DateTime |
minusMonths(int months)
Returns a copy of this datetime minus the specified number of months.
|
DateTime |
minusSeconds(int seconds)
Returns a copy of this datetime minus the specified number of seconds.
|
DateTime |
minusWeeks(int weeks)
Returns a copy of this datetime minus the specified number of weeks.
|
DateTime |
minusYears(int years)
Returns a copy of this datetime minus the specified number of years.
|
DateTime.Property |
minuteOfDay()
Get the minute of day property which provides access to advanced functionality.
|
DateTime.Property |
minuteOfHour()
Get the minute of hour field property which provides access to advanced functionality.
|
DateTime.Property |
monthOfYear()
Get the month of year property which provides access to advanced functionality.
|
static DateTime |
now()
Obtains a
DateTime set to the current system millisecond time
using ISOChronology in the default time zone. |
static DateTime |
now(Chronology chronology)
Obtains a
DateTime set to the current system millisecond time
using the specified chronology. |
static DateTime |
now(DateTimeZone zone)
Obtains a
DateTime set to the current system millisecond time
using ISOChronology in the specified time zone. |
static DateTime |
parse(java.lang.String str)
Parses a
DateTime from the specified string. |
static DateTime |
parse(java.lang.String str,
DateTimeFormatter formatter)
Parses a
DateTime from the specified string using a formatter. |
DateTime |
plus(long duration)
Returns a copy of this datetime with the specified duration added.
|
DateTime |
plus(ReadableDuration duration)
Returns a copy of this datetime with the specified duration added.
|
DateTime |
plus(ReadablePeriod period)
Returns a copy of this datetime with the specified period added.
|
DateTime |
plusDays(int days)
Returns a copy of this datetime plus the specified number of days.
|
DateTime |
plusHours(int hours)
Returns a copy of this datetime plus the specified number of hours.
|
DateTime |
plusMillis(int millis)
Returns a copy of this datetime plus the specified number of millis.
|
DateTime |
plusMinutes(int minutes)
Returns a copy of this datetime plus the specified number of minutes.
|
DateTime |
plusMonths(int months)
Returns a copy of this datetime plus the specified number of months.
|
DateTime |
plusSeconds(int seconds)
Returns a copy of this datetime plus the specified number of seconds.
|
DateTime |
plusWeeks(int weeks)
Returns a copy of this datetime plus the specified number of weeks.
|
DateTime |
plusYears(int years)
Returns a copy of this datetime plus the specified number of years.
|
DateTime.Property |
property(DateTimeFieldType type)
Gets the property object for the specified type, which contains many useful methods.
|
DateTime.Property |
secondOfDay()
Get the second of day property which provides access to advanced functionality.
|
DateTime.Property |
secondOfMinute()
Get the second of minute field property which provides access to advanced functionality.
|
DateMidnight |
toDateMidnight()
Deprecated.
DateMidnight is deprecated
|
DateTime |
toDateTime()
Get this object as a DateTime by returning
this . |
DateTime |
toDateTime(Chronology chronology)
Get this object as a DateTime, returning
this if possible. |
DateTime |
toDateTime(DateTimeZone zone)
Get this object as a DateTime, returning
this if possible. |
DateTime |
toDateTimeISO()
Get this object as a DateTime using ISOChronology in the default zone,
returning
this if possible. |
LocalDate |
toLocalDate()
Converts this object to a
LocalDate with the
same date and chronology. |
LocalDateTime |
toLocalDateTime()
Converts this object to a
LocalDateTime with
the same datetime and chronology. |
LocalTime |
toLocalTime()
Converts this object to a
LocalTime with the
same time and chronology. |
TimeOfDay |
toTimeOfDay()
Deprecated.
Use LocalTime instead of TimeOfDay
|
YearMonthDay |
toYearMonthDay()
Deprecated.
Use LocalDate instead of YearMonthDay
|
DateTime.Property |
weekOfWeekyear()
Get the week of a week based year property which provides access to advanced functionality.
|
DateTime.Property |
weekyear()
Get the year of a week based year property which provides access to advanced functionality.
|
DateTime |
withCenturyOfEra(int centuryOfEra)
Returns a copy of this datetime with the century of era field updated.
|
DateTime |
withChronology(Chronology newChronology)
Returns a copy of this datetime with a different chronology.
|
DateTime |
withDate(int year,
int monthOfYear,
int dayOfMonth)
Returns a copy of this datetime with the specified date, retaining the time fields.
|
DateTime |
withDate(LocalDate date)
Returns a copy of this datetime with the specified date, retaining the time fields.
|
DateTime |
withDayOfMonth(int dayOfMonth)
Returns a copy of this datetime with the day of month field updated.
|
DateTime |
withDayOfWeek(int dayOfWeek)
Returns a copy of this datetime with the day of week field updated.
|
DateTime |
withDayOfYear(int dayOfYear)
Returns a copy of this datetime with the day of year field updated.
|
DateTime |
withDurationAdded(long durationToAdd,
int scalar)
Returns a copy of this datetime with the specified duration added.
|
DateTime |
withDurationAdded(ReadableDuration durationToAdd,
int scalar)
Returns a copy of this datetime with the specified duration added.
|
DateTime |
withEarlierOffsetAtOverlap()
Returns a copy of this ZonedDateTime changing the zone offset to the earlier
of the two valid offsets at a local time-line overlap.
|
DateTime |
withEra(int era)
Returns a copy of this datetime with the era field updated.
|
DateTime |
withField(DateTimeFieldType fieldType,
int value)
Returns a copy of this datetime with the specified field set to a new value.
|
DateTime |
withFieldAdded(DurationFieldType fieldType,
int amount)
Returns a copy of this datetime with the value of the specified field increased.
|
DateTime |
withFields(ReadablePartial partial)
Returns a copy of this datetime with the partial set of fields replacing those
from this instance.
|
DateTime |
withHourOfDay(int hour)
Returns a copy of this datetime with the hour of day field updated.
|
DateTime |
withLaterOffsetAtOverlap()
Returns a copy of this ZonedDateTime changing the zone offset to the later
of the two valid offsets at a local time-line overlap.
|
DateTime |
withMillis(long newMillis)
Returns a copy of this datetime with different millis.
|
DateTime |
withMillisOfDay(int millis)
Returns a copy of this datetime with the millis of day field updated.
|
DateTime |
withMillisOfSecond(int millis)
Returns a copy of this datetime with the millis of second field updated.
|
DateTime |
withMinuteOfHour(int minute)
Returns a copy of this datetime with the minute of hour updated.
|
DateTime |
withMonthOfYear(int monthOfYear)
Returns a copy of this datetime with the month of year field updated.
|
DateTime |
withPeriodAdded(ReadablePeriod period,
int scalar)
Returns a copy of this datetime with the specified period added.
|
DateTime |
withSecondOfMinute(int second)
Returns a copy of this datetime with the second of minute field updated.
|
DateTime |
withTime(int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Returns a copy of this datetime with the specified time, retaining the date fields.
|
DateTime |
withTime(LocalTime time)
Returns a copy of this datetime with the specified time, retaining the date fields.
|
DateTime |
withTimeAtStartOfDay()
Returns a copy of this datetime with the time set to the start of the day.
|
DateTime |
withWeekOfWeekyear(int weekOfWeekyear)
Returns a copy of this datetime with the week of weekyear field updated.
|
DateTime |
withWeekyear(int weekyear)
Returns a copy of this datetime with the weekyear field updated.
|
DateTime |
withYear(int year)
Returns a copy of this datetime with the year field updated.
|
DateTime |
withYearOfCentury(int yearOfCentury)
Returns a copy of this datetime with the year of century field updated.
|
DateTime |
withYearOfEra(int yearOfEra)
Returns a copy of this datetime with the year of era field updated.
|
DateTime |
withZone(DateTimeZone newZone)
Returns a copy of this datetime with a different time zone, preserving the
millisecond instant.
|
DateTime |
withZoneRetainFields(DateTimeZone newZone)
Returns a copy of this datetime with a different time zone, preserving the
field values.
|
DateTime.Property |
year()
Get the year property which provides access to advanced functionality.
|
DateTime.Property |
yearOfCentury()
Get the year of century property which provides access to advanced functionality.
|
DateTime.Property |
yearOfEra()
Get the year of era property which provides access to advanced functionality.
|
getChronology, getMillis
get, getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toCalendar, toGregorianCalendar, toString, toString, toString
compareTo, equals, get, getZone, hashCode, isAfter, isAfter, isAfterNow, isBefore, isBefore, isBeforeNow, isEqual, isEqual, isEqualNow, isSupported, toDate, toInstant, toMutableDateTime, toMutableDateTime, toMutableDateTime, toMutableDateTimeISO, toString
getCenturyOfEra, getDayOfMonth, getDayOfWeek, getDayOfYear, getEra, getHourOfDay, getMillisOfDay, getMillisOfSecond, getMinuteOfDay, getMinuteOfHour, getMonthOfYear, getSecondOfDay, getSecondOfMinute, getWeekOfWeekyear, getWeekyear, getYear, getYearOfCentury, getYearOfEra, toMutableDateTime, toString, toString
equals, get, getChronology, getMillis, getZone, hashCode, isAfter, isBefore, isEqual, isSupported, toInstant, toString
public DateTime()
ISOChronology
in the default time zone.now()
public DateTime(DateTimeZone zone)
ISOChronology
in the specified time zone.
If the specified time zone is null, the default zone is used.
zone
- the time zone, null means default zonenow(DateTimeZone)
public DateTime(Chronology chronology)
If the chronology is null, ISOChronology
in the default time zone is used.
chronology
- the chronology, null means ISOChronology in default zonenow(Chronology)
public DateTime(long instant)
ISOChronology
in the default time zone.instant
- the milliseconds from 1970-01-01T00:00:00Zpublic DateTime(long instant, DateTimeZone zone)
ISOChronology
in the specified time zone.
If the specified time zone is null, the default zone is used.
instant
- the milliseconds from 1970-01-01T00:00:00Zzone
- the time zone, null means default zonepublic DateTime(long instant, Chronology chronology)
If the chronology is null, ISOChronology
in the default time zone is used.
instant
- the milliseconds from 1970-01-01T00:00:00Zchronology
- the chronology, null means ISOChronology in default zonepublic DateTime(java.lang.Object instant)
If the object implies a chronology (such as GregorianCalendar does), then that chronology will be used. Otherwise, ISO default is used. Thus if a GregorianCalendar is passed in, the chronology used will be GJ, but if a Date is passed in the chronology will be ISO.
The recognised object types are defined in
ConverterManager
and
include ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.dateTimeParser()
.
instant
- the datetime object, null means nowjava.lang.IllegalArgumentException
- if the instant is invalidpublic DateTime(java.lang.Object instant, DateTimeZone zone)
If the object implies a chronology (such as GregorianCalendar does), then that chronology will be used, but with the time zone adjusted. Otherwise, ISO is used in the specified time zone. If the specified time zone is null, the default zone is used. Thus if a GregorianCalendar is passed in, the chronology used will be GJ, but if a Date is passed in the chronology will be ISO.
The recognised object types are defined in
ConverterManager
and
include ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.dateTimeParser()
.
instant
- the datetime object, null means nowzone
- the time zone, null means default time zonejava.lang.IllegalArgumentException
- if the instant is invalidpublic DateTime(java.lang.Object instant, Chronology chronology)
If the chronology is null, ISO in the default time zone is used. Any chronology implied by the object (such as GregorianCalendar does) is ignored.
The recognised object types are defined in
ConverterManager
and
include ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.dateTimeParser()
.
instant
- the datetime object, null means nowchronology
- the chronology, null means ISO in default zonejava.lang.IllegalArgumentException
- if the instant is invalidpublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour)
ISOChronology
in the default time zone.year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59public DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, DateTimeZone zone)
ISOChronology
in the specified time zone.
If the specified time zone is null, the default zone is used.
year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59zone
- the time zone, null means default time zonepublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, Chronology chronology)
If the chronology is null, ISOChronology
in the default time zone is used.
year
- the year, valid values defined by the chronologymonthOfYear
- the month of the year, valid values defined by the chronologydayOfMonth
- the day of the month, valid values defined by the chronologyhourOfDay
- the hour of the day, valid values defined by the chronologyminuteOfHour
- the minute of the hour, valid values defined by the chronologychronology
- the chronology, null means ISOChronology in default zonepublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
ISOChronology
in the default time zone.year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59secondOfMinute
- the second of the minute, from 0 to 59public DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, DateTimeZone zone)
ISOChronology
in the specified time zone.
If the specified time zone is null, the default zone is used.
year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59secondOfMinute
- the second of the minute, from 0 to 59zone
- the time zone, null means default time zonepublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, Chronology chronology)
If the chronology is null, ISOChronology
in the default time zone is used.
year
- the year, valid values defined by the chronologymonthOfYear
- the month of the year, valid values defined by the chronologydayOfMonth
- the day of the month, valid values defined by the chronologyhourOfDay
- the hour of the day, valid values defined by the chronologyminuteOfHour
- the minute of the hour, valid values defined by the chronologysecondOfMinute
- the second of the minute, valid values defined by the chronologychronology
- the chronology, null means ISOChronology in default zonepublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
ISOChronology
in the default time zone.year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59secondOfMinute
- the second of the minute, from 0 to 59millisOfSecond
- the millisecond of the second, from 0 to 999public DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond, DateTimeZone zone)
ISOChronology
in the specified time zone.
If the specified time zone is null, the default zone is used.
year
- the yearmonthOfYear
- the month of the year, from 1 to 12dayOfMonth
- the day of the month, from 1 to 31hourOfDay
- the hour of the day, from 0 to 23minuteOfHour
- the minute of the hour, from 0 to 59secondOfMinute
- the second of the minute, from 0 to 59millisOfSecond
- the millisecond of the second, from 0 to 999zone
- the time zone, null means default time zonepublic DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond, Chronology chronology)
If the chronology is null, ISOChronology
in the default time zone is used.
year
- the year, valid values defined by the chronologymonthOfYear
- the month of the year, valid values defined by the chronologydayOfMonth
- the day of the month, valid values defined by the chronologyhourOfDay
- the hour of the day, valid values defined by the chronologyminuteOfHour
- the minute of the hour, valid values defined by the chronologysecondOfMinute
- the second of the minute, valid values defined by the chronologymillisOfSecond
- the millisecond of the second, valid values defined by the chronologychronology
- the chronology, null means ISOChronology in default zonepublic static DateTime now()
DateTime
set to the current system millisecond time
using ISOChronology
in the default time zone.public static DateTime now(DateTimeZone zone)
DateTime
set to the current system millisecond time
using ISOChronology
in the specified time zone.zone
- the time zone, not nullpublic static DateTime now(Chronology chronology)
DateTime
set to the current system millisecond time
using the specified chronology.chronology
- the chronology, not nullpublic static DateTime parse(java.lang.String str)
DateTime
from the specified string.
This uses ISODateTimeFormat.dateTimeParser()
.withOffsetParsed()
which is different to passing a String
to the constructor.
Sometimes this method and new DateTime(str)
return different results.
This can be confusing as the difference is not visible in AbstractDateTime.toString()
.
When passed a date-time string without an offset, such as '2010-06-30T01:20',
both the constructor and this method use the default time-zone.
As such, DateTime.parse("2010-06-30T01:20")
and
new DateTime("2010-06-30T01:20"))
are equal.
However, when this method is passed a date-time string with an offset,
the offset is directly parsed and stored.
As such, DateTime.parse("2010-06-30T01:20+02:00")
and
new DateTime("2010-06-30T01:20+02:00"))
are NOT equal.
The object produced via this method has a zone of DateTimeZone.forOffsetHours(2)
.
The object produced via the constructor has a zone of DateTimeZone.getDefault()
.
str
- the string to parse, not nullpublic static DateTime parse(java.lang.String str, DateTimeFormatter formatter)
DateTime
from the specified string using a formatter.str
- the string to parse, not nullformatter
- the formatter to use, not nullpublic DateTime toDateTime()
this
.toDateTime
in interface ReadableDateTime
toDateTime
in class AbstractInstant
this
public DateTime toDateTimeISO()
this
if possible.toDateTimeISO
in class AbstractInstant
public DateTime toDateTime(DateTimeZone zone)
this
if possible.toDateTime
in class AbstractInstant
zone
- time zone to apply, or default if nullpublic DateTime toDateTime(Chronology chronology)
this
if possible.toDateTime
in class AbstractInstant
chronology
- chronology to apply, or ISOChronology if nullpublic DateTime withMillis(long newMillis)
The returned object will be either be a new instance or this
.
Only the millis will change, the chronology and time zone are kept.
newMillis
- the new millis, from 1970-01-01T00:00:00Zpublic DateTime withChronology(Chronology newChronology)
The returned object will be either be a new instance or this
.
Only the chronology will change, the millis are kept.
newChronology
- the new chronology, null means ISO defaultpublic DateTime withZone(DateTimeZone newZone)
This method is useful for finding the local time in another timezone. For example, if this instant holds 12:30 in Europe/London, the result from this method with Europe/Paris would be 13:30.
The returned object will be a new instance of the same implementation type.
This method changes the time zone, and does not change the
millisecond instant, with the effect that the field values usually change.
The returned object will be either be a new instance or this
.
newZone
- the new time zonewithZoneRetainFields(org.joda.time.DateTimeZone)
public DateTime withZoneRetainFields(DateTimeZone newZone)
This method is useful for finding the millisecond time in another timezone. For example, if this instant holds 12:30 in Europe/London (ie. 12:30Z), the result from this method with Europe/Paris would be 12:30 (ie. 11:30Z).
The returned object will be a new instance of the same implementation type.
This method changes the time zone and the millisecond instant to keep
the field values the same.
The returned object will be either be a new instance or this
.
newZone
- the new time zone, null means defaultwithZone(org.joda.time.DateTimeZone)
public DateTime withEarlierOffsetAtOverlap()
This method only has any effect when the local time-line overlaps, such as at an autumn daylight savings cutover. In this scenario, there are two valid offsets for the local date-time. Calling this method will return a date-time with the earlier of the two selected.
If this method is called when it is not an overlap, this is returned.
This instance is immutable and unaffected by this method call.
public DateTime withLaterOffsetAtOverlap()
This method only has any effect when the local time-line overlaps, such as at an autumn daylight savings cutover. In this scenario, there are two valid offsets for the local date-time. Calling this method will return a date-time with the later of the two selected.
If this method is called when it is not an overlap, this is returned.
This instance is immutable and unaffected by this method call.
public DateTime withDate(int year, int monthOfYear, int dayOfMonth)
If the date is already the date passed in, then this
is returned.
To set a single field use the properties, for example:
DateTime set = monthOfYear().setCopy(6);
If the time is invalid on the new date due to the time-zone, the time will be adjusted.
This instance is immutable and unaffected by this method call.
year
- the new year valuemonthOfYear
- the new monthOfYear valuedayOfMonth
- the new dayOfMonth valuejava.lang.IllegalArgumentException
- if any value if invalidpublic DateTime withDate(LocalDate date)
If the time is invalid on the new date due to the time-zone, the time will be adjusted.
This instance is immutable and unaffected by this method call.
date
- the local datejava.lang.IllegalArgumentException
- if the time-of-day is invalid for the datejava.lang.NullPointerException
- if the date is nullpublic DateTime withTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
If the time is already the time passed in, then this
is returned.
To set a single field use the properties, for example:
DateTime set = dt.hourOfDay().setCopy(6);
If the new time is invalid due to the time-zone, the time will be adjusted.
This instance is immutable and unaffected by this method call.
hourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the secondjava.lang.IllegalArgumentException
- if any value if invalidpublic DateTime withTime(LocalTime time)
If the new time is invalid due to the time-zone, the time will be adjusted.
This instance is immutable and unaffected by this method call.
time
- the local timejava.lang.IllegalArgumentException
- if the time-of-day is invalid for the datejava.lang.NullPointerException
- if the time is nullpublic DateTime withTimeAtStartOfDay()
The time will normally be midnight, as that is the earliest time on any given day. However, in some time zones when Daylight Savings Time starts, there is no midnight because time jumps from 11:59 to 01:00. This method handles that situation by returning 01:00 on that date.
This instance is immutable and unaffected by this method call.
public DateTime withFields(ReadablePartial partial)
For example, if the partial is a TimeOfDay
then the time fields
would be changed in the returned instance.
If the partial is null, then this
is returned.
partial
- the partial set of fields to apply to this datetime, null ignoredjava.lang.IllegalArgumentException
- if any value is invalidpublic DateTime withField(DateTimeFieldType fieldType, int value)
For example, if the field type is hourOfDay
then the hour of day
field would be changed in the returned instance.
If the field type is null, then this
is returned.
These three lines are equivalent:
DateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6); DateTime updated = dt.dayOfMonth().setCopy(6); DateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
fieldType
- the field type to set, not nullvalue
- the value to setjava.lang.IllegalArgumentException
- if the value is null or invalidpublic DateTime withFieldAdded(DurationFieldType fieldType, int amount)
If the addition is zero or the field is null, then this
is returned.
These three lines are equivalent:
DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6); DateTime added = dt.plusYears(6); DateTime added = dt.plus(Period.years(6));
fieldType
- the field type to add to, not nullamount
- the amount to addjava.lang.IllegalArgumentException
- if the value is null or invalidjava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime withDurationAdded(long durationToAdd, int scalar)
If the addition is zero, then this
is returned.
durationToAdd
- the duration to add to this onescalar
- the amount of times to add, such as -1 to subtract oncejava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime withDurationAdded(ReadableDuration durationToAdd, int scalar)
If the addition is zero, then this
is returned.
durationToAdd
- the duration to add to this one, null means zeroscalar
- the amount of times to add, such as -1 to subtract oncejava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime withPeriodAdded(ReadablePeriod period, int scalar)
If the addition is zero, then this
is returned.
This method is typically used to add multiple copies of complex
period instances. Adding one field is best achieved using methods
like withFieldAdded(DurationFieldType, int)
or plusYears(int)
.
period
- the period to add to this one, null means zeroscalar
- the amount of times to add, such as -1 to subtract oncejava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime plus(long duration)
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
duration
- the duration, in millis, to add to this onejava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime plus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
duration
- the duration to add to this one, null means zerojava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime plus(ReadablePeriod period)
This method will add each element of the period one by one, from largest to smallest, adjusting the datetime to be accurate between each.
Thus, adding a period of one month and one day to 2007-03-31 will work as follows: First add one month and adjust, resulting in 2007-04-30 Then add one day and adjust, resulting in 2007-05-01.
This method is typically used to add complex period instances.
Adding one field is best achieved using methods
like plusYears(int)
.
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
period
- the duration to add to this one, null means zerojava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime plusYears(int years)
The calculation will do its best to only change the year field retaining the same month of year. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2008-02-29 plus one year cannot result in 2009-02-29, so the day of month is adjusted to 2009-02-28.
The following three lines are identical in effect:
DateTime added = dt.plusYears(6); DateTime added = dt.plus(Period.years(6)); DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
This datetime instance is immutable and unaffected by this method call.
years
- the amount of years to add, may be negativepublic DateTime plusMonths(int months)
The calculation will do its best to only change the month field retaining the same day of month. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2007-03-31 plus one month cannot result in 2007-04-31, so the day of month is adjusted to 2007-04-30.
The following three lines are identical in effect:
DateTime added = dt.plusMonths(6); DateTime added = dt.plus(Period.months(6)); DateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);
This datetime instance is immutable and unaffected by this method call.
months
- the amount of months to add, may be negativepublic DateTime plusWeeks(int weeks)
The calculation operates as if it were adding the equivalent in days.
The following three lines are identical in effect:
DateTime added = dt.plusWeeks(6); DateTime added = dt.plus(Period.weeks(6)); DateTime added = dt.withFieldAdded(DurationFieldType.weeks(), 6);
This datetime instance is immutable and unaffected by this method call.
weeks
- the amount of weeks to add, may be negativepublic DateTime plusDays(int days)
The calculation will do its best to only change the day field retaining the same time of day. However, in certain circumstances, typically daylight savings cutover, it may be necessary to alter the time fields.
In spring an hour is typically removed. If adding one day results in the time being within the cutover then the time is adjusted to be within summer time. For example, if the cutover is from 01:59 to 03:00 and the result of this method would have been 02:30, then the result will be adjusted to 03:30.
The following three lines are identical in effect:
DateTime added = dt.plusDays(6); DateTime added = dt.plus(Period.days(6)); DateTime added = dt.withFieldAdded(DurationFieldType.days(), 6);
This datetime instance is immutable and unaffected by this method call.
days
- the amount of days to add, may be negativepublic DateTime plusHours(int hours)
The calculation will add a duration equivalent to the number of hours expressed in milliseconds.
For example, if a spring daylight savings cutover is from 01:59 to 03:00 then adding one hour to 01:30 will result in 03:30. This is a duration of one hour later, even though the hour field value changed from 1 to 3.
The following three lines are identical in effect:
DateTime added = dt.plusHours(6); DateTime added = dt.plus(Period.hours(6)); DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6);
This datetime instance is immutable and unaffected by this method call.
hours
- the amount of hours to add, may be negativepublic DateTime plusMinutes(int minutes)
The calculation will add a duration equivalent to the number of minutes expressed in milliseconds.
The following three lines are identical in effect:
DateTime added = dt.plusMinutes(6); DateTime added = dt.plus(Period.minutes(6)); DateTime added = dt.withFieldAdded(DurationFieldType.minutes(), 6);
This datetime instance is immutable and unaffected by this method call.
minutes
- the amount of minutes to add, may be negativepublic DateTime plusSeconds(int seconds)
The calculation will add a duration equivalent to the number of seconds expressed in milliseconds.
The following three lines are identical in effect:
DateTime added = dt.plusSeconds(6); DateTime added = dt.plus(Period.seconds(6)); DateTime added = dt.withFieldAdded(DurationFieldType.seconds(), 6);
This datetime instance is immutable and unaffected by this method call.
seconds
- the amount of seconds to add, may be negativepublic DateTime plusMillis(int millis)
The calculation will add a duration equivalent to the number of milliseconds.
The following three lines are identical in effect:
DateTime added = dt.plusMillis(6); DateTime added = dt.plus(Period.millis(6)); DateTime added = dt.withFieldAdded(DurationFieldType.millis(), 6);
This datetime instance is immutable and unaffected by this method call.
millis
- the amount of millis to add, may be negativepublic DateTime minus(long duration)
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
duration
- the duration, in millis, to reduce this instant byjava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime minus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
duration
- the duration to reduce this instant byjava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime minus(ReadablePeriod period)
This method will subtract each element of the period one by one, from largest to smallest, adjusting the datetime to be accurate between each.
Thus, subtracting a period of one month and one day from 2007-05-31 will work as follows: First subtract one month and adjust, resulting in 2007-04-30 Then subtract one day and adjust, resulting in 2007-04-29. Note that the day has been adjusted by two.
This method is typically used to subtract complex period instances.
Subtracting one field is best achieved using methods
like minusYears(int)
.
If the amount is zero or null, then this
is returned.
This datetime instance is immutable and unaffected by this method call.
period
- the period to reduce this instant byjava.lang.ArithmeticException
- if the new datetime exceeds the capacity of a longpublic DateTime minusYears(int years)
The calculation will do its best to only change the year field retaining the same month of year. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2008-02-29 minus one year cannot result in 2007-02-29, so the day of month is adjusted to 2007-02-28.
The following three lines are identical in effect:
DateTime subtracted = dt.minusYears(6); DateTime subtracted = dt.minus(Period.years(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.years(), -6);
This datetime instance is immutable and unaffected by this method call.
years
- the amount of years to subtract, may be negativepublic DateTime minusMonths(int months)
The calculation will do its best to only change the month field retaining the same day of month. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2007-05-31 minus one month cannot result in 2007-04-31, so the day of month is adjusted to 2007-04-30.
The following three lines are identical in effect:
DateTime subtracted = dt.minusMonths(6); DateTime subtracted = dt.minus(Period.months(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.months(), -6);
This datetime instance is immutable and unaffected by this method call.
months
- the amount of months to subtract, may be negativepublic DateTime minusWeeks(int weeks)
The calculation operates as if it were subtracting the equivalent in days.
The following three lines are identical in effect:
DateTime subtracted = dt.minusWeeks(6); DateTime subtracted = dt.minus(Period.weeks(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.weeks(), -6);
This datetime instance is immutable and unaffected by this method call.
weeks
- the amount of weeks to subtract, may be negativepublic DateTime minusDays(int days)
The calculation will do its best to only change the day field retaining the same time of day. However, in certain circumstances, typically daylight savings cutover, it may be necessary to alter the time fields.
In spring an hour is typically removed. If subtracting one day results in the time being within the cutover then the time is adjusted to be within summer time. For example, if the cutover is from 01:59 to 03:00 and the result of this method would have been 02:30, then the result will be adjusted to 03:30.
The following three lines are identical in effect:
DateTime subtracted = dt.minusDays(6); DateTime subtracted = dt.minus(Period.days(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6);
This datetime instance is immutable and unaffected by this method call.
days
- the amount of days to subtract, may be negativepublic DateTime minusHours(int hours)
The calculation will subtract a duration equivalent to the number of hours expressed in milliseconds.
For example, if a spring daylight savings cutover is from 01:59 to 03:00 then subtracting one hour from 03:30 will result in 01:30. This is a duration of one hour earlier, even though the hour field value changed from 3 to 1.
The following three lines are identical in effect:
DateTime subtracted = dt.minusHours(6); DateTime subtracted = dt.minus(Period.hours(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);
This datetime instance is immutable and unaffected by this method call.
hours
- the amount of hours to subtract, may be negativepublic DateTime minusMinutes(int minutes)
The calculation will subtract a duration equivalent to the number of minutes expressed in milliseconds.
The following three lines are identical in effect:
DateTime subtracted = dt.minusMinutes(6); DateTime subtracted = dt.minus(Period.minutes(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.minutes(), -6);
This datetime instance is immutable and unaffected by this method call.
minutes
- the amount of minutes to subtract, may be negativepublic DateTime minusSeconds(int seconds)
The calculation will subtract a duration equivalent to the number of seconds expressed in milliseconds.
The following three lines are identical in effect:
DateTime subtracted = dt.minusSeconds(6); DateTime subtracted = dt.minus(Period.seconds(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6);
This datetime instance is immutable and unaffected by this method call.
seconds
- the amount of seconds to subtract, may be negativepublic DateTime minusMillis(int millis)
The calculation will subtract a duration equivalent to the number of milliseconds.
The following three lines are identical in effect:
DateTime subtracted = dt.minusMillis(6); DateTime subtracted = dt.minus(Period.millis(6)); DateTime subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6);
This datetime instance is immutable and unaffected by this method call.
millis
- the amount of millis to subtract, may be negativepublic DateTime.Property property(DateTimeFieldType type)
type
- the field type to get the chronology forjava.lang.IllegalArgumentException
- if the field is null or unsupported@Deprecated public DateMidnight toDateMidnight()
DateMidnight
using the
same millis and chronology.@Deprecated public YearMonthDay toYearMonthDay()
YearMonthDay
using the
same millis and chronology.@Deprecated public TimeOfDay toTimeOfDay()
TimeOfDay
using the
same millis and chronology.public LocalDateTime toLocalDateTime()
LocalDateTime
with
the same datetime and chronology.public LocalDate toLocalDate()
LocalDate
with the
same date and chronology.public LocalTime toLocalTime()
LocalTime
with the
same time and chronology.public DateTime withEra(int era)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of era changed.
era
- the era to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withCenturyOfEra(int centuryOfEra)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of century of era changed.
centuryOfEra
- the centurey of era to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withYearOfEra(int yearOfEra)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of year of era changed.
yearOfEra
- the year of era to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withYearOfCentury(int yearOfCentury)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of year of century changed.
yearOfCentury
- the year of century to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withYear(int year)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of year changed.
year
- the year to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withWeekyear(int weekyear)
The weekyear is the year that matches with the weekOfWeekyear field. In the standard ISO8601 week algorithm, the first week of the year is that in which at least 4 days are in the year. As a result of this definition, day 1 of the first week may be in the previous year. The weekyear allows you to query the effective year for that day.
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of weekyear changed.
weekyear
- the weekyear to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withMonthOfYear(int monthOfYear)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of month of year changed.
monthOfYear
- the month of year to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withWeekOfWeekyear(int weekOfWeekyear)
This field is associated with the "weekyear" via withWeekyear(int)
.
In the standard ISO8601 week algorithm, the first week of the year
is that in which at least 4 days are in the year. As a result of this
definition, day 1 of the first week may be in the previous year.
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of week of weekyear changed.
weekOfWeekyear
- the week of weekyear to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withDayOfYear(int dayOfYear)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of day of year changed.
dayOfYear
- the day of year to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withDayOfMonth(int dayOfMonth)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of day of month changed.
dayOfMonth
- the day of month to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withDayOfWeek(int dayOfWeek)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of day of week changed.
dayOfWeek
- the day of week to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withHourOfDay(int hour)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of hour of day changed.
hour
- the hour of day to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withMinuteOfHour(int minute)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of minute of hour changed.
minute
- the minute of hour to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withSecondOfMinute(int second)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of second of minute changed.
second
- the second of minute to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withMillisOfSecond(int millis)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of millis of second changed.
millis
- the millis of second to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime withMillisOfDay(int millis)
DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of millis of day changed.
millis
- the millis of day to setjava.lang.IllegalArgumentException
- if the value is invalidpublic DateTime.Property era()
public DateTime.Property centuryOfEra()
public DateTime.Property yearOfCentury()
public DateTime.Property yearOfEra()
public DateTime.Property year()
public DateTime.Property weekyear()
public DateTime.Property monthOfYear()
public DateTime.Property weekOfWeekyear()
public DateTime.Property dayOfYear()
public DateTime.Property dayOfMonth()
public DateTime.Property dayOfWeek()
public DateTime.Property hourOfDay()
public DateTime.Property minuteOfDay()
public DateTime.Property minuteOfHour()
public DateTime.Property secondOfDay()
public DateTime.Property secondOfMinute()
public DateTime.Property millisOfDay()
public DateTime.Property millisOfSecond()
Copyright © 2010 - 2020 Adobe. All Rights Reserved