GeoSoft API

no.geosoft.cc.util
Class Day

Object
  extended by Day
All Implemented Interfaces:
Comparable<Day>
Direct Known Subclasses:
Time

public final class Day
extends Object
implements Comparable<Day>

A time-less and immutable Date class for basic date arithmetics.

Author:
GeoSoft

Constructor Summary
Day()
          Create a new day representing the day of creation (according to the setting of the current machine).
Day(Calendar calendar)
          Create a new day based on a java.util.Calendar instance.
Day(Date date)
          Create a new day based on a java.util.Date instance.
Day(Day day)
          Create a new day as a copy of the specified day.
Day(int year, int dayOfYear)
          Create a new day, specifying the year and the day of year.
Day(int year, int month, int dayOfMonth)
          Create a new day.
Day(long time)
          Create a new day based on a time value.
 
Method Summary
 Day addDays(int nDays)
          Return a day which is the given number of days after this day.
 Day addMonths(int nMonths)
          Return a day wich is a given number of month after this day.
 Day addYears(int nYears)
          Return a day wich is a given number of years after this day.
 int compareTo(Day day)
          Compare this day to the specified day.
 int daysBetween(Day day)
          Return number of days between two days.
 boolean equals(Object object)
          Return true if this day equals (represent the same date) as the specified day.
 Calendar getCalendar()
          Return a Calendar instance representing the same day as this instance.
 Date getDate()
          Return a Date instance representing the same date as this instance.
 String getDayName()
          Get default locale name of this day ("Monday", "Tuesday", etc.
 int getDayNumberOfWeek()
          Return the day number of week of this day, where Monday=1, Tuesday=2, ...
 int getDayOfMonth()
          Return day of month of this day.
 int getDayOfWeek()
          Return the day of week of this day.
 int getDayOfYear()
          Return the day number of year this day represents.
 int getDaysInMonth()
          Return the number of days in the month of this day.
 int getDaysInYear()
          Return the number of days in the year of this day.
static Day getFirstOfMonth(int dayOfWeek, int month, int year)
          Find the first of a specific day in a given month.
static Day getLastOfMonth(int dayOfWeek, int month, int year)
          Find the last of a specific day in a given month.
 int getMonth()
          Return month of this day.
 int getMonthNo()
          Return the 1-based month number of the month of this day. 1 = January, 2 = February and so on.
static Day getNthOfMonth(int n, int dayOfWeek, int month, int year)
          Find the n'th xxxxday of s specified month (for instance find 1st sunday of May 2006; findNthOfMonth (1, Calendar.SUNDAY, Calendar.MAY, 2006); Return null if the specified day doesn't exists.
 int getWeekOfYear()
          Return the week number of year, this day belongs to. 1st=1 and so on.
 int getYear()
          Return year of this day.
 int hashCode()
          Overload required as default definition of equals() has changed.
 boolean isAfter(Day day)
          Return true if this day is after the specified day.
 boolean isBefore(Day day)
          Return true if this day is before the specified day.
 boolean isLeapYear()
          Return true if the year of this day is a leap year.
static boolean isLeapYear(int year)
          Return true if the specified year is a leap year.
static void main(String[] arguments)
          Testing this class.
 Day subtractDays(int nDays)
          Subtract a number of days from this day.
 Day subtractMonths(int nMonths)
          Subtract a number of months from this day.
 Day subtractYears(int nYears)
          Subtract a number of years from this day.
static Day today()
          A more explicit front-end to the Day() constructor which return a day object representing the day of creation.
 String toString()
          Return a scratch string representation of this day.
 
Methods inherited from class Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Day

public Day(int year,
           int month,
           int dayOfMonth)
Create a new day. The day is lenient meaning that illegal day parameters can be specified and results in a recomputed day with legal month/day values.

Parameters:
year - Year of new day.
month - Month of new day (0-11)
dayOfMonth - Day of month of new day (1-31)

Day

public Day(int year,
           int dayOfYear)
Create a new day, specifying the year and the day of year. The day is lenient meaning that illegal day parameters can be specified and results in a recomputed day with legal month/day values.

Parameters:
year - Year of new day.
dayOfYear - 1=January 1, etc.

Day

public Day()
Create a new day representing the day of creation (according to the setting of the current machine).


Day

public Day(Calendar calendar)
Create a new day based on a java.util.Calendar instance. NOTE: The time component from calendar will be pruned.

Parameters:
calendar - Calendar instance to copy.
Throws:
IllegalArgumentException - If calendar is null.

Day

public Day(Date date)
Create a new day based on a java.util.Date instance. NOTE: The time component from date will be pruned.

Parameters:
date - Date instance to copy.
Throws:
IllegalArgumentException - If date is null.

Day

public Day(long time)
Create a new day based on a time value. Time is milliseconds since "the Epoch" (1.1.1970). NOTE: The time component from time will be pruned.

Parameters:
time - Milliseconds since "the Epoch".

Day

public Day(Day day)
Create a new day as a copy of the specified day.

Parameters:
day - Day to clone.
Throws:
IllegalArgumentException - If day is null.
Method Detail

today

public static Day today()
A more explicit front-end to the Day() constructor which return a day object representing the day of creation.

Returns:
A day instance representing today.

getCalendar

public Calendar getCalendar()
Return a Calendar instance representing the same day as this instance. For use by secondary methods requiring java.util.Calendar as input.

Returns:
Calendar equivalent representing this day.

getDate

public Date getDate()
Return a Date instance representing the same date as this instance. For use by secondary methods requiring java.util.Date as input.

Returns:
Date equivalent representing this day.

compareTo

public int compareTo(Day day)
Compare this day to the specified day. If object is not of type Day a ClassCastException is thrown.

Specified by:
compareTo in interface Comparable<Day>
Parameters:
day - Day object to compare to.
Returns:
@see Comparable#compareTo(Object)
Throws:
IllegalArgumentException - If day is null.

isAfter

public boolean isAfter(Day day)
Return true if this day is after the specified day.

Parameters:
day - Day to compare to.
Returns:
True if this is after day, false otherwise.
Throws:
IllegalArgumentException - If day is null.

isBefore

public boolean isBefore(Day day)
Return true if this day is before the specified day.

Parameters:
day - Day to compare to.
Returns:
True if this is before day, false otherwise.
Throws:
IllegalArgumentException - If day is null.

equals

public boolean equals(Object object)
Return true if this day equals (represent the same date) as the specified day.

Overrides:
equals in class Object
Parameters:
object - Object to compare to.
Returns:
True if this equals day, false otherwise.
Throws:
IllegalArgumentException - If day is null.

hashCode

public int hashCode()
Overload required as default definition of equals() has changed.

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

getYear

public int getYear()
Return year of this day.

Returns:
Year of this day.

getMonth

public int getMonth()
Return month of this day. The result must be compared to Calendar.JANUARY, Calendar.FEBRUARY, etc.

Returns:
Month of this day.

getMonthNo

public int getMonthNo()
Return the 1-based month number of the month of this day. 1 = January, 2 = February and so on.

Returns:
Month number of this month

getDayOfMonth

public int getDayOfMonth()
Return day of month of this day. NOTE: First day of month is 1 (not 0).

Returns:
Day of month of this day.

getDayOfYear

public int getDayOfYear()
Return the day number of year this day represents. January 1 = 1 and so on.

Returns:
day number of year.

getDayOfWeek

public int getDayOfWeek()
Return the day of week of this day. NOTE: Must be compared to Calendar.MONDAY, TUSEDAY etc.

Returns:
Day of week of this day.

getDayNumberOfWeek

public int getDayNumberOfWeek()
Return the day number of week of this day, where Monday=1, Tuesday=2, ... Sunday=7.

Returns:
Day number of week of this day.

getWeekOfYear

public int getWeekOfYear()
Return the week number of year, this day belongs to. 1st=1 and so on.

Returns:
Week number of year of this day.

addDays

public Day addDays(int nDays)
Return a day which is the given number of days after this day.

Parameters:
nDays - Number of days to add. May be negative.
Returns:
Day as requested.

subtractDays

public Day subtractDays(int nDays)
Subtract a number of days from this day.

Parameters:
nDays - Number of days to subtract.
Returns:
Day as requested.

addMonths

public Day addMonths(int nMonths)
Return a day wich is a given number of month after this day. The actual number of days added depends on the staring day. Subtracting a number of months can be done by a negative argument to addMonths() or calling subtactMonths() explicitly. NOTE: addMonth(n) m times will in general give a different result than addMonth(m*n). Add 1 month to January 31, 2005 will give February 28, 2005.

Parameters:
nMonths - Number of months to add.
Returns:
Day as requested.

subtractMonths

public Day subtractMonths(int nMonths)
Subtract a number of months from this day.

Parameters:
nMonths - Number of months to subtract.
Returns:
Day as requested.
See Also:
addMonths(int).

addYears

public Day addYears(int nYears)
Return a day wich is a given number of years after this day. Add a number of years to this day. The actual number of days added depends on the starting day. Subtracting a number of years can be done by a negative argument to addYears() or calling subtractYears explicitly.

Parameters:
nYears - Number of years to add.
Returns:
Day as requested.

subtractYears

public Day subtractYears(int nYears)
Subtract a number of years from this day.

Parameters:
nYears - Number of years to subtract.
Returns:
Day as requested.
See Also:
addYears(int).

getDaysInYear

public int getDaysInYear()
Return the number of days in the year of this day.

Returns:
Number of days in this year.

isLeapYear

public boolean isLeapYear()
Return true if the year of this day is a leap year.

Returns:
True if this year is a leap year, false otherwise.

isLeapYear

public static boolean isLeapYear(int year)
Return true if the specified year is a leap year.

Parameters:
year - Year to check.
Returns:
True if specified year is leap year, false otherwise.

getDaysInMonth

public int getDaysInMonth()
Return the number of days in the month of this day.

Returns:
Number of days in this month.

getDayName

public String getDayName()
Get default locale name of this day ("Monday", "Tuesday", etc.

Returns:
Name of day.

daysBetween

public int daysBetween(Day day)
Return number of days between two days. The method always returns a positive number of days.

Parameters:
day - The day to compare to.
Returns:
Number of days between this and day.
Throws:
IllegalArgumentException - If day is null.

getNthOfMonth

public static Day getNthOfMonth(int n,
                                int dayOfWeek,
                                int month,
                                int year)
Find the n'th xxxxday of s specified month (for instance find 1st sunday of May 2006; findNthOfMonth (1, Calendar.SUNDAY, Calendar.MAY, 2006); Return null if the specified day doesn't exists.

Parameters:
n - Nth day to look for.
dayOfWeek - Day to look for (Calendar.XXXDAY).
month - Month to check (Calendar.XXX).
year - Year to check.
Returns:
Required Day (or null if non-existent)
Throws:
IllegalArgumentException - if dyaOfWeek parameter doesn't represent a valid day.

getFirstOfMonth

public static Day getFirstOfMonth(int dayOfWeek,
                                  int month,
                                  int year)
Find the first of a specific day in a given month. For instance first Tuesday of May: getFirstOfMonth(Calendar.TUESDAY, Calendar.MAY, 2005);

Parameters:
dayOfWeek - Weekday to get.
month - Month of day to get.
year - Year of day to get.
Returns:
The requested day.

getLastOfMonth

public static Day getLastOfMonth(int dayOfWeek,
                                 int month,
                                 int year)
Find the last of a specific day in a given month. For instance last Tuesday of May: getLastOfMonth (Calendar.TUESDAY, Calendar.MAY, 2005);

Parameters:
dayOfWeek - Weekday to get.
month - Month of day to get.
year - Year of day to get.
Returns:
The requested day.

toString

public String toString()
Return a scratch string representation of this day. Used for debugging only. The format of the day is dd/mm-yyyy

Overrides:
toString in class Object
Returns:
A string representation of this day.

main

public static void main(String[] arguments)
Testing this class.

Parameters:
arguments - Not used.

GeoSoft API

Copyright © 2004 - Geotechnical Software Services     geosoft.no