MutableDateTime is the standard implementation of a modifiable datetime class.
It holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.
This class uses a Chronology internally. The Chronology 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 compatable with the modern Gregorian calendar.
Each individual field can be accessed in two ways:
getHourOfDay()
hourOfDay().get()
The second technique also provides access to other useful methods on the
field:
- get numeric value
- set numeric value
- add to numeric value
- add to numeric value wrapping with the field
- get text vlaue
- get short text value
- set text value
- field maximum value
- field minimum value
MutableDateTime is mutable and not thread-safe, unless concurrent threads
are not invoking mutator methods.
MutableDateTime
public MutableDateTime()
Constructs an instance set to the current system millisecond time
using ISOChronology
in the default time zone.
MutableDateTime
public MutableDateTime(Object instant)
Constructs an instance from an Object that represents a datetime.
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.
instant
- the datetime object, null means now
MutableDateTime
public MutableDateTime(Object instant,
Chronology chronology)
Constructs an instance from an Object that represents a datetime,
using the specified 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.
instant
- the datetime object, null means nowchronology
- the chronology, null means ISOChronology in default zone
MutableDateTime
public MutableDateTime(Object instant,
DateTimeZone zone)
Constructs an instance from an Object that represents a datetime,
forcing the time zone to that specified.
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.
instant
- the datetime object, null means nowzone
- the time zone, null means default time zone
MutableDateTime
public MutableDateTime(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.
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the monthhourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the second
MutableDateTime
public MutableDateTime(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.
If the chronology is null,
ISOChronology
in the default time zone is used.
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the monthhourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the secondchronology
- the chronology, null means ISOChronology in default zone
MutableDateTime
public MutableDateTime(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.
If the specified time zone is null, the default zone is used.
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the monthhourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the secondzone
- the time zone, null means default time zone
MutableDateTime
public MutableDateTime(long instant)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using ISOChronology
in the default time zone.
instant
- the milliseconds from 1970-01-01T00:00:00Z
MutableDateTime
public MutableDateTime(long instant,
Chronology chronology)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using the specified 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 zone
MutableDateTime
public MutableDateTime(long instant,
DateTimeZone zone)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
using
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 zone
MutableDateTime
public MutableDateTime(Chronology chronology)
Constructs an instance set to the current system millisecond time
using the specified chronology.
If the chronology is null,
ISOChronology
in the default time zone is used.
chronology
- the chronology, null means ISOChronology in default zone
MutableDateTime
public MutableDateTime(DateTimeZone zone)
Constructs an instance set to the current system millisecond time
using
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 zone
add
public void add(long duration)
Add an amount of time to the datetime.
- add in interface ReadWritableInstant
duration
- the millis to add
add
public void add(DurationFieldType type,
int amount)
Adds to the instant specifying the duration and multiple to add.
- add in interface ReadWritableInstant
type
- a field type, usually obtained from DateTimeFieldType, not nullamount
- the amount to add of this duration
add
public void add(ReadableDuration duration)
Adds a duration to this instant.
This will typically change the value of most fields.
- add in interface ReadWritableInstant
duration
- the duration to add, null means add zero
add
public void add(ReadableDuration duration,
int scalar)
Adds a duration to this instant specifying how many times to add.
This will typically change the value of most fields.
- add in interface ReadWritableInstant
duration
- the duration to add, null means add zeroscalar
- direction and amount to add, which may be negative
add
public void add(ReadablePeriod period)
Adds a period to this instant.
This will typically change the value of most fields.
- add in interface ReadWritableInstant
period
- the period to add, null means add zero
add
public void add(ReadablePeriod period,
int scalar)
Adds a period to this instant specifying how many times to add.
This will typically change the value of most fields.
- add in interface ReadWritableInstant
period
- the period to add, null means add zeroscalar
- direction and amount to add, which may be negative
addMillis
public void addMillis(int millis)
Add a number of milliseconds to the date. The implementation of this
method differs from the
add(long)
method in that a
DateTimeField performs the addition.
- addMillis in interface ReadWritableDateTime
millis
- the milliseconds to add
addMinutes
public void addMinutes(int minutes)
Add a number of minutes to the date.
- addMinutes in interface ReadWritableDateTime
minutes
- the minutes to add
addMonths
public void addMonths(int months)
Add a number of months to the date.
- addMonths in interface ReadWritableDateTime
months
- the months to add
addSeconds
public void addSeconds(int seconds)
Add a number of seconds to the date.
- addSeconds in interface ReadWritableDateTime
seconds
- the seconds to add
addWeekyears
public void addWeekyears(int weekyears)
Add a number of weekyears to the date.
- addWeekyears in interface ReadWritableDateTime
weekyears
- the weekyears to add
clone
public Object clone()
Clone this object.
copy
public MutableDateTime copy()
Clone this object without having to cast the returned object.
- a clone of the this object.
getRoundingField
public DateTimeField getRoundingField()
Gets the field used for rounding this instant, returning null if rounding
is not enabled.
getRoundingMode
public int getRoundingMode()
Gets the rounding mode for this instant, returning ROUND_NONE if rounding
is not enabled.
- the rounding mode constant
millisOfSecond
public MutableDateTime.Property millisOfSecond()
Get the millis of second property
- the millis of second property
minuteOfHour
public MutableDateTime.Property minuteOfHour()
Get the minute of hour field property
- the minute of hour property
property
public MutableDateTime.Property property(DateTimeFieldType type)
Gets the property object for the specified type, which contains many useful methods.
type
- the field type to get the chronology for
secondOfMinute
public MutableDateTime.Property secondOfMinute()
Get the second of minute field property
- the second of minute property
set
public void set(DateTimeFieldType type,
int value)
Sets the value of one of the fields of the instant, such as hourOfDay.
- set in interface ReadWritableInstant
type
- a field type, usually obtained from DateTimeFieldType, not nullvalue
- the value to set the field to
setDate
public void setDate(int year,
int monthOfYear,
int dayOfMonth)
Set the date from fields.
The time part of this object will be unaffected.
- setDate in interface ReadWritableDateTime
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the month
setDate
public void setDate(long instant)
Set the date from milliseconds.
The time part of this object will be unaffected.
instant
- an instant to copy the date from, time part ignored
setDate
public void setDate(ReadableInstant instant)
Set the date from another instant.
The time part of this object will be unaffected.
instant
- an instant to copy the date from, time part ignored
setDateTime
public void setDateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Set the date and time from fields.
- setDateTime in interface ReadWritableDateTime
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the monthhourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the second
setDayOfMonth
public void setDayOfMonth(int dayOfMonth)
Set the day of the month to the specified value.
- setDayOfMonth in interface ReadWritableDateTime
dayOfMonth
- the day of the month
setDayOfWeek
public void setDayOfWeek(int dayOfWeek)
Set the day of week to the specified value.
- setDayOfWeek in interface ReadWritableDateTime
dayOfWeek
- the day of the week
setDayOfYear
public void setDayOfYear(int dayOfYear)
Set the day of year to the specified value.
- setDayOfYear in interface ReadWritableDateTime
dayOfYear
- the day of the year
setHourOfDay
public void setHourOfDay(int hourOfDay)
Set the hour of the day to the specified value.
- setHourOfDay in interface ReadWritableDateTime
hourOfDay
- the hour of day
setMillis
public void setMillis(long instant)
Set the milliseconds of the datetime.
All changes to the millisecond field occurs via this method.
- setMillis in interface ReadWritableInstant
- setMillis in interface BaseDateTime
instant
- the milliseconds since 1970-01-01T00:00:00Z to set the
datetime to
setMillis
public void setMillis(ReadableInstant instant)
Sets the millisecond instant of this instant from another.
This method does not change the chronology of this instant, just the
millisecond instant.
- setMillis in interface ReadWritableInstant
instant
- the instant to use, null means now
setMillisOfDay
public void setMillisOfDay(int millisOfDay)
Set the millis of the day to the specified value.
- setMillisOfDay in interface ReadWritableDateTime
millisOfDay
- the millis of day
setMillisOfSecond
public void setMillisOfSecond(int millisOfSecond)
Set the millis of the second to the specified value.
- setMillisOfSecond in interface ReadWritableDateTime
millisOfSecond
- the millis of second
setMinuteOfDay
public void setMinuteOfDay(int minuteOfDay)
Set the minute of the day to the specified value.
- setMinuteOfDay in interface ReadWritableDateTime
minuteOfDay
- the minute of day
setMinuteOfHour
public void setMinuteOfHour(int minuteOfHour)
Set the minute of the hour to the specified value.
- setMinuteOfHour in interface ReadWritableDateTime
minuteOfHour
- the minute of hour
setMonthOfYear
public void setMonthOfYear(int monthOfYear)
Set the month of the year to the specified value.
- setMonthOfYear in interface ReadWritableDateTime
monthOfYear
- the month of the year
setRounding
public void setRounding(DateTimeField field)
Sets the status of rounding to use the specified field and ROUND_FLOOR mode.
A null field will disable rounding.
Once set, the instant is then rounded using the new field and mode.
Enabling rounding will cause all subsequent calls to
setMillis(long)
to be rounded. This can be used to control the precision of the instant,
for example by setting a rounding field of minuteOfDay, the seconds and
milliseconds will always be zero.
field
- rounding field or null to disable
setRounding
public void setRounding(DateTimeField field,
int mode)
Sets the status of rounding to use the specified field and mode.
A null field or mode of ROUND_NONE will disable rounding.
Once set, the instant is then rounded using the new field and mode.
Enabling rounding will cause all subsequent calls to
setMillis(long)
to be rounded. This can be used to control the precision of the instant,
for example by setting a rounding field of minuteOfDay, the seconds and
milliseconds will always be zero.
field
- rounding field or null to disablemode
- rounding mode or ROUND_NONE to disable
setSecondOfDay
public void setSecondOfDay(int secondOfDay)
Set the second of the day to the specified value.
- setSecondOfDay in interface ReadWritableDateTime
secondOfDay
- the second of day
setSecondOfMinute
public void setSecondOfMinute(int secondOfMinute)
Set the second of the minute to the specified value.
- setSecondOfMinute in interface ReadWritableDateTime
secondOfMinute
- the second of minute
setTime
public void setTime(int hour,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Set the time from fields.
The date part of this object will be unaffected.
- setTime in interface ReadWritableDateTime
hour
- the hourminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the second
setTime
public void setTime(long millis)
Set the time from milliseconds.
The date part of this object will be unaffected.
millis
- an instant to copy the time from, date part ignored
setTime
public void setTime(ReadableInstant instant)
Set the time from another instant.
The date part of this object will be unaffected.
instant
- an instant to copy the time from, date part ignored
setWeekOfWeekyear
public void setWeekOfWeekyear(int weekOfWeekyear)
Set the week of weekyear to the specified value.
- setWeekOfWeekyear in interface ReadWritableDateTime
weekOfWeekyear
- the week of the weekyear
setZone
public void setZone(DateTimeZone newZone)
Sets the time zone of the datetime, changing the chronology and field values.
Changing the zone using this method retains the millisecond instant.
The millisecond instant is adjusted in the new zone to compensate.
chronology. Setting the time zone does not affect the millisecond value
of this instant.
If the chronology already has this time zone, no change occurs.
- setZone in interface ReadWritableInstant
newZone
- the time zone to use, null means default zone
setZoneRetainFields
public void setZoneRetainFields(DateTimeZone newZone)
Sets the time zone of the datetime, changing the chronology and millisecond.
Changing the zone using this method retains the field values.
The millisecond instant is adjusted in the new zone to compensate.
If the chronology already has this time zone, no change occurs.
- setZoneRetainFields in interface ReadWritableInstant
newZone
- the time zone to use, null means default zone
weekOfWeekyear
public MutableDateTime.Property weekOfWeekyear()
Get the week of a week based year property.
- the week of a week based year property
weekyear
public MutableDateTime.Property weekyear()
Get the year of a week based year property.
- the year of a week based year property