consciouscode.util
Class Dates

java.lang.Object
  extended by consciouscode.util.Dates

public final class Dates
extends Object

Utility class containing date manipulation routines.

See Also:
Calendar, Date

Constructor Summary
Dates()
           
 
Method Summary
static int computeAge(Date fromDate)
          Determines the age, in years, since the fromDate until the present date.
static int computeAge(Date fromDate, Calendar calendar)
          Determines the age, in years, since the fromDate until the present date.
static Date computeRelativeDay(Date originalDate, long dayDifference)
          Create a new Date object whose time is a given number of days away from the given date.
static boolean dateIsValid(int year, int month, int day, Calendar calendar)
          Determine whether the proposed date actually exists.
static Date makeTodaysDate()
          Create a new Date instance with the current date, but all fields smaller than Day of Month set to zero.
static Date makeTodaysDate(Calendar calendar)
          Create a new Date instance with the current date, but all fields smaller than Day of Month set to zero.
static Date makeValidDate(int year, int month, int day)
          Create a new Date instance representing the given calendar date.
static Date makeValidDate(int year, int month, int day, Calendar calendar)
          Create a new Date instance representing the given calendar date.
static Date truncateToDay(Date date)
          Return a Date similar to the given date, but with all fields smaller than Day of Month set to zero.
static Date truncateToDay(Date date, Calendar calendar)
          Return a Date similar to the given date, but with all fields smaller than Day of Month set to zero.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Dates

public Dates()
Method Detail

dateIsValid

public static boolean dateIsValid(int year,
                                  int month,
                                  int day,
                                  Calendar calendar)
Determine whether the proposed date actually exists.

Parameters:
month - is one-based!
calendar - will be set to the proposed date if it exists, or to an indeterminate time if the proposed date does not exist.
Returns:
true when the proposed date exists in the calendar. In addition, if the date exists, calendar will be set to contain it.

makeValidDate

public static Date makeValidDate(int year,
                                 int month,
                                 int day,
                                 Calendar calendar)
Create a new Date instance representing the given calendar date.

Parameters:
month - is one-based!
calendar - will be set to the proposed date if it exists, or to an indeterminate time if the proposed date does not exist.
Returns:
the appropriate calendar Date, or null if the proposed date does not actually exist (e.g., December 32).

makeValidDate

public static Date makeValidDate(int year,
                                 int month,
                                 int day)
Create a new Date instance representing the given calendar date. This is the same as makeValidDate(int, int, int, Calendar), but using the default Calendar.

Parameters:
month - is one-based!
Returns:
the appropriate calendar Date, or null if the proposed date does not actually exist (e.g., December 32).

truncateToDay

public static Date truncateToDay(Date date,
                                 Calendar calendar)
Return a Date similar to the given date, but with all fields smaller than Day of Month set to zero.

Parameters:
calendar - will be set to the truncated date.

truncateToDay

public static Date truncateToDay(Date date)
Return a Date similar to the given date, but with all fields smaller than Day of Month set to zero.

This is the same as truncateToDay(Date, Calendar), but using the default Calendar.


makeTodaysDate

public static Date makeTodaysDate(Calendar calendar)
Create a new Date instance with the current date, but all fields smaller than Day of Month set to zero.

Note that this is not the same as new Date() since that will be accurate to the current millisecond.

Parameters:
calendar - will be set to the today's date, with all fields smaller than Day of Month set to zero.

makeTodaysDate

public static Date makeTodaysDate()
Create a new Date instance with the current date, but all fields smaller than Day of Month set to zero.

Note that this is not the same as new Date() since that will be accurate to the current millisecond.

This is the same as makeTodaysDate(Calendar), but using the default Calendar.


computeRelativeDay

public static Date computeRelativeDay(Date originalDate,
                                      long dayDifference)
Create a new Date object whose time is a given number of days away from the given date.

Parameters:
originalDate - the starting point in time.
dayDifference - the number of days to roll into the future (if positive) or past (if negative).

computeAge

public static int computeAge(Date fromDate)
Determines the age, in years, since the fromDate until the present date. This is the same as computeAge(Date, Calendar), using the default Calendar.


computeAge

public static int computeAge(Date fromDate,
                             Calendar calendar)
Determines the age, in years, since the fromDate until the present date.

Parameters:
calendar - will be set to fromDate.


Copyright © 2001–2012 Todd V. Jonker. All Rights Reserved.