|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.pojava.datetime.DateTime
public class DateTime
DateTime provides an immutable representation of Date and Time to the nearest nanosecond. You can access DateTime properties either in milliseconds or in seconds and nanoseconds. Both the seconds and milliseconds values can be understood as being truncated to their respective precisions. Nanos holds the fractional portion of a second in the range 0-999999999. Note that whether seconds is positive or negative, the internal values will be adjusted if necessary to support a positive value for nanos.
You may think of a DateTime object as a fixed offset of System time measured from the Unix epoch in non-leap milliseconds or non-leap seconds and nanoseconds. Leap years are calculated according to the Gregorian Calendar, matching the same interpretation as the java.util.Date object (every 4th year is a leap year, except for years divisible by 100 but not divisible by 400). The times are stored according to the UTC (aka GMT) time zone, and a TimeZone object is referenced to translate to a local time zone.
DateTime includes a robust parser for interpreting date and time from a String. It parses a date and time using heuristics rather than comparing against preset formats, so it is point-and-shoot simple. The following, for example, are interpreted the same:
Some notes on the date interpretations:
All dates are interpreted in your local time zone, unless a time zone is specified in the String. Time zones are configurable in the DateTimeConfig object, so you can determine for your own application whether CST, for example, would adjust to Central Standard Time or Chinese Standard Time.
A two-digit year will assume up to 80 years in the past and 20 years in the future. It is prudent in many cases to follow this with a check based on whether you know the date to represent a past or future date. If you know you parsed a birthday, you can compare with today's date and subtract 100 yrs if needed (references to birthdays 20 years in the future are rare). Similarly, if you're dealing with an annuity date, you can add 100 years if the parsed date occurred in the past.
If you're parsing European dates expecting DD/MM/YYYY instead of MM/DD/YYYY, then you can
alter the global DateTimeConfig setting by first calling, "
DateTimeConfig.globalEuropeanDateFormat();".
| Field Summary | |
|---|---|
protected IDateTimeConfig |
config
Config contains info specific to zoning and formatting. |
protected Duration |
systemDur
System time is a lazy calculation of milliseconds from Unix epoch 1970-01-01 00:00:00, assuming no leap seconds and a leap year every year evenly divisible by 4, except for years divisible by 100 but not divisible by 400. |
| Constructor Summary | |
|---|---|
DateTime()
Default constructor gives current time to millisecond. |
|
DateTime(long millis)
DateTime constructed from time in milliseconds since epoch. |
|
DateTime(long millis,
IDateTimeConfig config)
DateTime constructed from time in milliseconds since epoch. |
|
DateTime(long seconds,
int nanos)
Construct a DateTime from seconds and fractional seconds. |
|
DateTime(long seconds,
int nanos,
IDateTimeConfig config)
Construct a DateTime from seconds and fractional seconds. |
|
DateTime(long seconds,
int nanos,
java.lang.String tzId)
Construct a DateTime from seconds and fractional seconds. |
|
DateTime(long seconds,
int nanos,
java.util.TimeZone tz)
Construct a DateTime from seconds and fractional seconds. |
|
DateTime(long millis,
java.lang.String tzId)
DateTime constructed from time in milliseconds since epoch. |
|
DateTime(long millis,
java.util.TimeZone tz)
DateTime constructed from time in milliseconds since epoch. |
|
DateTime(java.lang.String str)
DateTime constructed from a string using global defaults. |
|
DateTime(java.lang.String str,
IDateTimeConfig config)
DateTime constructed from a string using global defaults. |
|
DateTime(java.sql.Timestamp ts)
DateTime constructed from a Timestamp includes nanos. |
|
| Method Summary | |
|---|---|
DateTime |
add(CalendarUnit calUnit,
int qty)
Add +/- a block of time to a date in it's OutputTimeZone. |
DateTime |
add(Duration dur)
Add a fixed duration of time |
DateTime |
add(long milliseconds)
Add a fixed duration in milliseconds. |
int |
compareTo(DateTime other)
Compare two DateTime objects to determine ordering. |
IDateTimeConfig |
config()
Return the global configuration used by DateTime. |
boolean |
equals(java.lang.Object dateTime)
This compares a DateTime with another DateTime. |
int |
getNanos()
Positive nanosecond offset from Seconds. |
long |
getSeconds()
Whole seconds offset from epoch. |
java.lang.String |
getTimeZoneId()
Deprecated. After parse, only config determines output. |
int |
hashCode()
|
static DateTime |
parse(java.lang.String str)
Interpret a DateTime from a String using global defaults. |
static DateTime |
parse(java.lang.String str,
IDateTimeConfig config)
Interpret a DateTime from a String. |
DateTime |
shift(CalendarUnit calUnit,
int qty)
Add increments of any calendar time unit from a nanosecond to a century. |
DateTime |
shift(Shift shift)
Shift this DateTime +/- a Shift offset. |
DateTime |
shift(java.lang.String iso8601)
Shift this DateTime +/- a Shift offset specified as an ISO 8601 string. |
java.util.TimeZone |
timeZone()
Get the TimeZone |
java.util.Date |
toDate()
Get Date/Time as a Java Date object. |
java.lang.String |
toLocalString()
Deprecated. |
java.lang.String |
toLocalString(java.lang.String format)
Deprecated. |
long |
toMillis()
Whole milliseconds offset from epoch. |
java.lang.String |
toString()
By default, the toString method gives a sortable ISO 8601 date and time to nearest second in the same time zone as the system. |
java.lang.String |
toString(java.lang.String format)
Return a String according to the provided format. |
java.lang.String |
toString(java.lang.String format,
java.util.TimeZone tz)
Return a String according to the provided format. |
java.lang.String |
toString(java.lang.String format,
java.util.TimeZone tz,
java.util.Locale locale)
Return a String according to the provided format. |
java.sql.Timestamp |
toTimestamp()
Get a timestamp useful for JDBC |
DateTime |
truncate(CalendarUnit unit)
Truncate DateTime down to its nearest time unit as a time. |
int |
weekday()
Return numeric day of week, usually Sun=1, Mon=2, ... |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected IDateTimeConfig config
protected Duration systemDur
| Constructor Detail |
|---|
public DateTime()
public DateTime(long millis)
millis - time
public DateTime(long millis,
IDateTimeConfig config)
millis - config - time
public DateTime(long millis,
java.util.TimeZone tz)
millis - Number of milliseconds since epochtz - Override the output Time Zone
public DateTime(long millis,
java.lang.String tzId)
millis - Number of milliseconds since epochtzId - Override the output time zone
public DateTime(long seconds,
int nanos)
seconds - Number of seconds since epoch (typically 1970-01-01)nanos - Nanosecond offset in range +/- 999999999
public DateTime(long seconds,
int nanos,
java.util.TimeZone tz)
seconds - Number of seconds since epoch (typically 1970-01-01)nanos - Nanosecond offset in range +/- 999999999tz - Override the output time zone
public DateTime(long seconds,
int nanos,
java.lang.String tzId)
seconds - Number of seconds since epoch (typically 1970-01-01)nanos - Nanosecond offset in range +/- 999999999tzId - Override the output time zone
public DateTime(long seconds,
int nanos,
IDateTimeConfig config)
seconds - Number of seconds since epoch (typically 1970-01-01)nanos - Nanosecond offset in range +/- 999999999config - Provide custom configuration optionspublic DateTime(java.lang.String str)
str -
public DateTime(java.lang.String str,
IDateTimeConfig config)
str - String to parseconfig - Custom configuration optionspublic DateTime(java.sql.Timestamp ts)
ts - Timestamp| Method Detail |
|---|
public int compareTo(DateTime other)
compareTo in interface java.lang.Comparable<DateTime>other - DateTime to compare to this
public java.sql.Timestamp toTimestamp()
public java.util.Date toDate()
public java.util.TimeZone timeZone()
public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toString(java.lang.String format)
format -
public java.lang.String toString(java.lang.String format,
java.util.TimeZone tz)
format - tz - Show formatted date & time at the given TimeZone
public java.lang.String toString(java.lang.String format,
java.util.TimeZone tz,
java.util.Locale locale)
format - tz - Show formatted date & time at the given TimeZonelocale - Display date words like month or day of week in a given language.
public java.lang.String toLocalString()
public java.lang.String toLocalString(java.lang.String format)
format -
public DateTime add(Duration dur)
dur -
public DateTime add(long milliseconds)
milliseconds -
public DateTime add(CalendarUnit calUnit,
int qty)
calUnit - qty -
public DateTime shift(CalendarUnit calUnit,
int qty)
calUnit - qty - May be positive or negative.
public DateTime shift(Shift shift)
shift - a pre-defined shift of various calendar time increments.
public DateTime shift(java.lang.String iso8601)
iso8601 - A string of format "P[#Y][#M][#D][T[#H][#M][#S[.#]]" holding a list of
offsets.
public int weekday()
public static DateTime parse(java.lang.String str)
str - Date/Time string to be parsed.
public static DateTime parse(java.lang.String str,
IDateTimeConfig config)
str - Date/Time string to be parsed.config - Configuration parameters governing parsing and presentation.
public DateTime truncate(CalendarUnit unit)
unit - Unit of time to which new DateTime will be truncated.
public long getSeconds()
public long toMillis()
public int getNanos()
public boolean equals(java.lang.Object dateTime)
equals in class java.lang.ObjectdateTime - DateTime to which this DateTime will be compared.
public int hashCode()
hashCode in class java.lang.Objectpublic IDateTimeConfig config()
public java.lang.String getTimeZoneId()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||