de.jaret.util.date
Class JaretDate

java.lang.Object
  extended by de.jaret.util.misc.PropertyObservableBase
      extended by de.jaret.util.date.JaretDate
All Implemented Interfaces:
PropertyObservable, Comparable<JaretDate>

public class JaretDate
extends PropertyObservableBase
implements Comparable<JaretDate>

A wrapper for java.util.Date and java.util.Calendar to ease the use of date and time. There are some convenience methods for quick textual output. They are located in a delegate that supports i8n. Use setLocale to set the locale of the delegate. The delegate is static and any change will affect the output of all JaretDates!

A good practice is to use an immutable type for representing dates. The JaretDate is not immutable! This is nice in a lot of ways but it requires some caution when copying instances. Recommended is to use it like an immuntable type most of the times (use the copy() method).

Version:
$Id: JaretDate.java 883 2009-10-07 21:03:00Z kliem $
Author:
Peter Kliem

Field Summary
static long MILLIS_PER_HOUR
          number of milliseconds in one hour.
static long MILLIS_PER_MINUTE
          number of milliseconds in one minute.
static long MILLIS_PER_SECOND
          number of milliseconds in one second.
 
Fields inherited from class de.jaret.util.misc.PropertyObservableBase
_propertyChangeSupport
 
Constructor Summary
JaretDate()
          Constructs a JaretDate with the current date and time.
JaretDate(Date date)
          Constructs a JaretDate using a clone of the given Date.
JaretDate(int day, int month, int year, int hours, int minutes, int seconds)
          Constructs a JaretDate setting it to a given date and time.
JaretDate(JaretDate jdate)
          Constructs a new JaretDate object with the same time as the given jaretDate.
JaretDate(long milliseconds)
          Constructs a JaretDate using the given millesecond value.
JaretDate(String millisecondsString)
          Constructs a JaretDate by milliseconds given in a String.
 
Method Summary
 JaretDate advanceDays(double days)
          Adds a number of days to the date.
 JaretDate advanceDays(int days)
          Advance by a certain number of days.
 JaretDate advanceHours(double hours)
           
 JaretDate advanceMillis(long millis)
           
 JaretDate advanceMinutes(double minutes)
           
 JaretDate advanceMonths(int months)
          Advance by a certain number of months.
 JaretDate advanceSeconds(double seconds)
           
 JaretDate advanceYears(int years)
          Advance by a certain number of years.
 JaretDate backDays(double days)
           
 JaretDate backHours(double hours)
           
 JaretDate backMinutes(double minutes)
           
 JaretDate backSeconds(double seconds)
           
 void clearTime()
          Clear time fields to reduce the jaretdate to a clean date.
 int compareDateTo(JaretDate date)
          Compare the date (day) of this and the given date.
 long compareTimeTo(JaretDate date)
          Compares only the time of day of the date with the given day.
 int compareTo(JaretDate date)
          Compare this to another JaretDate.
 JaretDate copy()
          Produce a new JaretDate with the time of this date.
 long diffMilliSeconds(JaretDate date)
          Substracts the the given date from this date and returns the difference in milliseconds.
 double diffMinutes(JaretDate date)
          Substracts the the given date from this date and returns the difference in minutes.
 int diffSeconds(JaretDate date)
          Substracts the the given date from this date and returns the difference in seconds (long).
 long diffSecondsL(JaretDate date)
          Substracts the the given date from this date and returns the difference in seconds.
 boolean equals(Object o)
           Two JaretDates are equal if the encapsulated Date is equal.
protected  Calendar getCalendar()
          Supply a configured (set to the current time) Calendar for internal use.
 Date getDate()
          Returns the encapsulated java.util.Date.
 int getDay()
           
 int getDayOfWeek()
          Get the day of the week as constant defined in java.util.Calendar.
 String getDayOfWeekString()
          Retrieve localized name of the current day of the week.
 int getDayOfYear()
           
static JaretDate getFirstDayOfAWeek(int week, int year)
          Retrieve a jaret date for the first day in a given week.
 int getHours()
           
 JaretDateFormatter getJaretDateFormatter()
          Retrieve the configured JaretDateFormatter.
 int getMillis()
           
 long getMillisInDay()
          Calculate the Milliseconds from 0:00 ignoring the millisecond field.
 int getMinutes()
           
 int getMonth()
          Retrive the month (1 based).
 String getMonthString()
          Retrieve a string for the month of the date (full length).
 int getSeconds()
           
 String getShortDayOfWeekString()
           
 String getShortMonthString()
          Retrieve short string for the month.
 int getWeekOfYear()
           
 int getYear()
          Retrieve the year of the date.
 int hashCode()
           Hashcode is the Hash of the encapsulted Date.
 boolean isWeekendDay()
           
 JaretDate setDate(int day, int month, int year)
          Set the date.
 JaretDate setDate(JaretDate date)
          Set the date.
 boolean setDateByText(String text)
          Sets the date by a parseble String.
 void setDateTime(int day, int month, int year, int hours, int minutes, int seconds)
          Sets the date and time of the JaretDate by values.
 void setDateTime(int day, int month, int year, int hours, int minutes, int seconds, int milliseconds)
          Sets the date and time of the JaretDate by values.
 void setDay(int day)
          Set the day of month.
 void setHours(int hours)
          Set the hour in day field of the date.
static void setJaretDateFormatter(JaretDateFormatter jaretDateFormatter)
          Sets the JaretDateFormatter to be used for parsing and formatting.
 void setLocale(Locale locale)
          Set the locale of the JaretDateFormatter.
 void setMilliseconds(int milliseconds)
          Set the milliseconds.
 void setMinutes(int minutes)
          Set the minutes in the date.
 void setMonth(int month)
          Set the month of the year.
 void setSeconds(int seconds)
          Set the seconds.
 JaretDate setTime(int hours, int minutes, int seconds)
          Set the time.
 JaretDate setTime(int hours, int minutes, int seconds, int milliseconds)
          Set the time.
 void setToNow()
          Sets the date to the current system time.
 String toDisplayString()
          Convenient way for a quick textual output of the timestamp.
 String toDisplayStringDate()
          Convenient way for quick textual output of the date (day) only.
 String toDisplayStringTime()
           
 String toDisplayStringTime(boolean seconds)
          Convenient way for quick textual represenation (time).
 String toString()
          The standard toString behaviour is the value of the date in milliseconds.
 
Methods inherited from class de.jaret.util.misc.PropertyObservableBase
addPropertyChangeListener, firePropertyChange, isRealModification, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MILLIS_PER_HOUR

public static final long MILLIS_PER_HOUR
number of milliseconds in one hour.

See Also:
Constant Field Values

MILLIS_PER_MINUTE

public static final long MILLIS_PER_MINUTE
number of milliseconds in one minute.

See Also:
Constant Field Values

MILLIS_PER_SECOND

public static final long MILLIS_PER_SECOND
number of milliseconds in one second.

See Also:
Constant Field Values
Constructor Detail

JaretDate

public JaretDate()
Constructs a JaretDate with the current date and time.


JaretDate

public JaretDate(Date date)
Constructs a JaretDate using a clone of the given Date.

Parameters:
date - initial date to set

JaretDate

public JaretDate(String millisecondsString)
Constructs a JaretDate by milliseconds given in a String.

Parameters:
millisecondsString - as String

JaretDate

public JaretDate(long milliseconds)
Constructs a JaretDate using the given millesecond value.

Parameters:
milliseconds - as long

JaretDate

public JaretDate(JaretDate jdate)
Constructs a new JaretDate object with the same time as the given jaretDate.

Parameters:
jdate - JaretDate to copy

JaretDate

public JaretDate(int day,
                 int month,
                 int year,
                 int hours,
                 int minutes,
                 int seconds)
Constructs a JaretDate setting it to a given date and time.

Parameters:
day - day of month
month - month of year
year - year
hours - hours in day
minutes - minutes in hour
seconds - seconds in minute
Method Detail

getJaretDateFormatter

public JaretDateFormatter getJaretDateFormatter()
Retrieve the configured JaretDateFormatter.

Returns:
JaretDateFormatter used for formatting and parsing

setJaretDateFormatter

public static void setJaretDateFormatter(JaretDateFormatter jaretDateFormatter)
Sets the JaretDateFormatter to be used for parsing and formatting. The JaretDateFormatter is used for all JaretDates! A Change will affect parsing and formatting of all JaretDates!

Parameters:
jaretDateFormatter - the new Formatter to be used.

setLocale

public void setLocale(Locale locale)
Set the locale of the JaretDateFormatter. Calling will fire a property change for the date.

Parameters:
locale - Locale to be used

setDateTime

public void setDateTime(int day,
                        int month,
                        int year,
                        int hours,
                        int minutes,
                        int seconds,
                        int milliseconds)
Sets the date and time of the JaretDate by values.

Parameters:
day - day of month
month - month of year
year - year
hours - hours in day
minutes - minutes in hour
seconds - seconds in minute
milliseconds - milliseconds in second

setDateTime

public void setDateTime(int day,
                        int month,
                        int year,
                        int hours,
                        int minutes,
                        int seconds)
Sets the date and time of the JaretDate by values.

Parameters:
day - day of month
month - month of year
year - year
hours - hours in day
minutes - minutes in hour
seconds - seconds in minute

setTime

public JaretDate setTime(int hours,
                         int minutes,
                         int seconds,
                         int milliseconds)
Set the time. Does not affect the date.

Parameters:
hours - hour value
minutes - minute value
seconds - seconds value
milliseconds - millisecond value
Returns:
the modified date

setTime

public JaretDate setTime(int hours,
                         int minutes,
                         int seconds)
Set the time. Does not affect the date. Will set the milliseconds to zero.

Parameters:
hours - hour value
minutes - minute value
seconds - seconds value
Returns:
the modified date

setDate

public JaretDate setDate(int day,
                         int month,
                         int year)
Set the date. Does not affect the time.

Parameters:
day - day of month
month - month
year - year
Returns:
this

setDate

public JaretDate setDate(JaretDate date)
Set the date. Does not affect the time.

Parameters:
date - JaretDate to copy the date from
Returns:
this

setMonth

public void setMonth(int month)
Set the month of the year.

Parameters:
month - month of the year (1=january

setDay

public void setDay(int day)
Set the day of month.

Parameters:
day - day of month

setHours

public void setHours(int hours)
Set the hour in day field of the date. Does not affect any other field.

Parameters:
hours - hour value to set

setMinutes

public void setMinutes(int minutes)
Set the minutes in the date. Does not affect any other field.

Parameters:
minutes - minute value

setSeconds

public void setSeconds(int seconds)
Set the seconds. Will set the milliseconds to zero.

Parameters:
seconds - second value

setMilliseconds

public void setMilliseconds(int milliseconds)
Set the milliseconds.

Parameters:
milliseconds - millisecond value

setDateByText

public boolean setDateByText(String text)
Sets the date by a parseble String. If the String fails to be parsed the result will be false and the date will not be changed. The parsing will be delegated to the JaretDateFormatter.

Parameters:
text - string to be parsed as date
Returns:
true if parsing is successful false otherwise

getCalendar

protected Calendar getCalendar()
Supply a configured (set to the current time) Calendar for internal use.

Returns:
Calendar set to the current date

getMillisInDay

public long getMillisInDay()
Calculate the Milliseconds from 0:00 ignoring the millisecond field.

Returns:
Milliseconds from 0:00 ignoring millisecond field

getDate

public Date getDate()
Returns the encapsulated java.util.Date.

Returns:
the encapsulated java.util.Date

toDisplayString

public String toDisplayString()
Convenient way for a quick textual output of the timestamp.

Returns:
a textual representation of the date (timestamp)

toDisplayStringDate

public String toDisplayStringDate()
Convenient way for quick textual output of the date (day) only.

Returns:
a textual representation of the date (day)

toDisplayStringTime

public String toDisplayStringTime(boolean seconds)
Convenient way for quick textual represenation (time).

Parameters:
seconds - if true, seconds will be included in the format hh:mm:ss
Returns:
a textual represenation of the time

toDisplayStringTime

public String toDisplayStringTime()
Returns:
toDisplayStringTime(false)

toString

public String toString()
The standard toString behaviour is the value of the date in milliseconds.

Overrides:
toString in class Object
Returns:
milliseconds as String

setToNow

public void setToNow()
Sets the date to the current system time.


diffMinutes

public double diffMinutes(JaretDate date)
Substracts the the given date from this date and returns the difference in minutes.

Parameters:
date - the Date to be substracted
Returns:
difference in minutes

diffSeconds

public int diffSeconds(JaretDate date)
Substracts the the given date from this date and returns the difference in seconds (long).

Parameters:
date - the Date to be substracted
Returns:
difference in seconds (long)

diffSecondsL

public long diffSecondsL(JaretDate date)
Substracts the the given date from this date and returns the difference in seconds.

Parameters:
date - the Date to be substracted
Returns:
difference in seconds

diffMilliSeconds

public long diffMilliSeconds(JaretDate date)
Substracts the the given date from this date and returns the difference in milliseconds.

Parameters:
date - the Date to be substracted
Returns:
difference in milliseconds

advanceMonths

public JaretDate advanceMonths(int months)
Advance by a certain number of months.

Parameters:
months - number of months
Returns:
modified date

advanceYears

public JaretDate advanceYears(int years)
Advance by a certain number of years.

Parameters:
years - number of years to advance
Returns:
modified date

advanceDays

public JaretDate advanceDays(int days)
Advance by a certain number of days.

Parameters:
days - number of days to advance
Returns:
modified date

advanceDays

public JaretDate advanceDays(double days)
Adds a number of days to the date.

Parameters:
days - number of days to add
Returns:
the date for cascading calls

backDays

public JaretDate backDays(double days)

advanceHours

public JaretDate advanceHours(double hours)

backHours

public JaretDate backHours(double hours)

advanceMinutes

public JaretDate advanceMinutes(double minutes)

backMinutes

public JaretDate backMinutes(double minutes)

advanceSeconds

public JaretDate advanceSeconds(double seconds)

advanceMillis

public JaretDate advanceMillis(long millis)

backSeconds

public JaretDate backSeconds(double seconds)

equals

public boolean equals(Object o)
Two JaretDates are equal if the encapsulated Date is equal.

Overrides:
equals in class Object

hashCode

public int hashCode()
Hashcode is the Hash of the encapsulted Date.

Overrides:
hashCode in class Object

compareTo

public int compareTo(JaretDate date)
Compare this to another JaretDate.

Specified by:
compareTo in interface Comparable<JaretDate>
Parameters:
date - JaretDate to compare to
Returns:
0 for an exact match, >0 for this>date, <0 for this<date

compareTimeTo

public long compareTimeTo(JaretDate date)
Compares only the time of day of the date with the given day.

Parameters:
date - the date to compare to
Returns:
0 for an exact match, >0 for this>date, <0 for this<date

compareDateTo

public int compareDateTo(JaretDate date)
Compare the date (day) of this and the given date.

Parameters:
date - date to compare to
Returns:
0 for exact match, >0 for this>date, <0 for this<date

copy

public JaretDate copy()
Produce a new JaretDate with the time of this date.

Returns:
a new instance of JaretDate

getMonth

public int getMonth()
Retrive the month (1 based).

Returns:
month (1 for january)

getMonthString

public String getMonthString()
Retrieve a string for the month of the date (full length).

Returns:
string for teh month

getShortMonthString

public String getShortMonthString()
Retrieve short string for the month.

Returns:
shor (abbreviated) month name

getWeekOfYear

public int getWeekOfYear()

getDayOfWeekString

public String getDayOfWeekString()
Retrieve localized name of the current day of the week.

Returns:
long name of the day of week

getShortDayOfWeekString

public String getShortDayOfWeekString()

getDayOfWeek

public int getDayOfWeek()
Get the day of the week as constant defined in java.util.Calendar.

Returns:
day of the week.

isWeekendDay

public boolean isWeekendDay()

getDay

public int getDay()

getHours

public int getHours()

getMinutes

public int getMinutes()

getSeconds

public int getSeconds()

getMillis

public int getMillis()

getDayOfYear

public int getDayOfYear()

getYear

public int getYear()
Retrieve the year of the date.

Returns:
year of the date

clearTime

public void clearTime()
Clear time fields to reduce the jaretdate to a clean date.


getFirstDayOfAWeek

public static JaretDate getFirstDayOfAWeek(int week,
                                           int year)
Retrieve a jaret date for the first day in a given week.

Parameters:
week - week
year - year
Returns:
first day of that week


Copyright © 2013. All Rights Reserved.