org.jcoderz.commons.types
Class Date

java.lang.Object
  extended by org.jcoderz.commons.types.Date
All Implemented Interfaces:
Serializable, Comparable

public final class Date
extends Object
implements Comparable, Serializable

Immutable holder of a Date running through our system. This class also holds some time related constants to be used in the code. Years before 1 are not fully supported and might result in wrong string representations.

Author:
Andreas Mandel
See Also:
Serialized Form

Field Summary
static String DATE_FORMAT
          The format used to parse and write schema date types.
static String DATE_TIME_FORMAT
          The format used to write schema dateTime types.
static ThreadLocal DATE_TIME_FORMAT_FORMATER
          Date Formater to use for DATE_TIME_FORMAT format.
static String DATE_TIME_FORMAT_WITH_MILLIS
          The format used to write schema dateTime types, if milli seconds are not equal 0.
static ThreadLocal DATE_TIME_FORMAT_WITH_MILLIS_FORMATER
          Date Formater to use for DATE_TIME_FORMAT_WITH_MILLIS format.
static Date FUTURE_DATE
          This date represents the largest possible date (9999 * 365 * 24 * 60 * 60 * 1000).
static int MILLIS_PER_DAY
          Number of milliseconds per day.
static int MILLIS_PER_HOUR
          Number of milli seconds in an hour.
static int MILLIS_PER_MINUTE
          Number of milli seconds in a minute.
static int MILLIS_PER_SECOND
          Number of milli seconds in a second.
static int MILLIS_PER_WEEK
          Number of milliseconds per week.
static int MONTH_PER_YEAR
          Number of months per year.
static long NANOS_PER_MILLI
          Number of nano seconds in a milli second.
static long NANOS_PER_SECOND
          Number of nano seconds in a second.
static Date OLD_DATE
          This date represents the 1.1.1970 00:00:00.000.
static int SECONDS_PER_MINUTE
          Number of seconds in a minute.
static TimeZone TIME_ZONE
          Timezone used by the protocol.
static String TYPE_NAME
          The name of this type.
 
Constructor Summary
Date(long time)
          Creates a new instance of Date taking the given long as ms from 1.1.1970.
 
Method Summary
 boolean after(Date other)
          Checks if this date is after the given date.
 boolean afterOrEqual(Date other)
          Checks if this date is after or equal to the given date.
 boolean before(Date other)
          Checks if the this date is before the given date.
 boolean beforeOrEqual(Date other)
          Checks if the this date is before or equal to the given date.
 int compareTo(Object o)
          Compares this Date to another Object.
static Date earliest(Date a, Date b)
          Returns the earliest of the two dates.
 long elapsedMillis()
          Returns the elapsed number of milliseconds from this date to now.
 long elapsedMillis(Date other)
          Returns the elapsed number of milliseconds from this date to other.
 boolean equals(Object obj)
          Compares two dates for equality.
static Date fromLong(long time)
          Creates a new instance of Date taking the given long as ms from 1.1.1970.
static Date fromSqlDate(Date date)
          Creates a new instance of Date holding the value as found in the given java.sql.Date.
static Date fromSqlTimestamp(Timestamp timestamp)
          Creates a new instance of Date holding the value as found in the given java.sql.Timestamp.
static Date fromString(String date)
          Parses the given String as returned by the toString method.
static Date fromString(String date, String pattern)
          Parses the given String with the given format pattern.
static Date fromUtilDate(Date date)
          Creates a new instance of Date from a java.util.Date Object.
static int getDaysSinceEpoch()
          Returns the number of days since the Unix Epoch (1970/01/01).
static int getDaysSinceEpoch(Date d)
          Returns the number of days between Unix Epoch (1970/01/01) and d.
 long getTime()
          Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
 int hashCode()
          Returns a hash code value for this object.
static Date latest(Date a, Date b)
          Returns the latest of the two dates.
 Date minus(long offset)
          Returns a Date object that holds the time of this object minus the given milliseconds in the past.
static Date now()
          Returns a Date object that holds the current time.
static Date nowPlus(long offset)
          Returns a Date object that holds the current time plus the given milliseconds in the future.
 Date plus(long offset)
          Returns a Date object that holds the time of this object plus the given milliseconds in the future.
 String toDateString()
          Returns the String representation format is according schema date representation.
 Date toSqlDate()
          Returns the date represented by this object as newly generated java.sql.Date object.
 Timestamp toSqlTimestamp()
          Returns the date represented by this object as newly generated java.sql.Timestamp object.
 String toString()
          Returns the String representation format is according schema dateTime representation.
 String toString(String pattern)
          Returns the date as formatted string using the given pattern.
 Date toUtilDate()
          Returns the date represented by this object as newly generated java.util.Date object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DATE_TIME_FORMAT_WITH_MILLIS_FORMATER

public static final ThreadLocal DATE_TIME_FORMAT_WITH_MILLIS_FORMATER
Date Formater to use for DATE_TIME_FORMAT_WITH_MILLIS format.


DATE_TIME_FORMAT_FORMATER

public static final ThreadLocal DATE_TIME_FORMAT_FORMATER
Date Formater to use for DATE_TIME_FORMAT format.


TYPE_NAME

public static final String TYPE_NAME
The name of this type.

See Also:
Constant Field Values

TIME_ZONE

public static final TimeZone TIME_ZONE
Timezone used by the protocol.


NANOS_PER_MILLI

public static final long NANOS_PER_MILLI
Number of nano seconds in a milli second.

See Also:
Constant Field Values

MILLIS_PER_SECOND

public static final int MILLIS_PER_SECOND
Number of milli seconds in a second.

See Also:
Constant Field Values

NANOS_PER_SECOND

public static final long NANOS_PER_SECOND
Number of nano seconds in a second.

See Also:
Constant Field Values

SECONDS_PER_MINUTE

public static final int SECONDS_PER_MINUTE
Number of seconds in a minute.

See Also:
Constant Field Values

MILLIS_PER_MINUTE

public static final int MILLIS_PER_MINUTE
Number of milli seconds in a minute.

See Also:
Constant Field Values

MILLIS_PER_HOUR

public static final int MILLIS_PER_HOUR
Number of milli seconds in an hour.

See Also:
Constant Field Values

MILLIS_PER_DAY

public static final int MILLIS_PER_DAY
Number of milliseconds per day.

See Also:
Constant Field Values

MILLIS_PER_WEEK

public static final int MILLIS_PER_WEEK
Number of milliseconds per week.

See Also:
Constant Field Values

MONTH_PER_YEAR

public static final int MONTH_PER_YEAR
Number of months per year.

See Also:
Constant Field Values

OLD_DATE

public static final Date OLD_DATE
This date represents the 1.1.1970 00:00:00.000.


FUTURE_DATE

public static final Date FUTURE_DATE
This date represents the largest possible date (9999 * 365 * 24 * 60 * 60 * 1000).


DATE_TIME_FORMAT

public static final String DATE_TIME_FORMAT
The format used to write schema dateTime types.

See Also:
Constant Field Values

DATE_TIME_FORMAT_WITH_MILLIS

public static final String DATE_TIME_FORMAT_WITH_MILLIS
The format used to write schema dateTime types, if milli seconds are not equal 0.

See Also:
Constant Field Values

DATE_FORMAT

public static final String DATE_FORMAT
The format used to parse and write schema date types.

See Also:
Constant Field Values
Constructor Detail

Date

public Date(long time)
Creates a new instance of Date taking the given long as ms from 1.1.1970.

Parameters:
time - the number of milliseconds.
Method Detail

fromLong

public static Date fromLong(long time)
Creates a new instance of Date taking the given long as ms from 1.1.1970.

Parameters:
time - the number of milliseconds.
Returns:
a newly generated Date object taking the given long as ms from .1.1970.
See Also:
Date.Date()

fromUtilDate

public static Date fromUtilDate(Date date)
Creates a new instance of Date from a java.util.Date Object.

Parameters:
date - the date as java.util.Date.
Returns:
a newly generated Date object representing the same time as the given date.

fromSqlDate

public static Date fromSqlDate(Date date)
Creates a new instance of Date holding the value as found in the given java.sql.Date.

Parameters:
date - the date as java.sql.Date.
Returns:
a newly generated Date object representing the same time as the given date.

fromSqlTimestamp

public static Date fromSqlTimestamp(Timestamp timestamp)
Creates a new instance of Date holding the value as found in the given java.sql.Timestamp.

Parameters:
timestamp - the date as java.sql.Timestamp.
Returns:
a newly generated Date object representing the same time as the given date.

fromString

public static Date fromString(String date,
                              String pattern)
                       throws ParseException
Parses the given String with the given format pattern. NULL is returned if an empty string "" is given as parameter. (This is to allow empty elements as used in some places.)

Parameters:
date - the date in given notation
pattern - the pattern to be used for parsing.
Returns:
a newly generated Date object representing the time given in the date or NULL, if the given parameter was an empty string "".
Throws:
ParseException - if the given date is not in the format of the given pattern.
See Also:
SimpleDateFormat

fromString

public static Date fromString(String date)
                       throws ParseException
Parses the given String as returned by the toString method.

Parameters:
date - the date in String representation
Returns:
a newly generated Date object representing the time given in the date, or null if the input string was null.
Throws:
ParseException - if the given date is not in the format of the Date.toString() format.

now

public static Date now()
Returns a Date object that holds the current time.

Returns:
a newly generated Date object representing current time.

nowPlus

public static Date nowPlus(long offset)
Returns a Date object that holds the current time plus the given milliseconds in the future.

Parameters:
offset - the number of millis to step into the future.
Returns:
a newly generated Date object.

getDaysSinceEpoch

public static int getDaysSinceEpoch()
Returns the number of days since the Unix Epoch (1970/01/01).

Returns:
the number of days since the Unix Epoch (1970/01/01).

getDaysSinceEpoch

public static int getDaysSinceEpoch(Date d)
Returns the number of days between Unix Epoch (1970/01/01) and d.

Parameters:
d - the date until when the number of days should be computed
Returns:
the number of days between Unix Epoch and d.

earliest

public static Date earliest(Date a,
                            Date b)
Returns the earliest of the two dates.

Parameters:
a - date a to compare.
b - date b to compare.
Returns:
the earliest of the two dates.

latest

public static Date latest(Date a,
                          Date b)
Returns the latest of the two dates.

Parameters:
a - date a to compare.
b - date b to compare.
Returns:
the earliest of the two dates.

plus

public Date plus(long offset)
Returns a Date object that holds the time of this object plus the given milliseconds in the future.

Parameters:
offset - the number of millis to step into the future.
Returns:
a newly generated Date object.

minus

public Date minus(long offset)
Returns a Date object that holds the time of this object minus the given milliseconds in the past.

Parameters:
offset - the number of millis to step back from this date.
Returns:
a newly generated Date object.

toUtilDate

public Date toUtilDate()
Returns the date represented by this object as newly generated java.util.Date object.

Returns:
the date represented by this object as newly generated java.util.Date object.

toSqlDate

public Date toSqlDate()
Returns the date represented by this object as newly generated java.sql.Date object.

Returns:
the date represented by this object as newly generated java.sql.Date object.

toSqlTimestamp

public Timestamp toSqlTimestamp()
Returns the date represented by this object as newly generated java.sql.Timestamp object.

Returns:
the date represented by this object as newly generated java.sql.Timestamp object.

hashCode

public int hashCode()
Returns a hash code value for this object. The result is the exclusive OR of the two halves of the primitive long value returned by the getTime() method. That is, the hash code is the value of the expression:
 (int)(this.getTime()^(this.getTime() >>> 32))

Overrides:
hashCode in class Object
Returns:
a hash code value for this object.

equals

public boolean equals(Object obj)
Compares two dates for equality.

Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.

toString

public String toString(String pattern)
Returns the date as formatted string using the given pattern.

Parameters:
pattern - the pattern describing the date and time format
Returns:
the date as formatted string using the given pattern.
See Also:
SimpleDateFormat

toString

public String toString()
Returns the String representation format is according schema dateTime representation.

Overrides:
toString in class Object
Returns:
the String representation format is according schema dateTime representation.

toDateString

public String toDateString()
Returns the String representation format is according schema date representation.

Returns:
the String representation format is according schema date representation.

getTime

public long getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

compareTo

public int compareTo(Object o)
              throws NullPointerException,
                     ClassCastException
Compares this Date to another Object.

Specified by:
compareTo in interface Comparable
Parameters:
o - the Object to be compared.
Returns:
the value 0 if the argument is a Date equal to this Date; a value less than 0 if the argument is a Date after this Date; and a value greater than 0 if the argument is a Date before this Date.
Throws:
ClassCastException - if the argument is not a org.jcoderz.ipp.Date.
NullPointerException - if the argument is null.
See Also:
Comparable

before

public boolean before(Date other)
Checks if the this date is before the given date.

Parameters:
other - the date to compare with.
Returns:
true, if the this date is before the given date.

beforeOrEqual

public boolean beforeOrEqual(Date other)
Checks if the this date is before or equal to the given date.

Parameters:
other - the date to compare with.
Returns:
true, if the this date is before or equal to the given date.

after

public boolean after(Date other)
Checks if this date is after the given date.

Parameters:
other - the date to compare with.
Returns:
true, if this date is after the given date.

afterOrEqual

public boolean afterOrEqual(Date other)
Checks if this date is after or equal to the given date.

Parameters:
other - the date to compare with.
Returns:
true, if this date is after or equal to the given date.

elapsedMillis

public long elapsedMillis()
Returns the elapsed number of milliseconds from this date to now.

Returns:
the elapsed number of milliseconds from this date to now.

elapsedMillis

public long elapsedMillis(Date other)
Returns the elapsed number of milliseconds from this date to other. The result is negative if other is before this date.

Parameters:
other - the date to compare with this date.
Returns:
the elapsed number of milliseconds from this date to other.


Copyright 2007 The jCoderZ Project.