public final class LocalDateTime extends BaseLocal implements ReadablePartial, Serializable
LocalDateTime implements the ReadablePartial
interface.
To do this, certain methods focus on key fields Year, MonthOfYear,
DayOfYear and MillisOfDay.
However, all fields may in fact be queried.
Internally, LocalDateTime uses a single millisecond-based value to represent the local datetime. This value is only used internally and is not exposed to applications.
Calculations on LocalDateTime are performed using a Chronology
.
This chronology will be set internally to be in the UTC time zone
for all calculations.
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
LocalDateTime is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.
Modifier and Type | Class and Description |
---|---|
static class |
LocalDateTime.Property
LocalDateTime.Property binds a LocalDateTime to a DateTimeField allowing
powerful datetime functionality to be easily accessed.
|
Constructor and Description |
---|
LocalDateTime()
Constructs an instance set to the current local time evaluated using
ISO chronology in the default zone.
|
LocalDateTime(Chronology chronology)
Constructs an instance set to the current local time evaluated using
specified chronology.
|
LocalDateTime(DateTimeZone zone)
Constructs an instance set to the current local time evaluated using
ISO chronology in the specified zone.
|
LocalDateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour)
Constructs an instance set to the specified date and time
using
ISOChronology . |
LocalDateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute)
Constructs an instance set to the specified date and time
using
ISOChronology . |
LocalDateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Constructs an instance set to the specified date and time
using
ISOChronology . |
LocalDateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
Chronology chronology)
Constructs an instance set to the specified date and time
using the specified chronology, whose zone is ignored.
|
LocalDateTime(long instant)
Constructs an instance set to the local time defined by the specified
instant evaluated using ISO chronology in the default zone.
|
LocalDateTime(long instant,
Chronology chronology)
Constructs an instance set to the local time defined by the specified
instant evaluated using the specified chronology.
|
LocalDateTime(long instant,
DateTimeZone zone)
Constructs an instance set to the local time defined by the specified
instant evaluated using ISO chronology in the specified zone.
|
LocalDateTime(Object instant)
Constructs an instance from an Object that represents a datetime.
|
LocalDateTime(Object instant,
Chronology chronology)
Constructs an instance from an Object that represents a datetime,
using the specified chronology.
|
LocalDateTime(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 |
---|---|
LocalDateTime.Property |
centuryOfEra()
Get the century of era property which provides access to advanced functionality.
|
int |
compareTo(ReadablePartial partial)
Compares this partial with another returning an integer
indicating the order.
|
LocalDateTime.Property |
dayOfMonth()
Get the day of month property which provides access to advanced functionality.
|
LocalDateTime.Property |
dayOfWeek()
Get the day of week property which provides access to advanced functionality.
|
LocalDateTime.Property |
dayOfYear()
Get the day of year property which provides access to advanced functionality.
|
boolean |
equals(Object partial)
Compares this ReadablePartial with another returning true if the chronology,
field types and values are equal.
|
LocalDateTime.Property |
era()
Get the era property which provides access to advanced functionality.
|
static LocalDateTime |
fromCalendarFields(Calendar calendar)
Constructs a LocalDateTime from a
java.util.Calendar
using exactly the same field values. |
static LocalDateTime |
fromDateFields(Date date)
Constructs a LocalDateTime from a
java.util.Date
using exactly the same field values. |
int |
get(DateTimeFieldType type)
Get the value of one of the fields of a datetime.
|
int |
getCenturyOfEra()
Get the year of era field value.
|
Chronology |
getChronology()
Gets the chronology of the datetime.
|
int |
getDayOfMonth()
Get the day of month field value.
|
int |
getDayOfWeek()
Get the day of week field value.
|
int |
getDayOfYear()
Get the day of year field value.
|
int |
getEra()
Get the era field value.
|
protected DateTimeField |
getField(int index,
Chronology chrono)
Gets the field for a specific index in the chronology specified.
|
int |
getHourOfDay()
Get the hour of day field value.
|
protected long |
getLocalMillis()
Gets the milliseconds of the datetime instant from the Java epoch
of 1970-01-01T00:00:00 (not fixed to any specific time zone).
|
int |
getMillisOfDay()
Get the millis of day field value.
|
int |
getMillisOfSecond()
Get the millis of second field value.
|
int |
getMinuteOfHour()
Get the minute of hour field value.
|
int |
getMonthOfYear()
Get the month of year field value.
|
int |
getSecondOfMinute()
Get the second of minute field value.
|
int |
getValue(int index)
Gets the value of the field at the specified index.
|
int |
getWeekOfWeekyear()
Get the week of weekyear field value.
|
int |
getWeekyear()
Get the weekyear field value.
|
int |
getYear()
Get the year field value.
|
int |
getYearOfCentury()
Get the year of century field value.
|
int |
getYearOfEra()
Get the year of era field value.
|
int |
hashCode()
Gets a hash code for the ReadablePartial that is compatible with the
equals method.
|
LocalDateTime.Property |
hourOfDay()
Get the hour of day field property which provides access to advanced functionality.
|
boolean |
isSupported(DateTimeFieldType type)
Checks if the field type specified is supported by this
local datetime and chronology.
|
boolean |
isSupported(DurationFieldType type)
Checks if the duration type specified is supported by this
local datetime and chronology.
|
LocalDateTime.Property |
millisOfDay()
Get the millis of day property which provides access to advanced functionality.
|
LocalDateTime.Property |
millisOfSecond()
Get the millis of second property which provides access to advanced functionality.
|
LocalDateTime |
minus(ReadableDuration duration)
Returns a copy of this datetime with the specified duration taken away.
|
LocalDateTime |
minus(ReadablePeriod period)
Returns a copy of this datetime with the specified period taken away.
|
LocalDateTime |
minusDays(int days)
Returns a copy of this datetime minus the specified number of days.
|
LocalDateTime |
minusHours(int hours)
Returns a copy of this datetime minus the specified number of hours.
|
LocalDateTime |
minusMillis(int millis)
Returns a copy of this datetime minus the specified number of millis.
|
LocalDateTime |
minusMinutes(int minutes)
Returns a copy of this datetime minus the specified number of minutes.
|
LocalDateTime |
minusMonths(int months)
Returns a copy of this datetime minus the specified number of months.
|
LocalDateTime |
minusSeconds(int seconds)
Returns a copy of this datetime minus the specified number of seconds.
|
LocalDateTime |
minusWeeks(int weeks)
Returns a copy of this datetime minus the specified number of weeks.
|
LocalDateTime |
minusYears(int years)
Returns a copy of this datetime minus the specified number of years.
|
LocalDateTime.Property |
minuteOfHour()
Get the minute of hour field property which provides access to advanced functionality.
|
LocalDateTime.Property |
monthOfYear()
Get the month of year property which provides access to advanced functionality.
|
static LocalDateTime |
now()
Obtains a
LocalDateTime set to the current system millisecond time
using ISOChronology in the default time zone. |
static LocalDateTime |
now(Chronology chronology)
Obtains a
LocalDateTime set to the current system millisecond time
using the specified chronology. |
static LocalDateTime |
now(DateTimeZone zone)
Obtains a
LocalDateTime set to the current system millisecond time
using ISOChronology in the specified time zone. |
static LocalDateTime |
parse(String str)
Parses a
LocalDateTime from the specified string. |
static LocalDateTime |
parse(String str,
DateTimeFormatter formatter)
Parses a
LocalDateTime from the specified string using a formatter. |
LocalDateTime |
plus(ReadableDuration duration)
Returns a copy of this datetime with the specified duration added.
|
LocalDateTime |
plus(ReadablePeriod period)
Returns a copy of this datetime with the specified period added.
|
LocalDateTime |
plusDays(int days)
Returns a copy of this datetime plus the specified number of days.
|
LocalDateTime |
plusHours(int hours)
Returns a copy of this datetime plus the specified number of hours.
|
LocalDateTime |
plusMillis(int millis)
Returns a copy of this datetime plus the specified number of millis.
|
LocalDateTime |
plusMinutes(int minutes)
Returns a copy of this datetime plus the specified number of minutes.
|
LocalDateTime |
plusMonths(int months)
Returns a copy of this datetime plus the specified number of months.
|
LocalDateTime |
plusSeconds(int seconds)
Returns a copy of this datetime plus the specified number of seconds.
|
LocalDateTime |
plusWeeks(int weeks)
Returns a copy of this datetime plus the specified number of weeks.
|
LocalDateTime |
plusYears(int years)
Returns a copy of this datetime plus the specified number of years.
|
LocalDateTime.Property |
property(DateTimeFieldType fieldType)
Gets the property object for the specified type, which contains many
useful methods.
|
LocalDateTime.Property |
secondOfMinute()
Get the second of minute field property which provides access to advanced functionality.
|
int |
size()
Gets the number of fields in this partial, which is four.
|
Date |
toDate()
Get the date time as a
java.util.Date . |
Date |
toDate(TimeZone timeZone)
Get the date time as a
java.util.Date using the specified time zone. |
DateTime |
toDateTime()
Converts this object to a DateTime using the default zone.
|
DateTime |
toDateTime(DateTimeZone zone)
Converts this object to a DateTime using the specified zone.
|
LocalDate |
toLocalDate()
Converts this object to a LocalDate with the same date and chronology.
|
LocalTime |
toLocalTime()
Converts this object to a LocalTime with the same time and chronology.
|
String |
toString()
Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSS).
|
String |
toString(String pattern)
Output the date using the specified format pattern.
|
String |
toString(String pattern,
Locale locale)
Output the date using the specified format pattern.
|
LocalDateTime.Property |
weekOfWeekyear()
Get the week of a week based year property which provides access to advanced functionality.
|
LocalDateTime.Property |
weekyear()
Get the weekyear property which provides access to advanced functionality.
|
LocalDateTime |
withCenturyOfEra(int centuryOfEra)
Returns a copy of this datetime with the century of era field updated.
|
LocalDateTime |
withDate(int year,
int monthOfYear,
int dayOfMonth)
Returns a copy of this datetime with the specified date,
retaining the time fields.
|
LocalDateTime |
withDayOfMonth(int dayOfMonth)
Returns a copy of this datetime with the day of month field updated.
|
LocalDateTime |
withDayOfWeek(int dayOfWeek)
Returns a copy of this datetime with the day of week field updated.
|
LocalDateTime |
withDayOfYear(int dayOfYear)
Returns a copy of this datetime with the day of year field updated.
|
LocalDateTime |
withDurationAdded(ReadableDuration durationToAdd,
int scalar)
Returns a copy of this datetime with the specified duration added.
|
LocalDateTime |
withEra(int era)
Returns a copy of this datetime with the era field updated.
|
LocalDateTime |
withField(DateTimeFieldType fieldType,
int value)
Returns a copy of this datetime with the specified field set to a new value.
|
LocalDateTime |
withFieldAdded(DurationFieldType fieldType,
int amount)
Returns a copy of this datetime with the value of the specified
field increased.
|
LocalDateTime |
withFields(ReadablePartial partial)
Returns a copy of this datetime with the partial set of fields
replacing those from this instance.
|
LocalDateTime |
withHourOfDay(int hour)
Returns a copy of this datetime with the hour of day field updated.
|
LocalDateTime |
withMillisOfDay(int millis)
Returns a copy of this datetime with the millis of day field updated.
|
LocalDateTime |
withMillisOfSecond(int millis)
Returns a copy of this datetime with the millis of second field updated.
|
LocalDateTime |
withMinuteOfHour(int minute)
Returns a copy of this datetime with the minute of hour field updated.
|
LocalDateTime |
withMonthOfYear(int monthOfYear)
Returns a copy of this datetime with the month of year field updated.
|
LocalDateTime |
withPeriodAdded(ReadablePeriod period,
int scalar)
Returns a copy of this datetime with the specified period added.
|
LocalDateTime |
withSecondOfMinute(int second)
Returns a copy of this datetime with the second of minute field updated.
|
LocalDateTime |
withTime(int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond)
Returns a copy of this datetime with the specified time,
retaining the date fields.
|
LocalDateTime |
withWeekOfWeekyear(int weekOfWeekyear)
Returns a copy of this datetime with the week of weekyear field updated.
|
LocalDateTime |
withWeekyear(int weekyear)
Returns a copy of this datetime with the weekyear field updated.
|
LocalDateTime |
withYear(int year)
Returns a copy of this datetime with the year field updated.
|
LocalDateTime |
withYearOfCentury(int yearOfCentury)
Returns a copy of this datetime with the year of century field updated.
|
LocalDateTime |
withYearOfEra(int yearOfEra)
Returns a copy of this datetime with the year of era field updated.
|
LocalDateTime.Property |
year()
Get the year property which provides access to advanced functionality.
|
LocalDateTime.Property |
yearOfCentury()
Get the year of century property which provides access to advanced functionality.
|
LocalDateTime.Property |
yearOfEra()
Get the year of era property which provides access to advanced functionality.
|
getField, getFields, getFieldType, getFieldTypes, getValues, indexOf, indexOf, indexOfSupported, indexOfSupported, isAfter, isBefore, isEqual, toDateTime, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
getField, getFieldType, toDateTime
public LocalDateTime()
Once the constructor is completed, the zone is no longer used.
now()
public LocalDateTime(DateTimeZone zone)
If the specified time zone is null, the default zone is used. Once the constructor is completed, the zone is no longer used.
zone
- the time zone, null means default zonenow(DateTimeZone)
public LocalDateTime(Chronology chronology)
If the chronology is null, ISO chronology in the default time zone is used. Once the constructor is completed, the zone is no longer used.
chronology
- the chronology, null means ISOChronology in default zonenow(Chronology)
public LocalDateTime(long instant)
Once the constructor is completed, the zone is no longer used.
instant
- the milliseconds from 1970-01-01T00:00:00Zpublic LocalDateTime(long instant, DateTimeZone zone)
If the specified time zone is null, the default zone is used. Once the constructor is completed, the zone is no longer used.
instant
- the milliseconds from 1970-01-01T00:00:00Zzone
- the time zone, null means default zonepublic LocalDateTime(long instant, Chronology chronology)
If the chronology is null, ISO chronology in the default zone is used. Once the constructor is completed, the zone is no longer used.
instant
- the milliseconds from 1970-01-01T00:00:00Zchronology
- the chronology, null means ISOChronology in default zonepublic LocalDateTime(Object instant)
If the object contains no chronology, ISOChronology
is used.
If the object contains no time zone, the default zone is used.
Once the constructor is completed, the zone is no longer used.
The recognised object types are defined in
ConverterManager
and
include ReadablePartial, ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.localDateOptionalTimeParser()
.
The default String converter ignores the zone and only parses the field values.
instant
- the datetime objectIllegalArgumentException
- if the instant is invalidpublic LocalDateTime(Object instant, DateTimeZone zone)
If the object contains no chronology, ISOChronology
is used.
If the specified time zone is null, the default zone is used.
Once the constructor is completed, the zone is no longer used.
The recognised object types are defined in
ConverterManager
and
include ReadablePartial, ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.localDateOptionalTimeParser()
.
The default String converter ignores the zone and only parses the field values.
instant
- the datetime objectzone
- the time zoneIllegalArgumentException
- if the instant is invalidpublic LocalDateTime(Object instant, Chronology chronology)
If the chronology is null, ISO in the default time zone is used.
Once the constructor is completed, the zone is no longer used.
If the instant contains a chronology, it will be ignored.
For example, passing a LocalDate
and a different chronology
will return a date with the year/month/day from the date applied
unaltered to the specified chronology.
The recognised object types are defined in
ConverterManager
and
include ReadablePartial, ReadableInstant, String, Calendar and Date.
The String formats are described by ISODateTimeFormat.localDateOptionalTimeParser()
.
The default String converter ignores the zone and only parses the field values.
instant
- the datetime objectchronology
- the chronologyIllegalArgumentException
- if the instant is invalidpublic LocalDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour)
ISOChronology
.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 LocalDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
ISOChronology
.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 LocalDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
ISOChronology
.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 LocalDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond, Chronology chronology)
If the chronology is null, ISOChronology
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 LocalDateTime now()
LocalDateTime
set to the current system millisecond time
using ISOChronology
in the default time zone.
The resulting object does not use the zone.public static LocalDateTime now(DateTimeZone zone)
LocalDateTime
set to the current system millisecond time
using ISOChronology
in the specified time zone.
The resulting object does not use the zone.zone
- the time zone, not nullpublic static LocalDateTime now(Chronology chronology)
LocalDateTime
set to the current system millisecond time
using the specified chronology.
The resulting object does not use the zone.chronology
- the chronology, not nullpublic static LocalDateTime parse(String str)
LocalDateTime
from the specified string.
str
- the string to parse, not nullpublic static LocalDateTime parse(String str, DateTimeFormatter formatter)
LocalDateTime
from the specified string using a formatter.str
- the string to parse, not nullformatter
- the formatter to use, not nullpublic static LocalDateTime fromCalendarFields(Calendar calendar)
java.util.Calendar
using exactly the same field values.
Each field is queried from the Calendar and assigned to the LocalDateTime. This is useful if you have been using the Calendar as a local date, ignoring the zone.
One advantage of this method is that this method is unaffected if the version of the time zone data differs between the JDK and Joda-Time. That is because the local field values are transferred, calculated using the JDK time zone data and without using the Joda-Time time zone data.
This factory method ignores the type of the calendar and always
creates a LocalDateTime with ISO chronology. It is expected that you
will only pass in instances of GregorianCalendar
however
this is not validated.
calendar
- the Calendar to extract fields from, not nullIllegalArgumentException
- if the calendar is nullIllegalArgumentException
- if the date is invalid for the ISO chronologypublic static LocalDateTime fromDateFields(Date date)
java.util.Date
using exactly the same field values.
Each field is queried from the Date and assigned to the LocalDateTime. This is useful if you have been using the Date as a local date, ignoring the zone.
One advantage of this method is that this method is unaffected if the version of the time zone data differs between the JDK and Joda-Time. That is because the local field values are transferred, calculated using the JDK time zone data and without using the Joda-Time time zone data.
This factory method always creates a LocalDateTime with ISO chronology.
date
- the Date to extract fields from, not nullIllegalArgumentException
- if the calendar is nullIllegalArgumentException
- if the date is invalid for the ISO chronologypublic int size()
size
in interface ReadablePartial
protected DateTimeField getField(int index, Chronology chrono)
This method must not use any instance variables.
getField
in class AbstractPartial
index
- the index to retrievechrono
- the chronology to usepublic int getValue(int index)
This method is required to support the ReadablePartial
interface. The supported fields are Year, MonthOfDay, DayOfMonth and MillisOfDay.
getValue
in interface ReadablePartial
index
- the index, zero to twoIndexOutOfBoundsException
- if the index is invalidpublic int get(DateTimeFieldType type)
This method gets the value of the specified field. For example:
DateTime dt = new DateTime(); int year = dt.get(DateTimeFieldType.year());
get
in interface ReadablePartial
get
in class AbstractPartial
type
- a field type, usually obtained from DateTimeFieldType, not nullIllegalArgumentException
- if the field type is nullpublic boolean isSupported(DateTimeFieldType type)
get(DateTimeFieldType)
.isSupported
in interface ReadablePartial
isSupported
in class AbstractPartial
type
- a field type, usually obtained from DateTimeFieldTypepublic boolean isSupported(DurationFieldType type)
type
- a duration type, usually obtained from DurationFieldTypeprotected long getLocalMillis()
getLocalMillis
in class BaseLocal
public Chronology getChronology()
getChronology
in interface ReadablePartial
public boolean equals(Object partial)
equals
in interface ReadablePartial
equals
in class AbstractPartial
partial
- an object to check againstpublic int hashCode()
AbstractPartial
hashCode
in interface ReadablePartial
hashCode
in class AbstractPartial
public int compareTo(ReadablePartial partial)
The fields are compared in order, from largest to smallest. The first field that is non-equal is used to determine the result.
The specified object must be a partial instance whose field types match those of this partial.
compareTo
in interface Comparable<ReadablePartial>
compareTo
in class AbstractPartial
partial
- an object to check againstClassCastException
- if the partial is the wrong class
or if it has field types that don't matchNullPointerException
- if the partial is nullpublic DateTime toDateTime()
When the time zone is applied, the local date-time may be affected by daylight saving. In a daylight saving gap, when the local time does not exist, this method will throw an exception. In a daylight saving overlap, when the same local time occurs twice, this method returns the first occurrence of the local time.
this
IllegalInstantException
- if the local time does not exist when the time zone is appliedpublic DateTime toDateTime(DateTimeZone zone)
When the time zone is applied, the local date-time may be affected by daylight saving. In a daylight saving gap, when the local time does not exist, this method will throw an exception. In a daylight saving overlap, when the same local time occurs twice, this method returns the first occurrence of the local time.
zone
- time zone to apply, or default if nullIllegalInstantException
- if the local time does not exist when the time zone is appliedpublic LocalDate toLocalDate()
public LocalTime toLocalTime()
public Date toDate()
java.util.Date
.
The Date
object created has exactly the same fields as this
date-time, except when the time would be invalid due to a daylight savings
gap. In that case, the time will be set to the earliest valid time after the gap.
In the case of a daylight savings overlap, the earlier instant is selected.
Converting to a JDK Date is full of complications as the JDK Date constructor doesn't behave as you might expect around DST transitions. This method works by taking a first guess and then adjusting. This also handles the situation where the JDK time zone data differs from the Joda-Time time zone data.
public Date toDate(TimeZone timeZone)
java.util.Date
using the specified time zone.
The Date
object created has exactly the same fields as this
date-time, except when the time would be invalid due to a daylight savings
gap. In that case, the time will be set to the earliest valid time after the gap.
In the case of a daylight savings overlap, the earlier instant is selected.
Converting to a JDK Date is full of complications as the JDK Date constructor doesn't behave as you might expect around DST transitions. This method works by taking a first guess and then adjusting. This also handles the situation where the JDK time zone data differs from the Joda-Time time zone data.
Unlike toDate()
, this implementation does not rely on Java's synchronized
time zone initialization logic, and should demonstrate better concurrent performance
characteristics.
timeZone
- the time zonepublic LocalDateTime 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 = dt.monthOfYear().setCopy(6);
year
- the new year valuemonthOfYear
- the new monthOfYear valuedayOfMonth
- the new dayOfMonth valueIllegalArgumentException
- if any value if invalidpublic LocalDateTime 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:
LocalDateTime set = dt.hourOfDay().setCopy(6);
hourOfDay
- the hour of the dayminuteOfHour
- the minute of the hoursecondOfMinute
- the second of the minutemillisOfSecond
- the millisecond of the secondIllegalArgumentException
- if any value if invalidpublic LocalDateTime 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 ignoredIllegalArgumentException
- if any value is invalidpublic LocalDateTime 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:
LocalDateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6); LocalDateTime updated = dt.dayOfMonth().setCopy(6); LocalDateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
fieldType
- the field type to set, not nullvalue
- the value to setIllegalArgumentException
- if the value is null or invalidpublic LocalDateTime withFieldAdded(DurationFieldType fieldType, int amount)
If the addition is zero or the field is null, then this
is returned.
These three lines are equivalent:
LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6); LocalDateTime added = dt.plusYears(6); LocalDateTime added = dt.plus(Period.years(6));
fieldType
- the field type to add to, not nullamount
- the amount to addIllegalArgumentException
- if the value is null or invalidArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime 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 onceArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime 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 onceArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime plus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to add to this one, null means zeroArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime plus(ReadablePeriod period)
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)
.
period
- the period to add to this one, null means zeroArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime plusYears(int years)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusYears(6); LocalDateTime added = dt.plus(Period.years(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
years
- the amount of years to add, may be negativepublic LocalDateTime plusMonths(int months)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusMonths(6); LocalDateTime added = dt.plus(Period.months(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);
months
- the amount of months to add, may be negativepublic LocalDateTime plusWeeks(int weeks)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusWeeks(6); LocalDateTime added = dt.plus(Period.weeks(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.weeks(), 6);
weeks
- the amount of weeks to add, may be negativepublic LocalDateTime plusDays(int days)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusDays(6); LocalDateTime added = dt.plus(Period.days(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.days(), 6);
days
- the amount of days to add, may be negativepublic LocalDateTime plusHours(int hours)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusHours(6); LocalDateTime added = dt.plus(Period.hours(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6);
hours
- the amount of hours to add, may be negativepublic LocalDateTime plusMinutes(int minutes)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusMinutes(6); LocalDateTime added = dt.plus(Period.minutes(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.minutes(), 6);
minutes
- the amount of minutes to add, may be negativepublic LocalDateTime plusSeconds(int seconds)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusSeconds(6); LocalDateTime added = dt.plus(Period.seconds(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.seconds(), 6);
seconds
- the amount of seconds to add, may be negativepublic LocalDateTime plusMillis(int millis)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime added = dt.plusMillis(6); LocalDateTime added = dt.plus(Period.millis(6)); LocalDateTime added = dt.withFieldAdded(DurationFieldType.millis(), 6);
millis
- the amount of millis to add, may be negativepublic LocalDateTime minus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to reduce this instant byArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime minus(ReadablePeriod period)
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)
.
period
- the period to reduce this instant byArithmeticException
- if the result exceeds the internal capacitypublic LocalDateTime minusYears(int years)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusYears(6); LocalDateTime subtracted = dt.minus(Period.years(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.years(), -6);
years
- the amount of years to subtract, may be negativepublic LocalDateTime minusMonths(int months)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusMonths(6); LocalDateTime subtracted = dt.minus(Period.months(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.months(), -6);
months
- the amount of months to subtract, may be negativepublic LocalDateTime minusWeeks(int weeks)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusWeeks(6); LocalDateTime subtracted = dt.minus(Period.weeks(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.weeks(), -6);
weeks
- the amount of weeks to subtract, may be negativepublic LocalDateTime minusDays(int days)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusDays(6); LocalDateTime subtracted = dt.minus(Period.days(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6);
days
- the amount of days to subtract, may be negativepublic LocalDateTime minusHours(int hours)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusHours(6); LocalDateTime subtracted = dt.minus(Period.hours(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);
hours
- the amount of hours to subtract, may be negativepublic LocalDateTime minusMinutes(int minutes)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusMinutes(6); LocalDateTime subtracted = dt.minus(Period.minutes(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.minutes(), -6);
minutes
- the amount of minutes to subtract, may be negativepublic LocalDateTime minusSeconds(int seconds)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusSeconds(6); LocalDateTime subtracted = dt.minus(Period.seconds(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6);
seconds
- the amount of seconds to subtract, may be negativepublic LocalDateTime minusMillis(int millis)
This LocalDateTime instance is immutable and unaffected by this method call.
The following three lines are identical in effect:
LocalDateTime subtracted = dt.minusMillis(6); LocalDateTime subtracted = dt.minus(Period.millis(6)); LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6);
millis
- the amount of millis to subtract, may be negativepublic LocalDateTime.Property property(DateTimeFieldType fieldType)
fieldType
- the field type to get the chronology forIllegalArgumentException
- if the field is null or unsupportedpublic int getEra()
public int getCenturyOfEra()
public int getYearOfEra()
public int getYearOfCentury()
public int getYear()
public int getWeekyear()
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.
public int getMonthOfYear()
public int getWeekOfWeekyear()
This field is associated with the "weekyear" via getWeekyear()
.
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.
public int getDayOfYear()
public int getDayOfMonth()
The values for the day of month are defined in DateTimeConstants
.
public int getDayOfWeek()
The values for the day of week are defined in DateTimeConstants
.
public int getHourOfDay()
public int getMinuteOfHour()
public int getSecondOfMinute()
public int getMillisOfSecond()
public int getMillisOfDay()
public LocalDateTime withEra(int era)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withCenturyOfEra(int centuryOfEra)
LocalDateTime 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 century of era to setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withYearOfEra(int yearOfEra)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withYearOfCentury(int yearOfCentury)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withYear(int year)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime 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.
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withMonthOfYear(int monthOfYear)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime 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.
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withDayOfYear(int dayOfYear)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withDayOfMonth(int dayOfMonth)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withDayOfWeek(int dayOfWeek)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withHourOfDay(int hour)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withMinuteOfHour(int minute)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withSecondOfMinute(int second)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withMillisOfSecond(int millis)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime withMillisOfDay(int millis)
LocalDateTime 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 setIllegalArgumentException
- if the value is invalidpublic LocalDateTime.Property era()
public LocalDateTime.Property centuryOfEra()
public LocalDateTime.Property yearOfCentury()
public LocalDateTime.Property yearOfEra()
public LocalDateTime.Property year()
public LocalDateTime.Property weekyear()
public LocalDateTime.Property monthOfYear()
public LocalDateTime.Property weekOfWeekyear()
public LocalDateTime.Property dayOfYear()
public LocalDateTime.Property dayOfMonth()
public LocalDateTime.Property dayOfWeek()
public LocalDateTime.Property hourOfDay()
public LocalDateTime.Property minuteOfHour()
public LocalDateTime.Property secondOfMinute()
public LocalDateTime.Property millisOfSecond()
public LocalDateTime.Property millisOfDay()
public String toString()
toString
in interface ReadablePartial
toString
in class Object
public String toString(String pattern)
pattern
- the pattern specification, null means use toString
DateTimeFormat
public String toString(String pattern, Locale locale) throws IllegalArgumentException
pattern
- the pattern specification, null means use toString
locale
- Locale to use, null means defaultIllegalArgumentException
- if the pattern is invalidDateTimeFormat
Copyright © 2002–2024 Joda.org. All rights reserved.