public class DateTime extends Object implements Serializable, Comparable<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 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();
".
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
DateTime()
Default constructor gives current time to millisecond.
|
DateTime(IDateTimeConfig config)
DateTime with a specified config
|
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,
String tzId)
Construct a DateTime from seconds and fractional seconds.
|
DateTime(long seconds,
int nanos,
TimeZone tz)
Construct a DateTime from seconds and fractional seconds.
|
DateTime(long millis,
String tzId)
DateTime constructed from time in milliseconds since epoch.
|
DateTime(long millis,
TimeZone tz)
DateTime constructed from time in milliseconds since epoch.
|
DateTime(String str)
DateTime constructed from a string using global defaults.
|
DateTime(String str,
IDateTimeConfig config)
DateTime constructed from a string using specified defaults.
|
DateTime(String str,
TimeZone tz)
DateTime parsed from a string at a specified time zone
|
DateTime(String str,
TimeZone inputTz,
TimeZone outputTz)
DateTime parsed from a string at a specified time zone
|
DateTime(Timestamp ts)
DateTime constructed from a Timestamp includes nanos.
|
Modifier and Type | Method and Description |
---|---|
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.
|
static void |
assignIntegersToRemainingSlots(IDateTimeConfig config,
org.pojava.datetime.DateTime.HasDatepart hasDatepart,
org.pojava.datetime.DateTime.DateState dateState) |
int |
compareTo(DateTime other)
Compare two DateTime objects to determine ordering.
|
IDateTimeConfig |
config()
Return the global configuration used by DateTime.
|
boolean |
equals(Object dateTime)
This compares a DateTime with another DateTime.
|
int |
getNanos()
Positive nanosecond offset from Seconds.
|
long |
getSeconds()
Whole seconds offset from epoch.
|
int |
hashCode() |
static DateTime |
parse(String str)
Interpret a DateTime from a String using global defaults.
|
static DateTime |
parse(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(String iso8601)
Shift this DateTime +/- a Shift offset specified as an ISO 8601 string.
|
TimeZone |
timeZone()
Get the TimeZone used for formatted string output
|
Date |
toDate()
Get Date/Time as a Java Date object.
|
long |
toMillis()
Whole milliseconds offset from epoch.
|
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.
|
String |
toString(String format)
Return a String according to the provided format.
|
String |
toString(String format,
Locale locale)
Return a String according to the provided format.
|
String |
toString(String format,
TimeZone tz)
Return a String according to the provided format.
|
String |
toString(String format,
TimeZone tz,
Locale locale)
Return a String according to the provided format.
|
String |
toString(TimeZone tz)
Return a String according to the provided format.
|
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, ...
|
protected IDateTimeConfig config
protected Duration systemDur
public DateTime()
public DateTime(IDateTimeConfig config)
public DateTime(long millis)
millis
- time since epochpublic DateTime(long millis, IDateTimeConfig config)
millis
- time since epochconfig
- configuration defaultspublic DateTime(long millis, TimeZone tz)
millis
- Number of milliseconds since epochtz
- Override the output Time Zonepublic DateTime(long millis, String tzId)
millis
- Number of milliseconds since epochtzId
- Override the output time zonepublic DateTime(long seconds, int nanos)
seconds
- Number of seconds since epoch (typically 1970-01-01)nanos
- Nanosecond offset in range +/- 999999999public DateTime(long seconds, int nanos, TimeZone tz)
seconds
- Number of seconds since epoch (typically 1970-01-01)nanos
- Nanosecond offset in range +/- 999999999tz
- Override the output time zonepublic DateTime(long seconds, int nanos, String tzId)
seconds
- Number of seconds since epoch (typically 1970-01-01)nanos
- Nanosecond offset in range +/- 999999999tzId
- Override the output time zonepublic 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(String str)
str
- String holding date to parsepublic DateTime(String str, IDateTimeConfig config)
str
- String to parseconfig
- Custom configuration optionspublic DateTime(String str, TimeZone tz)
str
- Date string to parsetz
- Time zone used for both parsing input and formatting outputpublic DateTime(String str, TimeZone inputTz, TimeZone outputTz)
str
- Date string to parseinputTz
- Time Zone of the date being parsedoutputTz
- Time Zone under which toString will format datespublic DateTime(Timestamp ts)
ts
- Timestamppublic int compareTo(DateTime other)
compareTo
in interface Comparable<DateTime>
other
- DateTime to compare to thispublic Timestamp toTimestamp()
public Date toDate()
public TimeZone timeZone()
public String toString()
public String toString(String format)
format
- Date format specifierpublic String toString(String format, TimeZone tz)
format
- Date format specifiertz
- Show formatted date & time at the given TimeZonepublic String toString(String format, Locale locale)
format
- Date format specifierlocale
- Show formatted date & time at the given TimeZonepublic String toString(TimeZone tz)
tz
- Show formatted date & time at the given TimeZonepublic String toString(String format, TimeZone tz, Locale locale)
format
- Date format specifiertz
- Show formatted date & time at the given TimeZonelocale
- Display date words like month or day of week in a given language.public DateTime add(Duration dur)
dur
- Durationpublic DateTime add(long milliseconds)
milliseconds
- Duration in millisecondspublic DateTime add(CalendarUnit calUnit, int qty)
calUnit
- CalendarUnit (MINUTE, DAY, WEEK, MONTH, etc)qty
- May be positive or negative.public DateTime shift(CalendarUnit calUnit, int qty)
calUnit
- CalendarUnit (MINUTE, DAY, WEEK, MONTH, etc)qty
- May be positive or negative.public DateTime shift(Shift shift)
shift
- a pre-defined shift of various calendar time increments.public DateTime shift(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(String str)
str
- Date/Time string to be parsed.public static void assignIntegersToRemainingSlots(IDateTimeConfig config, org.pojava.datetime.DateTime.HasDatepart hasDatepart, org.pojava.datetime.DateTime.DateState dateState)
public static DateTime parse(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(Object dateTime)
public IDateTimeConfig config()
Copyright © 2015. All Rights Reserved.