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 String
DATE_FORMAT
Represents"d/M/yyyy HH:mm"
time format.static int
DAY
Number of milliseconds in a day (8,640,000).static String
DAY_MONTH_HOUR_MINUTE
Represents"dd/MM-HH:mm"
time format.static ZDate
DAY_OF_MESSIAH
The latest possible date (Long.MAX_VALUE).static String
FULL_DATE
Represents"dd/MM/yy-HH:mm:ss"
time format.static String
FULL_DATE_MILLISECONDS
Represents"dd/MM/yy-HH:mm:ss:SSS"
time format.static String
FULL_TIMESTAMP
Represents"ddMMyyHHmmssSSS"
time format.static int
HOUR
Number of milliseconds in an hour (360,000).static String
HOUR_MINUTE_SECOND
Represents"HH:mm:ss"
time format.static int
MINUTE
Number of milliseconds in a minute (60,000).static String
MINUTE_TIMESTAMP
Represents"ddMMyyHHmm"
time format.static String
MMDDYYHHMMSS
Represents"MMddyyHHmmss"
time format.static int
SECOND
Number of milliseconds in a second (1000).static String
SORTABLE
Represents"yyyy-MM-dd'T'HH:mm:ss"
time format.static String
SQL_DATE
Represents"yyyy-MM-dd HH:mm:ss"
time format.static String
TIMESTAMP
Represents"ddMMyyHHmmss"
time format.static String
YYMMDD
Represents"yyMMdd"
time format.static String
YYMMDDHHMM
Represents"yyMMddHHmm"
time format.static String
YYMMDDHHMMSS
Represents"yyMMddHHmmss"
time format.static String
YYYYMMDDHHMM
Represents"yyyyMMddHHmm"
time format.static String
YYYYMMDDHHMMSS
Represents"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 givenDate
object. -
Method Summary
Modifier and Type Method Description ZDate
add(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.ZDate
add(long ms)
Returns a time object with a value equal to the time in this object plus the given amount of milliseconds.boolean
after(Date when)
Returns true if the time of this object is after the time of the given object or if the given object isnull
.boolean
before(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.boolean
between(ZDate from, ZDate to)
Returns true if this object is between the two given time objects, inclusive.long
diff(Date date)
Returns the difference of time in milliseconds from the time represented by thedate
parameter till the time represented by this object.int
diffMonths(Date date)
Returns the difference in whole months from the time represented by thedate
parameter till the time represented by this object.ZDate
earliest(ZDate when)
Returns the earliest between this object and the given object.long
elapsed()
Returns the time that has elapsed since the time represented by this object in milliseconds.String
format(String fmt)
Formats this object into a time string.String
formatFullTimestamp()
Formats this object into a timestamp string using the format"ddMMyyHHmmssSSS"
.String
formatGMT(String fmt)
Formats this object into a time string representing the time of the object in GMT zone.String
formatRoundTimestamp(int precision)
Formats this object into a rounded timestamp string using the format"ddMMyyHHmmssS
(s)"
when the number ofS
s is the given precision.ZDate
fromGMT()
Returns a time object that represents the time of this object after shifting it from GMT time zone.static ZDate
fromSQLdate(Date date)
Converts anSQL Date
object to aZDate
object.static ZDate
fromSQLtime(Timestamp ts)
Converts anSQL Timestamp
object to aZDate
object.static ZDate
fromXMLGregorianCalendar(XMLGregorianCalendar xDate)
Converts anXMLGregorianCalendar
object to aZDate
object.int
getDay()
Returns the day of the week represented by this time object.int
getHours()
Returns the hour of the day that this time object represents as an integer number between 0 and 23.ZDate
getMidnight()
Returns a time object representing the latest midnight that precedes or equals this object.int
getMinutes()
Returns the minutes fraction of this time object as an integer number between 0 and 59.int
getSeconds()
Returns the seconds fraction of this time object as an integer number between 0 and 59.boolean
hasCrossed(long ms)
Returns true if since the time of the object until now a certain point of time was crossed.ZDate
latest(ZDate when)
Returns the latest between this object and the given object.static ZDate
now()
Returns a new time object set to the current time.static String
now(String fmt)
Returns the current time formatted into a string.static long
nowAsLong()
Returns the current time as milliseconds since 1/1/1970.static ZDate
parseFullTimestamp(String string)
Parses the beginning of the given string assuming it contains a timestamp value in the format"ddMMyyHHmmssSSS"
.static ZDate
parseGMTtime(String string, String format)
Parses the beginning of the given string assuming it contains a formatted GMT time string.static ZDate
parseRoundTime(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 ofS
s is the given precision.static ZDate
parseTime(String string, String format, ZDate ts)
Parses a formatted time string using the given date object to resolve full time data.ZDate
round(long roundBy)
Returns a time object equal to the time represented by this object rounded to the given amount of milliseconds.ZDate
setTime(int hour, int minutes)
Returns a time object with the date of this object and the given hour and minute.static ZDate
today()
Returns a new time object representing the midnight that started the current dayZDate
toGMT()
Returns a time object that represents the time of this object after shifting it to GMT time zone.Date
toSQLdate()
Returns the time of this object as anSQL Date
object.Timestamp
toSQLtime()
Returns the time of this object as anSQL Timestamp
object.XMLGregorianCalendar
toXMLGregorianCalendar(DatatypeFactory factory)
Returns the time of this object as anXMLGregorianCalendar
object, using the given factory for conversion.ZDate
truncate(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 givenDate
object.- 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
field
parameter to the time of this object and returns the result as a new time object. The possible values offield
are those defined by the Field numbers inCalendar
class. 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 byCalendar
class.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 thedate
parameter 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 thedate
parameter 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
from
parameter and not after the time ofto
parameter. Iffrom
isnull
then it is considered as the time of the Big Bang, i.e. ever. Ifto
isnull
it 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
null
or this time object is strictly beforewhen
then this object is returned, otherwisewhen
is 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
null
or this time object is strictly afterwhen
then this object is returned, otherwisewhen
is 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:
getMinutes
in 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:
getSeconds
in 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
hour
andminutes
. 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 ofS
s 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
string
parameter assuming it contains a time value formatted with theformat
parameter. The formatted string may contain partial information, for instance only the time of the day, in which case the method uses thets
parameter to resolve the full time value. Ifts
isnull
the 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 ofS
s 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
roundBy
is 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
truncateBy
is 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 anXMLGregorianCalendar
object to aZDate
object.- 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 anXMLGregorianCalendar
object, using the given factory for conversion.Converts the current object to an
XMLGregorianCalendar
object. Since the conversion requires the use of aDatatypeFactory
object whose instantiation consumes heavy resources, the method has afactory
parameter that lets you reusing the same object in multiple calls to this method. If you call the method with the valuenull
in 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 Date
object to aZDate
object.- 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 Date
object.- Returns:
- the time of this object as an SQL Date object.
-
fromSQLtime
Converts anSQL Timestamp
object to aZDate
object.- 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 Timestamp
object.- Returns:
- the time of this object as an SQL Timestamp object.
-