texsoft.java.util
Class XsDate

java.lang.Object
  |
  +--texsoft.java.util.XsDate
All Implemented Interfaces:
java.lang.Comparable

public class XsDate
extends java.lang.Object
implements java.lang.Comparable

The XsDate class represents a simple date in time.

It simply stores year, month and day, without many of the features of Date and Calendar.

An usefull feature of this class is that it implements the Comparable interface to allow searching and ordering.


Constructor Summary
XsDate()
          Creates new empty XsDate object.
XsDate(java.util.GregorianCalendar cal)
          Create a new XsDate object initialized with the given GregorianCalendar value.
XsDate(int y, int m, int d)
          Create a new XsDate object initialized with the given values.
XsDate(long t)
          Create a new XsDate object from timestamp in milliseconds since 1970-01-01 0:00:00.
XsDate(java.lang.String s)
          Create a new XsDate object initialized from the given String value.
 
Method Summary
 void addDays(int amount)
          Add the given number of days to the date value.
 int compareTo(java.lang.Object o)
          Compares this XsDate object with the specified object for order.
 int differenceDays(XsDate other)
          Calculate the difference in days between this date and other.
static int differenceDays(XsDate from, XsDate to)
          Calculate the difference in days between from and to.
static XsDate getCurrent()
          Returns a new XsDate object set to the system date.
 int getDay()
          Get the day of the date.
 int getDayOfWeek()
          Returns the day of week, starting from Monday = 1.
 int getDayOfYear()
          Returns the day of year, starting from 1.
 int getMonth()
          Get the month of the date.
 int getWeekOfYear()
          Returns the week of year, starting from 1.
 int getYear()
          Get the year of the date.
 boolean isEmpty()
          Returns true if the object stores an empty date.
 boolean isLeapYear()
          Returns true if the date's year is a leap year.
static void main(java.lang.String[] args)
          Some simple code to test the class.
 void setDate(java.util.GregorianCalendar cal)
          Set the date to the give GregorianCalendar value.
 void setDate(int y, int m, int d)
          Set the date with the given year, month and day values.
 void setDate(long t)
          Set the date to timestamp in milliseconds sice 1970-01-01 0:00:00.
 void setDate(java.lang.String s)
          Set the date from the given String value.
 void setDay(int d)
          Set the day of the date.
 void setMonth(int m)
          Set the month of the date.
 void setYear(int y)
          Set the year of the date.
 java.util.GregorianCalendar toGregorianCalendar()
          Returns a GregorianCalendar object set to the date of this object.
 long toMilliseconds()
          Returns date as timestamp in milliseconds since 1970-01-01 0:00:00.
 java.lang.String toString()
          Returns a String rapresentation of the date in the format YYYY-MM-DD.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XsDate

public XsDate()
Creates new empty XsDate object.

An empty date has year, month and day set to 0.


XsDate

public XsDate(int y,
              int m,
              int d)
Create a new XsDate object initialized with the given values.

If the resulting date is not valid, set the date to empty 0000-00-00.

Parameters:
y - year.
m - month.
d - day.

XsDate

public XsDate(long t)
Create a new XsDate object from timestamp in milliseconds since 1970-01-01 0:00:00.

Parameters:
t - timestamp value.

XsDate

public XsDate(java.util.GregorianCalendar cal)
Create a new XsDate object initialized with the given GregorianCalendar value.

Parameters:
cal - date in GregoriaCalendar format.

XsDate

public XsDate(java.lang.String s)
Create a new XsDate object initialized from the given String value.

For the available format of the String argument see setDate(String).

If the resulting date is not valid, set the date to empty 0000-00-00.

Parameters:
s - date value as String in one of the recognized formats.
Method Detail

setDate

public void setDate(int y,
                    int m,
                    int d)
Set the date with the given year, month and day values.

This method is called by all other methods that set or change any of the date fields. If the resulting date is not valid, set the date to empty 0000-00-00.

Parameters:
y - year.
m - month.
d - day.

setDate

public void setDate(long t)
Set the date to timestamp in milliseconds sice 1970-01-01 0:00:00.

Parameters:
t - timestamp.

setDate

public void setDate(java.util.GregorianCalendar cal)
Set the date to the give GregorianCalendar value.

Parameters:
cal - date as GregorianCalendar object.

setDate

public void setDate(java.lang.String s)
Set the date from the given String value.

The String argument can have a fix format or a format that uses a separator.

The fixed format has the form: YYYYMMDD and must be exacly 8 decimal digits.

The separator format has the form: Y-M-D. The separator between year, month and day components is the first non-numerical character found scanning the string.

If the resulting date is not valid, set the date to empty 0000-00-00.

Parameters:
s - date value as String in one of the recognized formats.

isEmpty

public boolean isEmpty()
Returns true if the object stores an empty date.

An empty date has year, month and day set to zero.

Returns:
true if the object stores an empty date.

isLeapYear

public boolean isLeapYear()
Returns true if the date's year is a leap year.

Returns:
true is the date's year is a leap year.

toString

public java.lang.String toString()
Returns a String rapresentation of the date in the format YYYY-MM-DD.

Overrides:
toString in class java.lang.Object
Returns:
the rapresentation of the date in the format YYYY-MM-DD.

toGregorianCalendar

public java.util.GregorianCalendar toGregorianCalendar()
Returns a GregorianCalendar object set to the date of this object.

Returns:
a GregorianCalendar object set to the date of this object.

toMilliseconds

public long toMilliseconds()
Returns date as timestamp in milliseconds since 1970-01-01 0:00:00.

Returns:
timestamp value in milliseconds since 1970-01-01 0:00:00.

getCurrent

public static XsDate getCurrent()
Returns a new XsDate object set to the system date.

Returns:
Returns a new XsDate object set with the current date of the system clock.

getYear

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

Returns:
the year of the date.

setYear

public void setYear(int y)
Set the year of the date.

Parameters:
y - new year value for the date.

getMonth

public int getMonth()
Get the month of the date.

Returns:
the month of the date.

setMonth

public void setMonth(int m)
Set the month of the date.

No check is done on the month value. Caller should check for valid value.

Parameters:
m - new month value for the date.

getDay

public int getDay()
Get the day of the date.

Returns:
day of the date.

setDay

public void setDay(int d)
Set the day of the date.

No check is done on the day value. Caller should check for valid value.

Parameters:
d - new day value for the date.

compareTo

public int compareTo(java.lang.Object o)
              throws java.lang.ClassCastException
Compares this XsDate object with the specified object for order.

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Empty XsDate object is less than any other date.

If the Object argument is null, return a positive value.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

If compared to null always return positive value.

Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this object.

getDayOfWeek

public int getDayOfWeek()
Returns the day of week, starting from Monday = 1. A 0 value means empty or invalid date.

Returns:
the day of week.

getDayOfYear

public int getDayOfYear()
Returns the day of year, starting from 1. A 0 value means empty or invalid date.

Returns:
the day of year.

getWeekOfYear

public int getWeekOfYear()
Returns the week of year, starting from 1. A 0 value means empty or invalid date.

Returns:
the week of year.

addDays

public void addDays(int amount)
Add the given number of days to the date value. Year and month components are adjusted accordingly.

Parameters:
amount - number of day to add, can be negative value.

differenceDays

public int differenceDays(XsDate other)
Calculate the difference in days between this date and other. That is, the time elapsed from this object to other expressed in days.

Parameters:
other - the date to be compared.
Returns:
the difference in days between this XsDate object and the given date.

differenceDays

public static int differenceDays(XsDate from,
                                 XsDate to)
Calculate the difference in days between from and to. That is the time elapsed from from to to expressed in days.

Parameters:
from - starting date.
to - ending date.
Returns:
difference in days between the two dates.

main

public static void main(java.lang.String[] args)
Some simple code to test the class.

Parameters:
args - command line arguments.