public final class DateTime
extends java.lang.Object
Objects of this class are immutable.
| Modifier and Type | Field and Description |
|---|---|
static CalendarMetrics |
GREGORIAN_CALENDAR_SCALE
The default calendar scale.
|
static java.util.TimeZone |
UTC
Static instance of the time zone UTC.
|
| Constructor and Description |
|---|
DateTime(CalendarMetrics calendarMetrics,
DateTime dateTime)
Clone constructor changing the
CalendarMetrics. |
DateTime(CalendarMetrics calendarMetrics,
int year,
int month,
int dayOfMonth)
Creates a new
DateTime for the given all-day date using the given calendar metrics. |
DateTime(CalendarMetrics calendarMetrics,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
Create a new floating DateTime using the given calendar metrics.
|
DateTime(CalendarMetrics calendarMetrics,
java.util.TimeZone timezone,
DateTime dateTime)
Clone constructor that changes the
CalendarMetrics and the TimeZone. |
DateTime(CalendarMetrics calendarMetrics,
java.util.TimeZone timezone,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
Creates a new absolute
DateTime instance in the given TimeZone and calendar metrics. |
DateTime(CalendarMetrics calendarMetrics,
java.util.TimeZone timezone,
long timestamp)
Create a new
DateTime from the given time stamp using the given CalendarMetrics and the given time zone. |
DateTime(int year,
int month,
int dayOfMonth)
Creates a new
DateTime for the given all-day date using the GREGORIAN_CALENDAR_SCALE. |
DateTime(int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
Create a new floating DateTime using
GREGORIAN_CALENDAR_SCALE. |
DateTime(long timestamp)
|
DateTime(java.lang.String calScale,
int year,
int month,
int dayOfMonth)
Creates a new
DateTime for the given all-day date using the given calendar scale. |
DateTime(java.lang.String calScale,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
Create a new floating DateTime using the given calendar scale.
|
DateTime(java.lang.String calScale,
java.util.TimeZone timezone,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
Creates a new absolute
DateTime instance in the given TimeZone and calendar scale. |
DateTime(java.util.TimeZone timezone,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
|
DateTime(java.util.TimeZone timezone,
long timestamp)
Create a new
DateTime from the given time stamp using GREGORIAN_CALENDAR_SCALE and the given time zone. |
| Modifier and Type | Method and Description |
|---|---|
DateTime |
addDuration(Duration duration)
Add the given duration to this DateTime.
|
boolean |
after(DateTime that)
Check if another date is earlier in time than this one.
|
boolean |
before(DateTime that)
Check if another date is later in time than this one.
|
boolean |
equals(java.lang.Object obj) |
CalendarMetrics |
getCalendarMetrics()
Returns the
CalendarMetrics of this DateTime. |
int |
getDayOfMonth()
Returns the month day of this date-time object.
|
int |
getDayOfWeek()
Returns the week day of this DateTime object.
|
int |
getHours()
Returns the hours in this date-time object.
|
long |
getInstance()
Get the packed instance value of this DateTime.
|
int |
getMinutes()
Returns the minutes in this date-time object.
|
int |
getMonth()
Returns the month of this date-time object.
|
int |
getSeconds()
Returns the seconds in this date-time object.
|
long |
getTimestamp()
Returns the timestamp of this
DateTime instance. |
java.util.TimeZone |
getTimeZone()
Returns the
TimeZone of this DateTime. |
int |
getWeekOfYear()
Returns the week of year of this date-time object.
|
int |
getYear()
Returns the year of this date-time object.
|
int |
hashCode() |
boolean |
isAllDay()
Returns whether this is an all-day instance.
|
boolean |
isFloating()
Returns whether this is a floating instance.
|
static DateTime |
now()
|
static DateTime |
now(java.util.TimeZone timeZone)
Return a new
DateTime having the current date and time in the given TimeZone. |
static DateTime |
nowAndHere()
Return a new
DateTime having the current date and time in the local TimeZone. |
static DateTime |
parse(CalendarMetrics calendarMetrics,
java.util.TimeZone timeZone,
java.lang.String string)
Parses a date-time string as specified in RFC 5545, Section 3.3.5.
|
static DateTime |
parse(java.lang.String string)
Parses a date-time string as specified in RFC 5545, Section 3.3.5.
|
static DateTime |
parse(java.lang.String timeZone,
java.lang.String string)
Parses a date-time string as specified in RFC 5545, Section 3.3.5.
|
static DateTime |
parse(java.lang.String calScale,
java.util.TimeZone timeZone,
java.lang.String string)
Parses a date-time string as specified in RFC 5545, Section 3.3.5.
|
static DateTime |
parse(java.util.TimeZone timeZone,
java.lang.String string)
Parses a date-time string as specified in RFC 5545, Section 3.3.5.
|
DateTime |
shiftTimeZone(java.util.TimeZone timezone)
Replace the current time zone by the given one, keeping the absolute time constant.
|
DateTime |
startOfDay()
Return the start of this day.
|
DateTime |
swapTimeZone(java.util.TimeZone timezone)
Replace the current time zone by the given one, keeping the local time constant.
|
DateTime |
toAllDay()
|
static DateTime |
today()
Return a new
DateTime having the current date. |
java.lang.String |
toString() |
void |
writeTo(java.lang.StringBuilder out)
Write the date-time string represented by this object to the given
StringBuilder. |
void |
writeTo(java.io.Writer out)
Write the date-time string represented by this object to the given
Writer.. |
public static final CalendarMetrics GREGORIAN_CALENDAR_SCALE
DateTime and Date values use the Gregorian calendar scale if not specified otherwise.public static final java.util.TimeZone UTC
public DateTime(CalendarMetrics calendarMetrics, DateTime dateTime)
CalendarMetrics. It will represent the same absolute time, but instances will be in another calendar scale.
All-day and floating instances will still be all-day respective floating.calendarMetrics - The new calendar scale.dateTime - The DateTime representing the absolute time.public DateTime(CalendarMetrics calendarMetrics, java.util.TimeZone timezone, DateTime dateTime)
CalendarMetrics and the TimeZone. It will represent the same absolute time, but instances will be in
another calendar scale and time zone. You can use this to convert between calendar scales.
If the given DateTime is all-day the time zone will be ignored. If the given DateTime is floating it will be converted to an absolute
time
calendarMetrics - The calendar scale to use.timezone - The new TimeZone.dateTime - The DateTime to clone from.public DateTime(long timestamp)
timestamp - The time in milliseconds since the epoch of this date-time value.public DateTime(java.util.TimeZone timezone,
long timestamp)
DateTime from the given time stamp using GREGORIAN_CALENDAR_SCALE and the given time zone.timezone - The TimeZone of the new instance.timestamp - The time in milliseconds since the epoch of this date-time value.public DateTime(CalendarMetrics calendarMetrics, java.util.TimeZone timezone, long timestamp)
DateTime from the given time stamp using the given CalendarMetrics and the given time zone.calendarMetrics - The CalendarMetrics of the DateTime.timezone - The TimeZone of the new instance.timestamp - The time in milliseconds since the epoch of this date-time value.public DateTime(int year,
int month,
int dayOfMonth)
DateTime for the given all-day date using the GREGORIAN_CALENDAR_SCALE. As a result of this the all-day flag will be set
to true.year - The year of the event.month - The month of the event.dayOfMonth - The monthday of the event.public DateTime(int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
GREGORIAN_CALENDAR_SCALE.year - The year.month - The month.dayOfMonth - The day of the month.hours - The hour.minutes - The minutes.seconds - The seconds.public DateTime(java.util.TimeZone timezone,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
timezone - The TimeZone of the date, may be null to create a floating date.year - The year of the date.month - The month.dayOfMonth - The month day.hours - The hours.minutes - The minutes.seconds - The seconds.public DateTime(java.lang.String calScale,
int year,
int month,
int dayOfMonth)
DateTime for the given all-day date using the given calendar scale. As a result of this the all-day flag will be set to true.calScale - The name of the calendar scale to use.year - The year of the event.month - The month of the event.dayOfMonth - The monthday of the event.public DateTime(java.lang.String calScale,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
calScale - The name of the calendar scale to use.year - The year.month - The month.dayOfMonth - The day of the month.hours - The hour.minutes - The minutes.seconds - The seconds.public DateTime(java.lang.String calScale,
java.util.TimeZone timezone,
int year,
int month,
int dayOfMonth,
int hours,
int minutes,
int seconds)
DateTime instance in the given TimeZone and calendar scale.calScale - The name of the calendar scale to use.timezone - The TimeZone of the date, may be null to create a floating date.year - The year of the date.month - The month.dayOfMonth - The month day.hours - The hours.minutes - The minutes.seconds - The seconds.public DateTime(CalendarMetrics calendarMetrics, int year, int month, int dayOfMonth)
DateTime for the given all-day date using the given calendar metrics.calendarMetrics - The CalendarMetrics to use.year - The year of the event.month - The month of the event.dayOfMonth - The monthday of the event.public DateTime(CalendarMetrics calendarMetrics, int year, int month, int dayOfMonth, int hours, int minutes, int seconds)
calendarMetrics - The CalendarMetrics to use.year - The year.month - The month.dayOfMonth - The day of the month.hours - The hour.minutes - The minutes.seconds - The seconds.public DateTime(CalendarMetrics calendarMetrics, java.util.TimeZone timezone, int year, int month, int dayOfMonth, int hours, int minutes, int seconds)
DateTime instance in the given TimeZone and calendar metrics.calendarMetrics - The CalendarMetrics to use.timezone - The TimeZone of the date, may be null to create a floating date.year - The year of the date.month - The month.dayOfMonth - The month day.hours - The hours.minutes - The minutes.seconds - The seconds.public static DateTime now()
public static DateTime nowAndHere()
DateTime having the current date and time in the local TimeZone. This is short for
new DateTime(DateTime.GREGORIAN_CALENDAR_SCALE, TimeZone.getDefault(), System.currentTimeMillis()).public static DateTime now(java.util.TimeZone timeZone)
DateTime having the current date and time in the given TimeZone. This is short for
new DateTime(DateTime.GREGORIAN_CALENDAR_SCALE, timeZone, System.currentTimeMillis()).timeZone - The TimeZone of the DateTime instance.public static DateTime today()
DateTime having the current date. This is short for
new DateTime(DateTime.GREGORIAN_CALENDAR_SCALE, TimeZone.getDefault(), System.currentTimeMillis()).toAllDay().public static DateTime parse(java.lang.String string)
GREGORIAN_CALENDAR_SCALE. Unless the given String ends with "Z" the resulting DateTime will be floating.string - A valid date-time string.DateTime instance.parse(CalendarMetrics, TimeZone, String)public static DateTime parse(java.lang.String timeZone, java.lang.String string)
GREGORIAN_CALENDAR_SCALE.timeZone - A time zone to apply to non-allday and non-UTC date-time values. If timeZone is null the event will be floating.string - A valid date-time string.DateTime instance.parse(CalendarMetrics, TimeZone, String)public static DateTime parse(java.util.TimeZone timeZone, java.lang.String string)
GREGORIAN_CALENDAR_SCALE.timeZone - A time zone to apply to non-allday and non-UTC date-time values. If timeZone is null the event will be floating.string - A valid date-time string.DateTime instance.parse(CalendarMetrics, TimeZone, String)public static DateTime parse(java.lang.String calScale, java.util.TimeZone timeZone, java.lang.String string)
calScale - The name of the calendar scale to use.timeZone - A time zone to apply to non-allday and non-UTC date-time values. If timeZone is null the event will be floating.string - A valid date-time string.DateTime instance.parse(CalendarMetrics, TimeZone, String)public static DateTime parse(CalendarMetrics calendarMetrics, java.util.TimeZone timeZone, java.lang.String string)
YYYYMMDDYYYYMMDD'T'HHMMSSYYYYMMDD'T'HHMMSS'Z''T' and 'Z' stand for
the literals T and Z.
If 'Z' is present the time zone is UTC. Otherwise the time zone is specified in an additional parameter or if no such parameter exists it's floating (i.e. always local time).
Use swapTimeZone(TimeZone) to set a specific time zone.
calendarMetrics - The CalendarMetrics to use.timeZone - A time zone to apply to non-allday and non-UTC date-time values. If timeZone is null the event will be floating unless it ends with
'Z'.string - A valid date-time string.DateTime instance.public CalendarMetrics getCalendarMetrics()
CalendarMetrics of this DateTime.CalendarMetrics instance.public java.util.TimeZone getTimeZone()
TimeZone of this DateTime.TimeZone or null for floating instances.public long getTimestamp()
DateTime instance. For floating dates, this equals the timestamp in UTC.public DateTime toAllDay()
public DateTime startOfDay()
TimeZone.public DateTime swapTimeZone(java.util.TimeZone timezone)
timezone - The new TimeZone.java.lang.IllegalStateException - if the date is all-day.public DateTime shiftTimeZone(java.util.TimeZone timezone)
FIXME: There is a problem in case the time falls on a DST change. In that case the time might not be calculated as expected.
timezone - The new TimeZone.java.lang.IllegalStateException - if the date is all-day.public boolean isAllDay()
true if this is all-day, false otherwise.public boolean isFloating()
true if this is floating, false otherwise.public int getYear()
public int getMonth()
public int getWeekOfYear()
public int getDayOfWeek()
public int getDayOfMonth()
public int getHours()
0.public int getMinutes()
0.public int getSeconds()
0.public DateTime addDuration(Duration duration)
public long getInstance()
public boolean after(DateTime that)
Note: it doesn't make sense to compare a floating time to an absolute time. However, this method won't complain and treat floating times like UTC times.
that - The DateTime to compare to.true if this instance is after the given instance.public boolean before(DateTime that)
Note: it doesn't make sense to compare a floating time to an absolute time. However, this method won't complain and treat floating times like UTC times.
that - The DateTime to compare to.true if this instance is before the given instance.public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Objectpublic void writeTo(java.lang.StringBuilder out)
throws java.io.IOException
Write the date-time string represented by this object to the given StringBuilder. The written value conforms to RFC 5545, Section 3.3.5.
20150320 20150320T123000 20150320T123000Z
out - The StringBuilder to write to.java.io.IOExceptionpublic void writeTo(java.io.Writer out)
throws java.io.IOException
Writer.. The written value conforms to RFC 5545, Section 3.3.5. 20150320 20150320T123000 20150320T123000Z
out - The Writer to write to.java.io.IOException