Class YearMonthDay
- All Implemented Interfaces:
Serializable
,Comparable<ReadablePartial>
,ReadablePartial
NOTE: This class only supports the three fields listed above. Thus, you
cannot query the dayOfWeek or centuryOfEra fields for example.
The new LocalDate
class removes this restriction.
Calculations on YearMonthDay are performed using a Chronology
.
This chronology is set to be in the UTC time zone for all calculations.
Each individual field can be queried in two ways:
getMonthOfYear()
monthOfYear().get()
- numeric value -
monthOfYear().get()
- text value -
monthOfYear().getAsText()
- short text value -
monthOfYear().getAsShortText()
- maximum/minimum values -
monthOfYear().getMaximumValue()
- add/subtract -
monthOfYear().addToCopy()
- set -
monthOfYear().setCopy()
YearMonthDay is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.
- Since:
- 1.0
- Author:
- Stephen Colebourne
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Deprecated.Use LocalDate which has a much better internal implementation -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Deprecated.The index of the dayOfMonth field in the field arraystatic final int
Deprecated.The index of the monthOfYear field in the field arraystatic final int
Deprecated.The index of the year field in the field array -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Constructs a YearMonthDay with the current date, using ISOChronology in the default zone to extract the fields.YearMonthDay
(int year, int monthOfYear, int dayOfMonth) Deprecated.Constructs a YearMonthDay with specified time field values usingISOChronology
in the default zone.YearMonthDay
(int year, int monthOfYear, int dayOfMonth, Chronology chronology) Deprecated.Constructs a YearMonthDay with specified time field values.YearMonthDay
(long instant) Deprecated.Constructs a YearMonthDay extracting the partial fields from the specified milliseconds using the ISOChronology in the default zone.YearMonthDay
(long instant, Chronology chronology) Deprecated.Constructs a YearMonthDay extracting the partial fields from the specified milliseconds using the chronology provided.YearMonthDay
(Object instant) Deprecated.Constructs a YearMonthDay from an Object that represents a time.YearMonthDay
(Object instant, Chronology chronology) Deprecated.Constructs a YearMonthDay from an Object that represents a time, using the specified chronology.YearMonthDay
(Chronology chronology) Deprecated.Constructs a YearMonthDay with the current date, using the specified chronology and zone to extract the fields.YearMonthDay
(DateTimeZone zone) Deprecated.Constructs a YearMonthDay with the current date, using ISOChronology in the specified zone to extract the fields. -
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Get the day of month field property which provides access to advanced functionality.static YearMonthDay
fromCalendarFields
(Calendar calendar) Deprecated.Constructs a YearMonthDay from ajava.util.Calendar
using exactly the same field values avoiding any time zone effects.static YearMonthDay
fromDateFields
(Date date) Deprecated.Constructs a YearMonthDay from ajava.util.Date
using exactly the same field values avoiding any time zone effects.int
Deprecated.Get the day of month field value.protected DateTimeField
getField
(int index, Chronology chrono) Deprecated.Gets the field for a specific index in the chronology specified.getFieldType
(int index) Deprecated.Gets the field type at the specified index.Deprecated.Gets an array of the field type of each of the fields that this partial supports.int
Deprecated.Get the month of year field value.int
getYear()
Deprecated.Get the year field value.minus
(ReadablePeriod period) Deprecated.Returns a copy of this date with the specified period taken away.minusDays
(int days) Deprecated.Returns a copy of this date minus the specified number of days.minusMonths
(int months) Deprecated.Returns a copy of this date minus the specified number of months.minusYears
(int years) Deprecated.Returns a copy of this date minus the specified number of years.Deprecated.Get the month of year field property which provides access to advanced functionality.plus
(ReadablePeriod period) Deprecated.Returns a copy of this date with the specified period added.plusDays
(int days) Deprecated.Returns a copy of this date plus the specified number of days.plusMonths
(int months) Deprecated.Returns a copy of this date plus the specified number of months.plusYears
(int years) Deprecated.Returns a copy of this date plus the specified number of years.property
(DateTimeFieldType type) Deprecated.Gets the property object for the specified type, which contains many useful methods.int
size()
Deprecated.Gets the number of fields in this partial.Deprecated.Converts this object to a DateMidnight in the default time zone.toDateMidnight
(DateTimeZone zone) Deprecated.Converts this object to a DateMidnight.toDateTime
(TimeOfDay time) Deprecated.Converts this object to a DateTime using a TimeOfDay to fill in the missing fields and using the default time zone.toDateTime
(TimeOfDay time, DateTimeZone zone) Deprecated.Converts this object to a DateTime using a TimeOfDay to fill in the missing fields.Deprecated.Converts this partial to a full datetime using the default time zone setting the date fields from this instance and the time fields from the current time.Deprecated.Converts this partial to a full datetime using the specified time zone setting the date fields from this instance and the time fields from the current time.Deprecated.Converts this YearMonthDay to a full datetime at midnight using the default time zone.Deprecated.Converts this YearMonthDay to a full datetime at midnight using the specified time zone.Deprecated.Converts this object to an Interval representing the whole day in the default time zone.toInterval
(DateTimeZone zone) Deprecated.Converts this object to an Interval representing the whole day.Deprecated.Converts this object to a LocalDate with the same date and chronology.toString()
Deprecated.Output the date in the ISO8601 format YYYY-MM-DD.withChronologyRetainFields
(Chronology newChronology) Deprecated.Returns a copy of this date with the specified chronology.withDayOfMonth
(int dayOfMonth) Deprecated.Returns a copy of this date with the day of month field updated.withField
(DateTimeFieldType fieldType, int value) Deprecated.Returns a copy of this date with the specified field set to a new value.withFieldAdded
(DurationFieldType fieldType, int amount) Deprecated.Returns a copy of this date with the value of the specified field increased.withMonthOfYear
(int monthOfYear) Deprecated.Returns a copy of this date with the month of year field updated.withPeriodAdded
(ReadablePeriod period, int scalar) Deprecated.Returns a copy of this date with the specified period added.withYear
(int year) Deprecated.Returns a copy of this date with the year field updated.year()
Deprecated.Get the year field property which provides access to advanced functionality.Methods inherited from class org.joda.time.base.BasePartial
getChronology, getValue, getValues, setValue, setValues, toString, toString
Methods inherited from class org.joda.time.base.AbstractPartial
compareTo, equals, get, getField, getFields, hashCode, indexOf, indexOf, indexOfSupported, indexOfSupported, isAfter, isBefore, isEqual, isSupported, toDateTime, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Comparable
compareTo
Methods inherited from interface org.joda.time.ReadablePartial
equals, get, getChronology, getField, getValue, hashCode, isSupported, toDateTime
-
Field Details
-
YEAR
public static final int YEARDeprecated.The index of the year field in the field array- See Also:
-
MONTH_OF_YEAR
public static final int MONTH_OF_YEARDeprecated.The index of the monthOfYear field in the field array- See Also:
-
DAY_OF_MONTH
public static final int DAY_OF_MONTHDeprecated.The index of the dayOfMonth field in the field array- See Also:
-
-
Constructor Details
-
YearMonthDay
public YearMonthDay()Deprecated.Constructs a YearMonthDay with the current date, using ISOChronology in the default zone to extract the fields.The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
-
YearMonthDay
Deprecated.Constructs a YearMonthDay with the current date, using ISOChronology in the specified zone to extract the fields.The constructor uses the specified time zone to obtain the current date. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
zone
- the zone to use, null means default zone- Since:
- 1.1
-
YearMonthDay
Deprecated.Constructs a YearMonthDay with the current date, using the specified chronology and zone to extract the fields.The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
chronology
- the chronology, null means ISOChronology in the default zone
-
YearMonthDay
public YearMonthDay(long instant) Deprecated.Constructs a YearMonthDay extracting the partial fields from the specified milliseconds using the ISOChronology in the default zone.The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
instant
- the milliseconds from 1970-01-01T00:00:00Z
-
YearMonthDay
Deprecated.Constructs a YearMonthDay extracting the partial fields from the specified milliseconds using the chronology provided.The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
instant
- the milliseconds from 1970-01-01T00:00:00Zchronology
- the chronology, null means ISOChronology in the default zone
-
YearMonthDay
Deprecated.Constructs a YearMonthDay from an Object that represents a time.The recognised object types are defined in
ConverterManager
and include ReadableInstant, String, Calendar and Date. The String formats are described byISODateTimeFormat.dateOptionalTimeParser()
.The chronology used will be derived from the object, defaulting to ISO.
NOTE: Prior to v1.3 the string format was described by
ISODateTimeFormat.dateTimeParser()
. Time only strings are now rejected.- Parameters:
instant
- the datetime object, null means now- Throws:
IllegalArgumentException
- if the instant is invalid
-
YearMonthDay
Deprecated.Constructs a YearMonthDay from an Object that represents a time, using the specified chronology.The recognised object types are defined in
ConverterManager
and include ReadableInstant, String, Calendar and Date. The String formats are described byISODateTimeFormat.dateOptionalTimeParser()
.The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC). The specified chronology overrides that of the object.
NOTE: Prior to v1.3 the string format was described by
ISODateTimeFormat.dateTimeParser()
. Time only strings are now rejected.- Parameters:
instant
- the datetime object, null means nowchronology
- the chronology, null means ISO default- Throws:
IllegalArgumentException
- if the instant is invalid
-
YearMonthDay
public YearMonthDay(int year, int monthOfYear, int dayOfMonth) Deprecated.Constructs a YearMonthDay with specified time field values usingISOChronology
in the default zone.The constructor uses the no time zone initialising the fields as provided. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the month
-
YearMonthDay
Deprecated.Constructs a YearMonthDay with specified time field values.The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations are performed without reference to a timezone (by switching to UTC).
- Parameters:
year
- the yearmonthOfYear
- the month of the yeardayOfMonth
- the day of the monthchronology
- the chronology, null means ISOChronology in the default zone
-
-
Method Details
-
fromCalendarFields
Deprecated.Constructs a YearMonthDay from ajava.util.Calendar
using exactly the same field values avoiding any time zone effects.Each field is queried from the Calendar and assigned to the YearMonthDay. This is useful if you have been using the Calendar as a local date, ignoring the zone.
This factory method ignores the type of the calendar and always creates a YearMonthDay with ISO chronology. It is expected that you will only pass in instances of
GregorianCalendar
however this is not validated.- Parameters:
calendar
- the Calendar to extract fields from- Returns:
- the created YearMonthDay
- Throws:
IllegalArgumentException
- if the calendar is nullIllegalArgumentException
- if the date is invalid for the ISO chronology- Since:
- 1.2
-
fromDateFields
Deprecated.Constructs a YearMonthDay from ajava.util.Date
using exactly the same field values avoiding any time zone effects.Each field is queried from the Date and assigned to the YearMonthDay. This is useful if you have been using the Date as a local date, ignoring the zone.
This factory method always creates a YearMonthDay with ISO chronology.
- Parameters:
date
- the Date to extract fields from- Returns:
- the created YearMonthDay
- Throws:
IllegalArgumentException
- if the calendar is nullIllegalArgumentException
- if the date is invalid for the ISO chronology- Since:
- 1.2
-
size
public int size()Deprecated.Gets the number of fields in this partial.- Specified by:
size
in interfaceReadablePartial
- Returns:
- the field count
-
getField
Deprecated.Gets the field for a specific index in the chronology specified.This method must not use any instance variables.
- Specified by:
getField
in classAbstractPartial
- Parameters:
index
- the index to retrievechrono
- the chronology to use- Returns:
- the field
-
getFieldType
Deprecated.Gets the field type at the specified index.- Specified by:
getFieldType
in interfaceReadablePartial
- Overrides:
getFieldType
in classAbstractPartial
- Parameters:
index
- the index to retrieve- Returns:
- the field at the specified index
- Throws:
IndexOutOfBoundsException
- if the index is invalid
-
getFieldTypes
Deprecated.Gets an array of the field type of each of the fields that this partial supports.The fields are returned largest to smallest, Year, Month, Day
- Overrides:
getFieldTypes
in classAbstractPartial
- Returns:
- the array of field types (cloned), largest to smallest
-
withChronologyRetainFields
Deprecated.Returns a copy of this date with the specified chronology. This instance is immutable and unaffected by this method call.This method retains the values of the fields, thus the result will typically refer to a different instant.
The time zone of the specified chronology is ignored, as YearMonthDay operates without a time zone.
- Parameters:
newChronology
- the new chronology, null means ISO- Returns:
- a copy of this datetime with a different chronology
- Throws:
IllegalArgumentException
- if the values are invalid for the new chronology
-
withField
Deprecated.Returns a copy of this date with the specified field set to a new value.For example, if the field type is
dayOfMonth
then the day would be changed in the returned instance.These three lines are equivalent:
YearMonthDay updated = ymd.withField(DateTimeFieldType.dayOfMonth(), 6); YearMonthDay updated = ymd.dayOfMonth().setCopy(6); YearMonthDay updated = ymd.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
- Parameters:
fieldType
- the field type to set, not nullvalue
- the value to set- Returns:
- a copy of this instance with the field set
- Throws:
IllegalArgumentException
- if the value is null or invalid
-
withFieldAdded
Deprecated.Returns a copy of this date with the value of the specified field increased.If the addition is zero, then
this
is returned.These three lines are equivalent:
YearMonthDay added = ymd.withFieldAdded(DurationFieldType.days(), 6); YearMonthDay added = ymd.plusDays(6); YearMonthDay added = ymd.dayOfMonth().addToCopy(6);
- Parameters:
fieldType
- the field type to add to, not nullamount
- the amount to add- Returns:
- a copy of this instance with the field updated
- Throws:
IllegalArgumentException
- if the value is null or invalidArithmeticException
- if the new datetime exceeds the capacity
-
withPeriodAdded
Deprecated.Returns a copy of this date with the specified period added.If the addition is zero, then
this
is returned. Fields in the period that aren't present in the partial are ignored.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)
orplusYears(int)
.- Parameters:
period
- the period to add to this one, null means zeroscalar
- the amount of times to add, such as -1 to subtract once- Returns:
- a copy of this instance with the period added
- Throws:
ArithmeticException
- if the new datetime exceeds the capacity
-
plus
Deprecated.Returns a copy of this date with the specified period added.If the amount is zero or null, then
this
is returned.This method is typically used to add complex period instances. Adding one field is best achieved using methods like
plusYears(int)
.- Parameters:
period
- the duration to add to this one, null means zero- Returns:
- a copy of this instance with the period added
- Throws:
ArithmeticException
- if the new datetime exceeds the capacity of a long
-
plusYears
Deprecated.Returns a copy of this date plus the specified number of years.This date instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay added = dt.plusYears(6); YearMonthDay added = dt.plus(Period.years(6)); YearMonthDay added = dt.withFieldAdded(DurationFieldType.years(), 6);
- Parameters:
years
- the amount of years to add, may be negative- Returns:
- the new date plus the increased years
- Since:
- 1.1
-
plusMonths
Deprecated.Returns a copy of this date plus the specified number of months.This date instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay added = dt.plusMonths(6); YearMonthDay added = dt.plus(Period.months(6)); YearMonthDay added = dt.withFieldAdded(DurationFieldType.months(), 6);
- Parameters:
months
- the amount of months to add, may be negative- Returns:
- the new date plus the increased months
- Since:
- 1.1
-
plusDays
Deprecated.Returns a copy of this date plus the specified number of days.This date instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay added = dt.plusDays(6); YearMonthDay added = dt.plus(Period.days(6)); YearMonthDay added = dt.withFieldAdded(DurationFieldType.days(), 6);
- Parameters:
days
- the amount of days to add, may be negative- Returns:
- the new date plus the increased days
- Since:
- 1.1
-
minus
Deprecated.Returns a copy of this date with the specified period taken away.If the amount is zero or null, then
this
is returned.This method is typically used to subtract complex period instances. Subtracting one field is best achieved using methods like
minusYears(int)
.- Parameters:
period
- the period to reduce this instant by- Returns:
- a copy of this instance with the period taken away
- Throws:
ArithmeticException
- if the new datetime exceeds the capacity of a long
-
minusYears
Deprecated.Returns a copy of this date minus the specified number of years.This datetime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay subtracted = dt.minusYears(6); YearMonthDay subtracted = dt.minus(Period.years(6)); YearMonthDay subtracted = dt.withFieldAdded(DurationFieldType.years(), -6);
- Parameters:
years
- the amount of years to subtract, may be negative- Returns:
- the new datetime minus the increased years
- Since:
- 1.1
-
minusMonths
Deprecated.Returns a copy of this date minus the specified number of months.This datetime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay subtracted = dt.minusMonths(6); YearMonthDay subtracted = dt.minus(Period.months(6)); YearMonthDay subtracted = dt.withFieldAdded(DurationFieldType.months(), -6);
- Parameters:
months
- the amount of months to subtract, may be negative- Returns:
- the new datetime minus the increased months
- Since:
- 1.1
-
minusDays
Deprecated.Returns a copy of this date minus the specified number of days.This datetime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
YearMonthDay subtracted = dt.minusDays(6); YearMonthDay subtracted = dt.minus(Period.days(6)); YearMonthDay subtracted = dt.withFieldAdded(DurationFieldType.days(), -6);
- Parameters:
days
- the amount of days to subtract, may be negative- Returns:
- the new datetime minus the increased days
- Since:
- 1.1
-
property
Deprecated.Gets the property object for the specified type, which contains many useful methods.- Parameters:
type
- the field type to get the property for- Returns:
- the property object
- Throws:
IllegalArgumentException
- if the field is null or unsupported
-
toLocalDate
Deprecated.Converts this object to a LocalDate with the same date and chronology.- Returns:
- a LocalDate with the same date and chronology
- Since:
- 1.3
-
toDateTimeAtMidnight
Deprecated.Converts this YearMonthDay to a full datetime at midnight using the default time zone.- Returns:
- this date as a datetime at midnight
-
toDateTimeAtMidnight
Deprecated.Converts this YearMonthDay to a full datetime at midnight using the specified time zone.This method uses the chronology from this instance plus the time zone specified.
- Parameters:
zone
- the zone to use, null means default- Returns:
- this date as a datetime at midnight
-
toDateTimeAtCurrentTime
Deprecated.Converts this partial to a full datetime using the default time zone setting the date fields from this instance and the time fields from the current time.- Returns:
- this date as a datetime with the time as the current time
-
toDateTimeAtCurrentTime
Deprecated.Converts this partial to a full datetime using the specified time zone setting the date fields from this instance and the time fields from the current time.This method uses the chronology from this instance plus the time zone specified.
- Parameters:
zone
- the zone to use, null means default- Returns:
- this date as a datetime with the time as the current time
-
toDateMidnight
Deprecated.Converts this object to a DateMidnight in the default time zone.- Returns:
- the DateMidnight instance in the default zone
-
toDateMidnight
Deprecated.Converts this object to a DateMidnight.- Parameters:
zone
- the zone to get the DateMidnight in, null means default- Returns:
- the DateMidnight instance
-
toDateTime
Deprecated.Converts this object to a DateTime using a TimeOfDay to fill in the missing fields and using the default time zone. This instance is immutable and unaffected by this method call.The resulting chronology is determined by the chronology of this YearMonthDay plus the time zone. The chronology of the time is ignored - only the field values are used.
- Parameters:
time
- the time of day to use, null means current time- Returns:
- the DateTime instance
-
toDateTime
Deprecated.Converts this object to a DateTime using a TimeOfDay to fill in the missing fields. This instance is immutable and unaffected by this method call.The resulting chronology is determined by the chronology of this YearMonthDay plus the time zone. The chronology of the time is ignored - only the field values are used.
- Parameters:
time
- the time of day to use, null means current timezone
- the zone to get the DateTime in, null means default- Returns:
- the DateTime instance
-
toInterval
Deprecated.Converts this object to an Interval representing the whole day in the default time zone.- Returns:
- a interval over the day
-
toInterval
Deprecated.Converts this object to an Interval representing the whole day.- Parameters:
zone
- the zone to get the Interval in, null means default- Returns:
- a interval over the day
-
getYear
public int getYear()Deprecated.Get the year field value.- Returns:
- the year
-
getMonthOfYear
public int getMonthOfYear()Deprecated.Get the month of year field value.- Returns:
- the month of year
-
getDayOfMonth
public int getDayOfMonth()Deprecated.Get the day of month field value.- Returns:
- the day of month
-
withYear
Deprecated.Returns a copy of this date with the year field updated.YearMonthDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of year changed.
- Parameters:
year
- the year to set- Returns:
- a copy of this object with the field set
- Throws:
IllegalArgumentException
- if the value is invalid- Since:
- 1.3
-
withMonthOfYear
Deprecated.Returns a copy of this date with the month of year field updated.YearMonthDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of month of year changed.
- Parameters:
monthOfYear
- the month of year to set- Returns:
- a copy of this object with the field set
- Throws:
IllegalArgumentException
- if the value is invalid- Since:
- 1.3
-
withDayOfMonth
Deprecated.Returns a copy of this date with the day of month field updated.YearMonthDay is immutable, so there are no set methods. Instead, this method returns a new instance with the value of day of month changed.
- Parameters:
dayOfMonth
- the day of month to set- Returns:
- a copy of this object with the field set
- Throws:
IllegalArgumentException
- if the value is invalid- Since:
- 1.3
-
year
Deprecated.Get the year field property which provides access to advanced functionality.- Returns:
- the year property
-
monthOfYear
Deprecated.Get the month of year field property which provides access to advanced functionality.- Returns:
- the month of year property
-
dayOfMonth
Deprecated.Get the day of month field property which provides access to advanced functionality.- Returns:
- the day of month property
-
toString
Deprecated.Output the date in the ISO8601 format YYYY-MM-DD.- Specified by:
toString
in interfaceReadablePartial
- Overrides:
toString
in classObject
- Returns:
- ISO8601 formatted string
-