Class ZDate
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Date>
public final class ZDate extends Date
Date that provides extra utility fields and methods.- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static StringDATE_FORMATRepresents"d/M/yyyy HH:mm"time format.static intDAYNumber of milliseconds in a day (8,640,000).static StringDAY_MONTH_HOUR_MINUTERepresents"dd/MM-HH:mm"time format.static ZDateDAY_OF_MESSIAHThe latest possible date (Long.MAX_VALUE).static StringFULL_DATERepresents"dd/MM/yy-HH:mm:ss"time format.static StringFULL_DATE_MILLISECONDSRepresents"dd/MM/yy-HH:mm:ss:SSS"time format.static StringFULL_TIMESTAMPRepresents"ddMMyyHHmmssSSS"time format.static intHOURNumber of milliseconds in an hour (360,000).static StringHOUR_MINUTE_SECONDRepresents"HH:mm:ss"time format.static intMINUTENumber of milliseconds in a minute (60,000).static StringMINUTE_TIMESTAMPRepresents"ddMMyyHHmm"time format.static StringMMDDYYHHMMSSRepresents"MMddyyHHmmss"time format.static intSECONDNumber of milliseconds in a second (1000).static StringSORTABLERepresents"yyyy-MM-dd'T'HH:mm:ss"time format.static StringSQL_DATERepresents"yyyy-MM-dd HH:mm:ss"time format.static StringTIMESTAMPRepresents"ddMMyyHHmmss"time format.static StringYYMMDDRepresents"yyMMdd"time format.static StringYYMMDDHHMMRepresents"yyMMddHHmm"time format.static StringYYMMDDHHMMSSRepresents"yyMMddHHmmss"time format.static StringYYYYMMDDHHMMRepresents"yyyyMMddHHmm"time format.static StringYYYYMMDDHHMMSSRepresents"yyyyMMddHHmmss"time format. -
Constructor Summary
Constructors Constructor Description ZDate()Constructs an object representing the current time, measured to the nearest millisecond.ZDate(float seconds)Constructs an object representing the date of today, with the given amount of seconds, possibly with a faction, from midnight in local time.ZDate(int hour, int minute)Constructs an object representing the date of today, with the given hour and minute in local time.ZDate(long date)Constructs an object from a value of milliseconds since 1/1/1970 GMT.ZDate(String string, String fmt)Constructs an object by parsing the beginning of the given string with a given time format.ZDate(Date date)Constructs an object from the value represented by a givenDateobject. -
Method Summary
Modifier and Type Method Description ZDateadd(int field, int amount)Returns a time object with a value equal to the time in this object plus the given amount of the given time units.ZDateadd(long ms)Returns a time object with a value equal to the time in this object plus the given amount of milliseconds.booleanafter(Date when)Returns true if the time of this object is after the time of the given object or if the given object isnull.booleanbefore(Date when)Returns true if the time of the given object is not null and the time of this object is before the time of the given object.booleanbetween(ZDate from, ZDate to)Returns true if this object is between the two given time objects, inclusive.longdiff(Date date)Returns the difference of time in milliseconds from the time represented by thedateparameter till the time represented by this object.intdiffMonths(Date date)Returns the difference in whole months from the time represented by thedateparameter till the time represented by this object.ZDateearliest(ZDate when)Returns the earliest between this object and the given object.longelapsed()Returns the time that has elapsed since the time represented by this object in milliseconds.Stringformat(String fmt)Formats this object into a time string.StringformatFullTimestamp()Formats this object into a timestamp string using the format"ddMMyyHHmmssSSS".StringformatGMT(String fmt)Formats this object into a time string representing the time of the object in GMT zone.StringformatRoundTimestamp(int precision)Formats this object into a rounded timestamp string using the format"ddMMyyHHmmssS(s)"when the number ofSs is the given precision.ZDatefromGMT()Returns a time object that represents the time of this object after shifting it from GMT time zone.static ZDatefromSQLdate(Date date)Converts anSQL Dateobject to aZDateobject.static ZDatefromSQLtime(Timestamp ts)Converts anSQL Timestampobject to aZDateobject.static ZDatefromXMLGregorianCalendar(XMLGregorianCalendar xDate)Converts anXMLGregorianCalendarobject to aZDateobject.intgetDay()Returns the day of the week represented by this time object.intgetHours()Returns the hour of the day that this time object represents as an integer number between 0 and 23.ZDategetMidnight()Returns a time object representing the latest midnight that precedes or equals this object.intgetMinutes()Returns the minutes fraction of this time object as an integer number between 0 and 59.intgetSeconds()Returns the seconds fraction of this time object as an integer number between 0 and 59.booleanhasCrossed(long ms)Returns true if since the time of the object until now a certain point of time was crossed.ZDatelatest(ZDate when)Returns the latest between this object and the given object.static ZDatenow()Returns a new time object set to the current time.static Stringnow(String fmt)Returns the current time formatted into a string.static longnowAsLong()Returns the current time as milliseconds since 1/1/1970.static ZDateparseFullTimestamp(String string)Parses the beginning of the given string assuming it contains a timestamp value in the format"ddMMyyHHmmssSSS".static ZDateparseGMTtime(String string, String format)Parses the beginning of the given string assuming it contains a formatted GMT time string.static ZDateparseRoundTime(String string, int precision)Parses the beginning of the given string assuming it contains a rounded timestamp string formatted as"ddMMyyHHmmssS(s)"when the number ofSs is the given precision.static ZDateparseTime(String string, String format, ZDate ts)Parses a formatted time string using the given date object to resolve full time data.ZDateround(long roundBy)Returns a time object equal to the time represented by this object rounded to the given amount of milliseconds.ZDatesetTime(int hour, int minutes)Returns a time object with the date of this object and the given hour and minute.static ZDatetoday()Returns a new time object representing the midnight that started the current dayZDatetoGMT()Returns a time object that represents the time of this object after shifting it to GMT time zone.DatetoSQLdate()Returns the time of this object as anSQL Dateobject.TimestamptoSQLtime()Returns the time of this object as anSQL Timestampobject.XMLGregorianCalendartoXMLGregorianCalendar(DatatypeFactory factory)Returns the time of this object as anXMLGregorianCalendarobject, using the given factory for conversion.ZDatetruncate(long truncateBy)Returns a time object equal to the time represented by this object truncated to a floor value that is a whole multiply of the given amount of milliseconds.Methods inherited from class java.util.Date
clone, compareTo, equals, from, getDate, getMonth, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setTime, setYear, toGMTString, toInstant, toLocaleString, toString, UTC
-
Field Details
-
DATE_FORMAT
Represents"d/M/yyyy HH:mm"time format.- See Also:
- Constant Field Values
-
FULL_DATE
Represents"dd/MM/yy-HH:mm:ss"time format.- See Also:
- Constant Field Values
-
FULL_DATE_MILLISECONDS
Represents"dd/MM/yy-HH:mm:ss:SSS"time format.- See Also:
- Constant Field Values
-
DAY_MONTH_HOUR_MINUTE
Represents"dd/MM-HH:mm"time format.- See Also:
- Constant Field Values
-
HOUR_MINUTE_SECOND
Represents"HH:mm:ss"time format.- See Also:
- Constant Field Values
-
TIMESTAMP
Represents"ddMMyyHHmmss"time format.- See Also:
- Constant Field Values
-
FULL_TIMESTAMP
Represents"ddMMyyHHmmssSSS"time format.- See Also:
- Constant Field Values
-
MINUTE_TIMESTAMP
Represents"ddMMyyHHmm"time format.- See Also:
- Constant Field Values
-
YYYYMMDDHHMM
Represents"yyyyMMddHHmm"time format.- See Also:
- Constant Field Values
-
YYYYMMDDHHMMSS
Represents"yyyyMMddHHmmss"time format.- See Also:
- Constant Field Values
-
YYMMDD
Represents"yyMMdd"time format.- See Also:
- Constant Field Values
-
YYMMDDHHMM
Represents"yyMMddHHmm"time format.- See Also:
- Constant Field Values
-
YYMMDDHHMMSS
Represents"yyMMddHHmmss"time format.- See Also:
- Constant Field Values
-
MMDDYYHHMMSS
Represents"MMddyyHHmmss"time format.- See Also:
- Constant Field Values
-
SQL_DATE
Represents"yyyy-MM-dd HH:mm:ss"time format.- See Also:
- Constant Field Values
-
SORTABLE
Represents"yyyy-MM-dd'T'HH:mm:ss"time format.- See Also:
- Constant Field Values
-
SECOND
public static final int SECONDNumber of milliseconds in a second (1000).- See Also:
- Constant Field Values
-
MINUTE
public static final int MINUTENumber of milliseconds in a minute (60,000).- See Also:
- Constant Field Values
-
HOUR
public static final int HOURNumber of milliseconds in an hour (360,000).- See Also:
- Constant Field Values
-
DAY
public static final int DAYNumber of milliseconds in a day (8,640,000).- See Also:
- Constant Field Values
-
DAY_OF_MESSIAH
The latest possible date (Long.MAX_VALUE).
-
-
Constructor Details
-
ZDate
public ZDate()Constructs an object representing the current time, measured to the nearest millisecond.- See Also:
now()
-
ZDate
public ZDate(long date)Constructs an object from a value of milliseconds since 1/1/1970 GMT.- Parameters:
date- the milliseconds since January 1, 1970, 00:00:00 GMT.
-
ZDate
Constructs an object from the value represented by a givenDateobject.- Parameters:
date- a Date object whose value shall be copied to the constructed object.
-
ZDate
public ZDate(int hour, int minute)Constructs an object representing the date of today, with the given hour and minute in local time.- Parameters:
hour- hour to be set.minute- minute to be set.
-
ZDate
public ZDate(float seconds)Constructs an object representing the date of today, with the given amount of seconds, possibly with a faction, from midnight in local time.- Parameters:
seconds- amount of seconds, possibly with a faction, from midnight in local time.
-
ZDate
Constructs an object by parsing the beginning of the given string with a given time format.- Parameters:
string- the string to parsefmt- the format to use- Throws:
ParseException- See Also:
SimpleDateFormat,SimpleDateFormat.parse()
-
-
Method Details
-
now
Returns a new time object set to the current time.Calling
ZDate.now()is equivalent tonew ZDate().- Returns:
- a new time object set to the current time.
- See Also:
ZDate()
-
today
Returns a new time object representing the midnight that started the current day- Returns:
- a new time object representing the midnight that started the current day
-
now
Returns the current time formatted into a string.This method is equivalent to
new ZDate.format(fmt).- Parameters:
fmt- format string- Returns:
- the current time formatted into a string.
- See Also:
format()
-
nowAsLong
public static long nowAsLong()Returns the current time as milliseconds since 1/1/1970.- Returns:
- the current time as milliseconds since 1/1/1970.
-
add
Returns a time object with a value equal to the time in this object plus the given amount of milliseconds.- Parameters:
ms- the amount of milliseconds to add.- Returns:
- a time object with a value equal to the time in this object plus the given amount of milliseconds.
-
add
Returns a time object with a value equal to the time in this object plus the given amount of the given time units.This method adds the specified amount of the unit defined by the
fieldparameter to the time of this object and returns the result as a new time object. The possible values offieldare those defined by the Field numbers inCalendarclass. For instance,add(Calendar.DAY_OF_MONTH, -5)returns a time object that represents the time of 5 days before the time of the object the method is called on.- Parameters:
field- the time unit as defined byCalendarclass.amount- the amount of units to add.- Returns:
- a time object with a value equal to the time in this object plus the specified amount of the specified time units.
-
diff
Returns the difference of time in milliseconds from the time represented by thedateparameter till the time represented by this object.- Parameters:
date- a time object to calculate difference from.- Returns:
- the difference of time in milliseconds from the time represented by the parameter till the time represented by this object.
-
diffMonths
Returns the difference in whole months from the time represented by thedateparameter till the time represented by this object.- Parameters:
date- a time object to calculate difference from.- Returns:
- the difference in whole months from the time represented by the parameter till the time represented by this object, or zero if the parameter is null.
-
elapsed
public long elapsed()Returns the time that has elapsed since the time represented by this object in milliseconds.- Returns:
- the time that has elapsed since the time represented by this object in milliseconds.
-
after
Returns true if the time of this object is after the time of the given object or if the given object isnull. -
before
Returns true if the time of the given object is not null and the time of this object is before the time of the given object. -
between
Returns true if this object is between the two given time objects, inclusive.The method returns true if and only if the time of this object is not before the time of
fromparameter and not after the time oftoparameter. Iffromisnullthen it is considered as the time of the Big Bang, i.e. ever. Iftoisnullit is considered as the coming of Messiah, i.e. forever.- Parameters:
from- from time, null is considered as infinite time ago.to- to time, null is considered as infinite time ahead.- Returns:
- true if and only if the object is not before from and not after to.
-
earliest
Returns the earliest between this object and the given object.If the given time object is
nullor this time object is strictly beforewhenthen this object is returned, otherwisewhenis returned.- Parameters:
when- time object to compare to.- Returns:
- this object if it is before the given object or the given object is null, otherwise return the given object.
-
latest
Returns the latest between this object and the given object.If the given time object is
nullor this time object is strictly afterwhenthen this object is returned, otherwisewhenis returned.- Parameters:
when- time object to compare to.- Returns:
- this object is it is after the given object or the given object is null, otherwise return the given object.
-
getHours
public int getHours()Returns the hour of the day that this time object represents as an integer number between 0 and 23.The method overrides the depreciated
Date.getHours()and uses theCalendar.get(Calendar.HOUR_OF_DAY)replacement. -
getMinutes
public int getMinutes()Returns the minutes fraction of this time object as an integer number between 0 and 59.The method overrides the depreciated
Date.getMinutes()and uses theCalendar.get(Calendar.MINUTE)replacement.- Overrides:
getMinutesin classDate- Returns:
- the minutes fraction of this time object in the local time zone as an integer number between 0 and 59.
-
getSeconds
public int getSeconds()Returns the seconds fraction of this time object as an integer number between 0 and 59.The method overrides the depreciated
Date.getSeconds()and uses theCalendar.get(Calendar.SECOND)replacement.- Overrides:
getSecondsin classDate- Returns:
- the seconds fraction of this time object as an integer number between 0 and 59.
-
getDay
public int getDay()Returns the day of the week represented by this time object.The method overrides the depreciated
Date.getDay()and uses theCalendar.get(Calendar.DAY_OF_WEEK)replacement. -
setTime
Returns a time object with the date of this object and the given hour and minute.The method returns a new time object whose value is a combination of the date of this object with the given
hourandminutes. Seconds and milliseconds are set to zero.- Parameters:
hour- the given hourminutes- the given minutes- Returns:
- an object with the date of this object and the given hour and minute.
-
format
Formats this object into a time string.- Parameters:
fmt- the given format.- Returns:
- the formatted string.
- See Also:
SimpleDateFormat.format()
-
formatFullTimestamp
Formats this object into a timestamp string using the format"ddMMyyHHmmssSSS".- Returns:
- the formatted string.
-
formatRoundTimestamp
Formats this object into a rounded timestamp string using the format"ddMMyyHHmmssS(s)"when the number ofSs is the given precision.- Parameters:
precision- 0 - round by seconds, 1 - round by 10th of a second, 2 - round by 100th of a second, 3 - round by milliseconds (native format).- Returns:
- the formatted string.
-
formatGMT
Formats this object into a time string representing the time of the object in GMT zone.- Parameters:
fmt- the given format.- Returns:
- the formatted string.
-
parseTime
Parses a formatted time string using the given date object to resolve full time data.Parses the beginning of the
stringparameter assuming it contains a time value formatted with theformatparameter. The formatted string may contain partial information, for instance only the time of the day, in which case the method uses thetsparameter to resolve the full time value. Iftsisnullthe current time is used.- Parameters:
string- the string to parse.format- the format to use.ts- the base timestamp. If null, the current time is used.- Returns:
- the parsed and resolved time object.
- Throws:
ParseException
-
parseGMTtime
Parses the beginning of the given string assuming it contains a formatted GMT time string.- Parameters:
string- the string to parse.format- the format to use.- Returns:
- the parsed time object.
- Throws:
ParseException
-
parseFullTimestamp
Parses the beginning of the given string assuming it contains a timestamp value in the format"ddMMyyHHmmssSSS".- Parameters:
string- the string to parse.- Returns:
- the parsed time object.
- Throws:
ParseException
-
parseRoundTime
Parses the beginning of the given string assuming it contains a rounded timestamp string formatted as"ddMMyyHHmmssS(s)"when the number ofSs is the given precision.- Parameters:
string- the string to parse.precision- 0 - round by seconds, 1 - round by 10th of a second, 2 - round by 100th of a second, 3 - round by milliseconds (native format).- Returns:
- the parsed time object.
- Throws:
ParseException
-
toGMT
Returns a time object that represents the time of this object after shifting it to GMT time zone.This method assumes that the time represented by this object is in the local time zone. It allocates a new time object and sets it to the literal time of this object shifted to GMT. For instance if this object represents the time 2pm PST and the local time zone is PST, the returned object represents the time 2pm GMT.
- Returns:
- a time object that represents the time of this object after shifting it to GMT time zone.
-
fromGMT
Returns a time object that represents the time of this object after shifting it from GMT time zone.This method assumes that the time represented by this object is in GMT time zone. It allocates a new time object and sets it to the literal time of this object shifted to the local time zone. For instance if this object represents the time 2pm GMT and the local time zone is PST, the returned object represents the time 2pm PST.
- Returns:
- a time object that represents the time of this object after shifting it from GMT time zone.
-
round
Returns a time object equal to the time represented by this object rounded to the given amount of milliseconds.For instance, if
roundByis 1000, the returned time is the time of this object rounded to the nearest second.- Parameters:
roundBy- unit of round in milliseconds- Returns:
- a time object equal to the time represented by this object rounded to the given amount of milliseconds.
-
truncate
Returns a time object equal to the time represented by this object truncated to a floor value that is a whole multiply of the given amount of milliseconds.For instance, if
truncateByis 1000, the returned time is the latest time in whole seconds that is not after this object.- Parameters:
truncateBy- unit of round in milliseconds- Returns:
- a time object equal to the time represented by this object rounded to the given amount of milliseconds.
-
hasCrossed
public boolean hasCrossed(long ms)Returns true if since the time of the object until now a certain point of time was crossed.The method accepts a parameter that is an amount of milliseconds and checks whether since the time of the object until now the point of time equal to last midnight plus the given milliseconds was crossed, i.e. if that point of time is between the time of the object (exclusive) and the current time (inclusive).
- Parameters:
ms- milliseconds since last midnight.- Returns:
- true if and only if the point of time equal to last midnight plus the given amount of milliseconds is between the time of the object (exclusive) and the current time (inclusive).
-
getMidnight
Returns a time object representing the latest midnight that precedes or equals this object.- Returns:
- a time object representing the latest midnight that precedes or equals this object.
-
fromXMLGregorianCalendar
Converts anXMLGregorianCalendarobject to aZDateobject.- Parameters:
xDate- the given XMLGregorianCalendar object, or null, in which case the method returns null.- Returns:
- the date as a ZDate object or null if the given XMLGregorianCalendar object is null.
-
toXMLGregorianCalendar
Returns the time of this object as anXMLGregorianCalendarobject, using the given factory for conversion.Converts the current object to an
XMLGregorianCalendarobject. Since the conversion requires the use of aDatatypeFactoryobject whose instantiation consumes heavy resources, the method has afactoryparameter that lets you reusing the same object in multiple calls to this method. If you call the method with the valuenullin this parameter, the method creates a new instance and uses it for the conversion.- Parameters:
factory- a factory object used for the conversion. If the value of this parameter is null the method instantiates a new factory object.- Returns:
- the time of this object as an XMLGregorianCalendar object.
- Throws:
Exception
-
fromSQLdate
Converts anSQL Dateobject to aZDateobject.- Parameters:
date- the given SQL Date object, or null, in which case the method returns null.- Returns:
- the date as a ZDate object or null if the given SQL Date object is null.
-
toSQLdate
Returns the time of this object as anSQL Dateobject.- Returns:
- the time of this object as an SQL Date object.
-
fromSQLtime
Converts anSQL Timestampobject to aZDateobject.- Parameters:
ts- the given SQL Timestamp object, or null, in which case the method returns null.- Returns:
- the date as a ZDate object or null if the given SQL Timestamp object is null.
-
toSQLtime
Returns the time of this object as anSQL Timestampobject.- Returns:
- the time of this object as an SQL Timestamp object.
-