Class DateTime
- All Implemented Interfaces:
AutoCloseable
CkDateTime represents a date/time value and provides conversion methods for common
date/time formats, including .NET DateTime, DateTime ticks, RFC 822, RFC 3339 / ISO 8601-style
timestamps, Unix time, DOS date/time, OLE DATE, NTP time, ULID timestamps, and Chilkat DtObj. It
also supports date arithmetic, age/expiration checks, serialization, and local/UTC conversion.
For an extended overview, see CkDateTime Class Overview
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDays(int numDays)Adds an integer number of days to the date/time.voidaddSeconds(int numSeconds)Adds an integer number of seconds to the date/time.voiddeSerialize(String serializedDateTime)Loads the date/time with a string having the format as produced by the Serialize method, which is a string of SPACE separated integers containing (in this order) year, month, day, hour, minutes, seconds, and a UTC flag having the value of 1/0.intdiffSeconds(DateTime dateTimeArg)Returns the difference in seconds between thedateTimeArgand this date/time.booleanexpiresWithin(int n, String units)Returnstrueif the date/time is withinnseconds/minutes/hours/days of the current system date/time.longgetAsDateTimeTicks(boolean bLocal)Returns the date/time as a 64-bit integer .NET DateTime value.longgetAsDosDate(boolean bLocal)Returns the date/time as a 32-bit DOS date/time bitmask.getAsIso8601(String formatStr, boolean bLocal)Returns the date/time in a compatible ISO 8601 format according to the format specified informatStr..doublegetAsOleDate(boolean bLocal)Returns the date/time in a Windows OLEDATEformat.getAsRfc822(boolean bLocal)Returns the date/time as an RFC822 formatted string.getAsTimestamp(boolean bLocal)Returns the date/time as an RFC 3339 formatted string, such as1990-12-31T23:59:60Z.longgetAsUnixTime(boolean bLocal)Returns the date/time as a Unix time value, which is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.longgetAsUnixTime64(boolean bLocal)The same as GetUnixTime, except returns the date/time as a 64-bit integer.doublegetAsUnixTimeDbl(boolean bLocal)The same as GetUnixTime, except returns the date/time as a double.getAsUnixTimeStr(boolean bLocal)Returns the time in Unix format (in seconds since the epoch: 00:00:00 UTC on 1 January 1970).intgetIsDst()This is the Daylight Saving Time flag.intFor the current system's timezone, returns the number of seconds offset from UTC for this date/time.booleanReturnstrueif the date/time is older than the current system date/time bynseconds/minutes/hours/days.Serializes the date/time to a us-ascii string that can be imported at a later time via the DeSerialize method.voidSets the date/time from the current system time.voidsetFromDateTimeTicks(boolean bLocal, long ticks)Sets the date/time from a .NET DateTime value represented in ticks.voidsetFromDosDate(boolean bLocal, long t)Sets the date/time from a 32-bit DOS date/time bitmask.voidsetFromDtObj(DtObj dt)Sets the date/time from a ChilkatDtobject.voidsetFromNtpServer(String jsonStr)Sets the date/time by sending a query to an NTP server.voidsetFromNtpTime(int ntpSeconds)Sets the date/time from a 32-bit NTP time value.voidsetFromOleDate(boolean bLocal, double dt)Sets the date/time from a Windows OLEDATEvalue.voidsetFromRfc822(String rfc822Str)Sets the date/time from an RFC822 date/time formatted string.voidsetFromTimestamp(String timestamp)Sets the date/time from an RFC 3339 timestamp format.voidsetFromUlid(boolean bLocal, String ulid)Sets this object's date/time using the timestamp embedded in theulid.voidsetFromUnixTime(boolean bLocal, long t)Sets the date/time from a UNIX time value.voidsetFromUnixTime64(boolean bLocal, long t)The same as SetFromUnixTime, except that it uses a 64-bit integer to solve the Y2038 problem.voidsetFromUnixTimeDbl(boolean bLocal, double d)The same as SetFromUnixTime, except that it uses a double to solve the Y2038 problem.voidCopies the date/time to thedtObj.ulidGenerate(boolean bLocal)Generates and returns a new ULID using this object's date/time value.voidulidIncrement(StringBuilder ulid)Increments the ULID that is passed inulid.voidulidValidate(String ulid)Validates theulid.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
DateTime
public DateTime()Creates a newDateTimeobject.
-
-
Method Details
-
getIsDst
public int getIsDst()This is the Daylight Saving Time flag. It can have one of three possible values: 1, 0, or -1. It has the value 1 if Daylight Saving Time is in effect, 0 if Daylight Saving Time is not in effect, and -1 if the information is not available.Note: This is NOT the DST for the current system time. It is the DST that was in effect at the date value contained in this object.
- Returns:
- the value of the
IsDstproperty (read-only)
-
getUtcOffset
public int getUtcOffset()For the current system's timezone, returns the number of seconds offset from UTC for this date/time. The offset includes daylight savings adjustment. Local timezones west of UTC return a negative offset.- Returns:
- the value of the
UtcOffsetproperty (read-only)
-
addDays
public void addDays(int numDays)Adds an integer number of days to the date/time. To subtract days, pass a negative integer.- Parameters:
numDays- thenumDaysargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addSeconds
public void addSeconds(int numSeconds)Adds an integer number of seconds to the date/time. To subtract seconds, pass a negative integer.- Parameters:
numSeconds- thenumSecondsargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
deSerialize
Loads the date/time with a string having the format as produced by the Serialize method, which is a string of SPACE separated integers containing (in this order) year, month, day, hour, minutes, seconds, and a UTC flag having the value of 1/0.- Parameters:
serializedDateTime- theserializedDateTimeargument (notnull)
-
diffSeconds
Returns the difference in seconds between thedateTimeArgand this date/time. The value returned is this object's date/time -dateTimeArg's date/time. For example, if the returned value is positive, then this object's date/time is more recent thandateTimeArg's date/time. If the return value is negative, then this object's date/time is older thandateTimeArg's date/time.- Parameters:
dateTimeArg- thedateTimeArgargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
expiresWithin
Returnstrueif the date/time is withinnseconds/minutes/hours/days of the current system date/time. Otherwise returnsfalse. Theunitscan beseconds,minutes,hours, ordays(plural or singular).- Parameters:
n- thenargumentunits- theunitsargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
getAsDateTimeTicks
public long getAsDateTimeTicks(boolean bLocal)Returns the date/time as a 64-bit integer .NET DateTime value.bLocalindicates whether a local or UTC time is returned.This is a date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.).
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsDosDate
public long getAsDosDate(boolean bLocal)Returns the date/time as a 32-bit DOS date/time bitmask.bLocalindicates whether a local or UTC time is returned.The DOS date/time format is a bitmask:
24 16 8 0 +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ |Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s| +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ \___________/\________/\_________/ \________/\____________/\_________/ year month day hour minute secondThe year is stored as an offset from 1980. Seconds are stored in two-second increments. (So if the
secondvalue is 15, it actually represents 30 seconds.)- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsIso8601
Returns the date/time in a compatible ISO 8601 format according to the format specified informatStr.. Examples of ISO 8601 formats include the following:YYYY-MM-DD YYYY-MM-DDThh:mmTZD YYYY-MM-DDThh:mm:ssTZD YYYY-MM-DDThh:mm:ss:sssTZDFor the date portion of these formats,
YYYYis a four-digit year representation,MMis a two-digit month, andDDis a two-digit day. For the time portion,hhis the hour in 24-hour notation,mmis the two-digit minutes,ssis the two-digit seconds, andsssis the 3-digit milliseconds. A time designatorTseparates the date and time portions of the string, while a time zone designatorTZDspecifies a time zone (UTC).bLocalindicates whether a local or UTC time is returned.- Parameters:
formatStr- theformatStrargument (notnull)bLocal- thebLocalargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAsOleDate
public double getAsOleDate(boolean bLocal)Returns the date/time in a Windows OLEDATEformat.bLocalindicates whether a local or UTC time is returned.The OLE automation date format is a floating point value, counting days since midnight 30 December 1899. Hours and minutes are represented as fractional days.
- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsRfc822
Returns the date/time as an RFC822 formatted string. (An RFC822 format string is what is found in theDateheader field of an email, such asWed, 18 Oct 2017 09:08:21 GMT.)bLocalindicates whether a local or UTC time is returned.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAsTimestamp
Returns the date/time as an RFC 3339 formatted string, such as1990-12-31T23:59:60Z. (This is an ISO 8061 format like the following: YYYY-MM-DDThh:mm:ssTZD)bLocalindicates whether a local or UTC time is returned.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAsUnixTime
public long getAsUnixTime(boolean bLocal)Returns the date/time as a Unix time value, which is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.bLocalindicates whether the date/time returned is local or UTC.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsUnixTime64
public long getAsUnixTime64(boolean bLocal)The same as GetUnixTime, except returns the date/time as a 64-bit integer.bLocalindicates whether a local or UTC time is returned.Note: 64-bit integers are needed after Jan 19, 2038. The Y2038 problem happens because some systems store Unix time as a signed 32-bit number of seconds since Jan 1, 1970. This number runs out on Jan 19, 2038, causing the time to wrap around to a date in 1901.
- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsUnixTimeDbl
public double getAsUnixTimeDbl(boolean bLocal)The same as GetUnixTime, except returns the date/time as a double.bLocalindicates whether a local or UTC time is returned.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getAsUnixTimeStr
Returns the time in Unix format (in seconds since the epoch: 00:00:00 UTC on 1 January 1970).bLocalindicates whether the date/time returned is local or UTC.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
olderThan
Returnstrueif the date/time is older than the current system date/time bynseconds/minutes/hours/days. Otherwise returnsfalse. Theunitscan beseconds,minutes,hours, ordays(plural or singular).- Parameters:
n- thenargumentunits- theunitsargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
serialize
Serializes the date/time to a us-ascii string that can be imported at a later time via the DeSerialize method. The format of the string returned by this method is not intended to match any published standard. It is formatted to a string with SPACE separated integers containing (in this order) year, month, day, hour, minutes, seconds, and a UTC flag having the value of 1 or 0.- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromCurrentSystemTime
public void setFromCurrentSystemTime()Sets the date/time from the current system time.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromDateTimeTicks
public void setFromDateTimeTicks(boolean bLocal, long ticks)Sets the date/time from a .NET DateTime value represented in ticks. See GetAsDateTimeTicks for more information.bLocalindicates whether the passed in date/time is local or UTC.- Parameters:
bLocal- thebLocalargumentticks- theticksargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromDosDate
public void setFromDosDate(boolean bLocal, long t)Sets the date/time from a 32-bit DOS date/time bitmask. See GetAsDosDate for more information.- Parameters:
bLocal- thebLocalargumentt- thetargument (0 .. 4294967295)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromDtObj
Sets the date/time from a ChilkatDtobject.- Parameters:
dt- thedtargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromNtpServer
Sets the date/time by sending a query to an NTP server.- Parameters:
jsonStr- thejsonStrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromNtpTime
public void setFromNtpTime(int ntpSeconds)Sets the date/time from a 32-bit NTP time value.ntpSecondsis the number of seconds since 00:00 (midnight) 1 January 1900 GMT.- Parameters:
ntpSeconds- thentpSecondsargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromOleDate
public void setFromOleDate(boolean bLocal, double dt)Sets the date/time from a Windows OLEDATEvalue.bLocalindicates whether the passed in date/time is local or UTC.Note: This method was not working correctly. The problem was discovered just after releasing v9.5.0.65. It will be fixed in the next version update.
- Parameters:
bLocal- thebLocalargumentdt- thedtargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromRfc822
Sets the date/time from an RFC822 date/time formatted string. Here are some examples of RFC822 formatted date/times:Tue, 15 Nov 2022 22:00:58 +0000 Tue, 15 Nov 2022 20:21:50 +0100 Tue, 01 Nov 2022 18:09:41 -0600
- Parameters:
rfc822Str- therfc822Strargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromTimestamp
Sets the date/time from an RFC 3339 timestamp format. (such as1990-12-31T23:59:60Z:)(This is an ISO 8061 format like the following: YYYY-MM-DDThh:mm:ssTZD)
Note: Starting in v9.5.0.77, strings formatted as
YYMMDDhhmmssZ, such as181221132225Z, can also be passed to this method.- Parameters:
timestamp- thetimestampargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromUlid
Sets this object's date/time using the timestamp embedded in theulid. If theulidwas not a valid ULID, returnsfalseand the date/time is not set.bLocalindicates whether theuliduses a local time or UTC time.- Parameters:
bLocal- thebLocalargumentulid- theulidargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromUnixTime
public void setFromUnixTime(boolean bLocal, long t)Sets the date/time from a UNIX time value.bLocalindicates whether the passed in date/time is local or UTC.- Parameters:
bLocal- thebLocalargumentt- thetargument (0 .. 4294967295)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromUnixTime64
public void setFromUnixTime64(boolean bLocal, long t)The same as SetFromUnixTime, except that it uses a 64-bit integer to solve the Y2038 problem.bLocalindicates whether the passed in date/time is local or UTC.Note: 64-bit integers are needed after Jan 19, 2038. The Y2038 problem happens because some systems store Unix time as a signed 32-bit number of seconds since Jan 1, 1970. This number runs out on Jan 19, 2038, causing the time to wrap around to a date in 1901.
- Parameters:
bLocal- thebLocalargumentt- thetargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setFromUnixTimeDbl
public void setFromUnixTimeDbl(boolean bLocal, double d)The same as SetFromUnixTime, except that it uses a double to solve the Y2038 problem. (See GetAsUnixTime for more information about Y2038).bLocalindicates whether the passed in date/time is local or UTC.- Parameters:
bLocal- thebLocalargumentd- thedargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
toDtObj
Copies the date/time to thedtObj. Gets the date/time in the local time ifbLocalistrue, or GMT ifbLocalisfalse.- Parameters:
bLocal- thebLocalargumentdtObj- thedtObjargument (notnull)
-
ulidGenerate
Generates and returns a new ULID using this object's date/time value.bLocalindicates whether to use local time or UTC time.- Parameters:
bLocal- thebLocalargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
ulidIncrement
Increments the ULID that is passed inulid.- Parameters:
ulid- theulidargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
ulidValidate
Validates theulid. Returnstrueif theulidis valid, otherwise returnsfalse.- Parameters:
ulid- theulidargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-