ahc.util
Class DateDay

java.lang.Object
  |
  +--ahc.util.DateDay
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class DateDay
extends java.lang.Object
implements java.lang.Comparable, java.io.Serializable

This class represents a date, i.e. a combination of day, month and year.

It fills a gap in the JDK libraries: java.util.Date is a point in time including hours, minutes, secondes etc., and it does a reasonably good job of it. A point in time is prone to time zone conversions etc. so that its representation depends on a time zone / locale setting.

Dates are different. If some one's birthday is Sept 5 1974, then that is it, and we usually do not want the birthday to be displayed differently for different time zones. In a business setting it is also quite commonly necessary to do date arithmetic: one week later, one month previously etc. The common approach of treating a day as 24 * 60 * 60 * 1000 milliseconds and adding / subtracting these is not only cumbersome but also a violation of the Date contract (there is such a thing as leap seconds...) Doing this sort of thing cleanly requires the use of GregorianCalendar which is complex, cumbersome and expensive to use.

To cut a long story short, this class fills the gap by providing a small, inexpensive class for date representation and arithmetic. The class is immutable, and all modifying methods return new instances and leave the original instance unmodified.

Author:
Arno Haase
See Also:
Serialized Form

Method Summary
 boolean after(DateDay other)
           
 java.util.Date asDate()
          converts to a java.util.Date based on the default TimeZone
 java.util.Date asDate(java.util.TimeZone timeZone)
          converts to a java.util.Date based on a specific TimeZone
 boolean before(DateDay other)
           
 int compareTo(java.lang.Object o)
          implements java.util.Comparable.
static DateDay create(java.util.Date date)
          returns a DateDay instance based on a java.util.Date and the default TimeZone
static DateDay create(java.util.Date date, java.util.TimeZone timezone)
          returns a DateDay instance based on the full java.util representation, i.e. a Date and a TimeZone
static DateDay create(int day, int month, int year)
          returns a DateDay instance based on day, month and year
 DateDay decDay()
          returns the previous date
 DateDay decDay(int num)
          returns the equivalent of num calls to decDay
 DateDay decMonth(boolean roundLater)
          The opposite of incMonth(boolean)
 DateDay decMonth(int num, boolean roundLater)
          The opposite of incMonth (int, boolean)
 DateDay decYear(boolean roundLater)
          The opposite of incYear(boolean)
 DateDay decYear(int num, boolean roundLater)
          The opposite of incYear(int, boolean)
 boolean equals(java.lang.Object o)
           
 int getDay()
           
 int getMonth()
           
 int getYear()
           
 int hashCode()
           
 DateDay incDay()
          returns the next date
 DateDay incDay(int num)
          returns the equivalent of num calls to indDay()
 DateDay incMonth(boolean roundLater)
          returns the same day of the next month.
 DateDay incMonth(int num, boolean roundLater)
          returns the same day num months later.
 DateDay incYear(boolean roundLater)
          returns the same date of the next year.
 DateDay incYear(int num, boolean roundLater)
          returns the same date num years later.
static boolean isLeapYear(int year)
           
static int numDays(int month, int year)
           
 DateDay setDay(int day)
           
 DateDay setMonth(int month)
           
 DateDay setYear(int year)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

create

public static DateDay create(int day,
                             int month,
                             int year)
returns a DateDay instance based on day, month and year


create

public static DateDay create(java.util.Date date,
                             java.util.TimeZone timezone)
returns a DateDay instance based on the full java.util representation, i.e. a Date and a TimeZone


create

public static DateDay create(java.util.Date date)
returns a DateDay instance based on a java.util.Date and the default TimeZone


getDay

public int getDay()

getMonth

public int getMonth()

getYear

public int getYear()

setDay

public DateDay setDay(int day)

setMonth

public DateDay setMonth(int month)

setYear

public DateDay setYear(int year)

asDate

public java.util.Date asDate()
converts to a java.util.Date based on the default TimeZone


asDate

public java.util.Date asDate(java.util.TimeZone timeZone)
converts to a java.util.Date based on a specific TimeZone


compareTo

public int compareTo(java.lang.Object o)
implements java.util.Comparable. This method expects the other object to be an instance of DateDay.

Specified by:
compareTo in interface java.lang.Comparable

before

public boolean before(DateDay other)

after

public boolean after(DateDay other)

isLeapYear

public static boolean isLeapYear(int year)

numDays

public static int numDays(int month,
                          int year)

incDay

public DateDay incDay()
returns the next date


incDay

public DateDay incDay(int num)
returns the equivalent of num calls to indDay()


decDay

public DateDay decDay()
returns the previous date


decDay

public DateDay decDay(int num)
returns the equivalent of num calls to decDay


incMonth

public DateDay incMonth(boolean roundLater)
returns the same day of the next month. If the current day of the month is bigger than the maximum for that month, the parameter flag roundLater determines whether the first of the month after (true) or the last day of that month is used (false).


incMonth

public DateDay incMonth(int num,
                        boolean roundLater)
returns the same day num months later. If the current day of the month is bigger than the maximum for that month, the parameter flag roundLater determines whether the first of the month after (true) or the last day of that month is used (false).

This is not equivalent to num calls to incMonth(boolean)!


decMonth

public DateDay decMonth(boolean roundLater)
The opposite of incMonth(boolean)


decMonth

public DateDay decMonth(int num,
                        boolean roundLater)
The opposite of incMonth (int, boolean)


incYear

public DateDay incYear(boolean roundLater)
returns the same date of the next year. In the rare case that that year does not have that date (i.e. 29th Feb), the parameter flag roundLater determines wheter the previous date (false) or the next date (true) is returned.


incYear

public DateDay incYear(int num,
                       boolean roundLater)
returns the same date num years later. In the rare case that that year does not have that date (i.e. 29th Feb), the parameter flag roundLater determines wheter the previous date (false) or the next date (true) is returned.

This is not equivalent to several subsequent calls of incYear (boolean)!


decYear

public DateDay decYear(boolean roundLater)
The opposite of incYear(boolean)


decYear

public DateDay decYear(int num,
                       boolean roundLater)
The opposite of incYear(int, boolean)


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object