org.ujac.util
Class DateUtils

java.lang.Object
  extended by org.ujac.util.DateUtils

public class DateUtils
extends Object

Name: DateUtils
Description: Provides calendar and date utilities for more convenient date handling.

Author:
lauerc

Constructor Summary
DateUtils()
           
 
Method Summary
static Date beginOfYear(Date d)
          Gets the begin of year for the given date.
static Calendar createCalendar(Date date)
          Creates a Calendar instance for the given date.
static Calendar createCalendar(Date date, int precision)
          Creates a Calendar instance for the given date.
static Calendar createCalendar(int precision)
          Creates a Calendar instance for the current time.
static Calendar createCalendar(int year, int month, int day)
          Creates a Calendar instance for the given arguments.
static Calendar createCalendar(int year, int month, int day, int hour, int minute, int second)
          Creates a Calendar instance for the given arguments.
static Calendar createCalendar(int year, int month, int day, int hour, int minute, int second, int millisecond)
          Creates a Calendar instance for the given arguments.
static Date createDate(int year, int month, int day)
          Creates a Date instance for the given arguments.
static Date createDate(int year, int month, int day, int hour, int minute, int second)
          Creates a Date instance for the given arguments.
static Date createDate(int year, int month, int day, int hour, int minute, int second, int millisecond)
          Creates a Date instance for the given arguments.
static Date endOfYear(Date d)
          Gets the end of year for the given date.
static int getDay(Calendar cal)
          Gets the day of month from the given calendar.
static int getDay(Date date)
          Gets the day of month from the given date.
static int getHour(Calendar cal)
          Gets the millisecond from the given calendar.
static int getHour(Date date)
          Gets the millisecond from the given date.
static int getMillisecond(Calendar cal)
          Gets the millisecond from the given calendar.
static int getMillisecond(Date date)
          Gets the millisecond from the given calendar.
static int getMinute(Calendar cal)
          Gets the minute from the given calendar.
static int getMinute(Date date)
          Gets the minute from the given date.
static int getMonth(Calendar cal)
          Gets the month from the given calendar.
static int getMonth(Date date)
          Gets the month from the given date.
static int getSecond(Calendar cal)
          Gets the second from the given calendar.
static int getSecond(Date date)
          Gets the second from the given date.
static int getYear(Calendar cal)
          Gets the year from the given calendar.
static int getYear(Date date)
          Gets the year from the given date.
static void incrDay(Calendar cal, int days)
          Increments the given calendar by the given amount of days.
static Date incrDay(Date date, int days)
          Increments the given date by the given amount of days.
static void incrHour(Calendar cal, int hours)
          Increments the given calendar by the given amount of hours.
static Date incrHour(Date date, int hours)
          Increments the given date by the given amount of hours.
static void incrMillisecond(Calendar cal, int milliseconds)
          Increments the given calendar by the given amount of milliseconds.
static Date incrMillisecond(Date date, int milliseconds)
          Increments the given calendar by the given amount of milliseconds.
static void incrMinute(Calendar cal, int minutes)
          Increments the given calendar by the given amount of minutes.
static Date incrMinute(Date date, int minutes)
          Increments the given date by the given amount of minutes.
static void incrMonth(Calendar cal, int months)
          Increments the given calendar by the given amount of months.
static Date incrMonth(Date date, int months)
          Increments the given date by the given amount of months.
static void incrSecond(Calendar cal, int seconds)
          Increments the given calendar by the given amount of seconds.
static Date incrSecond(Date date, int seconds)
          Increments the given date by the given amount of seconds.
static void incrYear(Calendar cal, int years)
          Increments the given calendar by the given amount of years.
static Date incrYear(Date date, int year)
          Increments the given date by the given amount of years.
static Date max(Date a, Date b)
          Gets the minimum of the given two date values.
static Date min(Date a, Date b)
          Gets the minimum of the given two date values.
static Date nextUltimo(Date d)
          Gets the next ultimo for the given date.
static Date now()
          Gets the current time.
static Date now(int precision)
          Gets the current time.
static Date previousUltimo(Date d)
          Gets the previous ultimo for the given date.
static void setDay(Calendar cal, int day)
          Sets the day of month at the given calendar.
static Date setDay(Date date, int day)
          Sets the day of month at the given date.
static void setHour(Calendar cal, int hour)
          Sets the millisecond at the given calendar.
static Date setHour(Date date, int hour)
          Sets the millisecond at the given date.
static void setMillisecond(Calendar cal, int millisecond)
          Sets the millisecond at the given calendar.
static Date setMillisecond(Date date, int millisecond)
          Sets the millisecond at the given calendar.
static void setMinute(Calendar cal, int minute)
          Sets the minute at the given calendar.
static Date setMinute(Date date, int minute)
          Sets the minute at the given date.
static void setMonth(Calendar cal, int month)
          Sets the month at the given calendar.
static Date setMonth(Date date, int month)
          Sets the month at the given date.
static void setSecond(Calendar cal, int second)
          Sets the second at the given calendar.
static Date setSecond(Date date, int second)
          Sets the second at the given date.
static void setYear(Calendar cal, int year)
          Sets the year at the given calendar.
static Date setYear(Date date, int year)
          Sets the year at the given date.
static Date today()
          Gets the current date.
static Date yesterday()
          Gets the current date -1 day.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtils

public DateUtils()
Method Detail

today

public static final Date today()
Gets the current date.

Returns:
The current date with all time values set to minimum.

yesterday

public static final Date yesterday()
Gets the current date -1 day.

Returns:
The current date -1 day with all time values set to minimum.

now

public static final Date now()
Gets the current time.

Returns:
The current time with milliseconds set to minimum.

now

public static final Date now(int precision)
Gets the current time.

Parameters:
precision - The ID of the most precise value of interest.
Returns:
The current timestamp with all values more acurate than the passed field ID set to minimum.
See Also:
Calendar.MILLISECOND, Calendar.SECOND, Calendar.MINUTE, Calendar.HOUR

createCalendar

public static final Calendar createCalendar(int year,
                                            int month,
                                            int day)
Creates a Calendar instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
Returns:
The created calendar instance.

createCalendar

public static final Calendar createCalendar(int year,
                                            int month,
                                            int day,
                                            int hour,
                                            int minute,
                                            int second)
Creates a Calendar instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
hour - The hour to set.
minute - The minute to set.
second - The second to set.
Returns:
The created calendar instance.

createCalendar

public static final Calendar createCalendar(int year,
                                            int month,
                                            int day,
                                            int hour,
                                            int minute,
                                            int second,
                                            int millisecond)
Creates a Calendar instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
hour - The hour to set.
minute - The minute to set.
second - The second to set.
millisecond - The millisecond to set.
Returns:
The created calendar instance.

createCalendar

public static final Calendar createCalendar(Date date)
Creates a Calendar instance for the given date.

Parameters:
date - The date to set for the new calendar.
Returns:
The created calendar instance.

createCalendar

public static final Calendar createCalendar(int precision)
Creates a Calendar instance for the current time.

Parameters:
precision - The ID of the most precise value of interest.
Returns:
The created calendar instance with all values more acurate than the passed field ID set to minimum.
See Also:
Calendar.MILLISECOND, Calendar.SECOND, Calendar.MINUTE, Calendar.HOUR

createCalendar

public static final Calendar createCalendar(Date date,
                                            int precision)
Creates a Calendar instance for the given date.

Parameters:
date - The date to set for the new calendar.
precision - The ID of the most precise value of interest.
Returns:
The created calendar instance with all values more acurate than the passed field ID set to minimum.
See Also:
Calendar.MILLISECOND, Calendar.SECOND, Calendar.MINUTE, Calendar.HOUR

createDate

public static final Date createDate(int year,
                                    int month,
                                    int day)
Creates a Date instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
Returns:
The created date instance.

createDate

public static final Date createDate(int year,
                                    int month,
                                    int day,
                                    int hour,
                                    int minute,
                                    int second)
Creates a Date instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
hour - The hour to set.
minute - The minute to set.
second - The second to set.
Returns:
The created date instance.

createDate

public static final Date createDate(int year,
                                    int month,
                                    int day,
                                    int hour,
                                    int minute,
                                    int second,
                                    int millisecond)
Creates a Date instance for the given arguments.

Parameters:
year - The year to set.
month - The month to set.
day - The day to set.
hour - The hour to set.
minute - The minute to set.
second - The second to set.
millisecond - The millisecond to set.
Returns:
The created date instance.

getDay

public static final int getDay(Calendar cal)
Gets the day of month from the given calendar.

Parameters:
cal - The calendar to get the current day of month from.
Returns:
The determined day of month.

getDay

public static final int getDay(Date date)
Gets the day of month from the given date.

Parameters:
date - The date to get the current day of month from.
Returns:
The determined day of month.

setDay

public static final void setDay(Calendar cal,
                                int day)
Sets the day of month at the given calendar.

Parameters:
cal - The calendar to set the current day of month at.
day - The day of month to set for the calendar.

setDay

public static final Date setDay(Date date,
                                int day)
Sets the day of month at the given date.

Parameters:
date - The calendar to set the current day of month at.
day - The day of month to set for the date.
Returns:
The resulting date.

incrDay

public static final void incrDay(Calendar cal,
                                 int days)
Increments the given calendar by the given amount of days.

Parameters:
cal - The calendar to alter.
days - The number of days to add to the calendar.

incrDay

public static final Date incrDay(Date date,
                                 int days)
Increments the given date by the given amount of days.

Parameters:
date - The date to alter.
days - The number of days to add to the date.
Returns:
The resulting date.

getMonth

public static final int getMonth(Calendar cal)
Gets the month from the given calendar. Since the Calendar's month index is zero based, which feels not very natural, the served value is incremented by 1 (so: January = 1, February = 2, ...).

Parameters:
cal - The calendar to get the current month from.
Returns:
The determined month.

getMonth

public static final int getMonth(Date date)
Gets the month from the given date. Since the date's month index is zero based, which feels not very natural, the served value is incremented by 1 (so: January = 1, February = 2, ...).

Parameters:
date - The date to get the current month from.
Returns:
The determined month.

setMonth

public static final void setMonth(Calendar cal,
                                  int month)
Sets the month at the given calendar. The expected month index is 1 based which feels more natural than the Calendar's values (zero based).

Parameters:
cal - The calendar to set the current month at.
month - The month to set for the calendar.

setMonth

public static final Date setMonth(Date date,
                                  int month)
Sets the month at the given date. The expected month index is 1 based which feels more natural than the date's values (zero based).

Parameters:
date - The calendar to set the current month at.
month - The month to set to the date.
Returns:
The resulting date.

incrMonth

public static final void incrMonth(Calendar cal,
                                   int months)
Increments the given calendar by the given amount of months.

Parameters:
cal - The calendar to alter.
months - The number of months to add to the calendar.

incrMonth

public static final Date incrMonth(Date date,
                                   int months)
Increments the given date by the given amount of months.

Parameters:
date - The date to alter.
months - The number of months to add to the date.
Returns:
The resulting date.

getYear

public static final int getYear(Calendar cal)
Gets the year from the given calendar.

Parameters:
cal - The calendar to get the current year from.
Returns:
The determined year.

getYear

public static final int getYear(Date date)
Gets the year from the given date.

Parameters:
date - The date to get the current year from.
Returns:
The determined year.

setYear

public static final void setYear(Calendar cal,
                                 int year)
Sets the year at the given calendar.

Parameters:
cal - The calendar to set the current year at.
year - The year to set for the calendar.

setYear

public static final Date setYear(Date date,
                                 int year)
Sets the year at the given date.

Parameters:
date - The date to set the current year at.
year - The year to set for the date.
Returns:
The resulting date.

incrYear

public static final void incrYear(Calendar cal,
                                  int years)
Increments the given calendar by the given amount of years.

Parameters:
cal - The calendar to alter.
years - The number of years to add to the calendar.

incrYear

public static final Date incrYear(Date date,
                                  int year)
Increments the given date by the given amount of years.

Parameters:
date - The calendar to set the current year at.
year - The year to set for the date.
Returns:
The resulting date.

getHour

public static final int getHour(Calendar cal)
Gets the millisecond from the given calendar.

Parameters:
cal - The calendar to get the current millisecond from.
Returns:
The determined millisecond.

getHour

public static final int getHour(Date date)
Gets the millisecond from the given date.

Parameters:
date - The date to get the current millisecond from.
Returns:
The determined millisecond.

setHour

public static final void setHour(Calendar cal,
                                 int hour)
Sets the millisecond at the given calendar.

Parameters:
cal - The calendar to set the current millisecond at.
hour - The hour to set for the calendar.

setHour

public static final Date setHour(Date date,
                                 int hour)
Sets the millisecond at the given date.

Parameters:
date - The date to set the current millisecond at.
hour - The hour to set for the calendar.
Returns:
The resulting date.

incrHour

public static final void incrHour(Calendar cal,
                                  int hours)
Increments the given calendar by the given amount of hours.

Parameters:
cal - The calendar to alter.
hours - The number of hours to add to the calendar.

incrHour

public static final Date incrHour(Date date,
                                  int hours)
Increments the given date by the given amount of hours.

Parameters:
date - The date to alter.
hours - The number of hours to add to the calendar.
Returns:
The resulting date.

getMinute

public static final int getMinute(Calendar cal)
Gets the minute from the given calendar.

Parameters:
cal - The calendar to get the current minute from.
Returns:
The determined minute.

getMinute

public static final int getMinute(Date date)
Gets the minute from the given date.

Parameters:
date - The date to get the current minute from.
Returns:
The determined minute.

setMinute

public static final void setMinute(Calendar cal,
                                   int minute)
Sets the minute at the given calendar.

Parameters:
cal - The calendar to set the current minute at.
minute - The minute to set for the calendar.

setMinute

public static final Date setMinute(Date date,
                                   int minute)
Sets the minute at the given date.

Parameters:
date - The date to set the current minute at.
minute - The minute to set for the date.
Returns:
The resulting date.

incrMinute

public static final void incrMinute(Calendar cal,
                                    int minutes)
Increments the given calendar by the given amount of minutes.

Parameters:
cal - The calendar to alter.
minutes - The number of minutes to add to the calendar.

incrMinute

public static final Date incrMinute(Date date,
                                    int minutes)
Increments the given date by the given amount of minutes.

Parameters:
date - The date to alter.
minutes - The number of minutes to add to the date.
Returns:
The resulting date.

getSecond

public static final int getSecond(Calendar cal)
Gets the second from the given calendar.

Parameters:
cal - The calendar to get the current second from.
Returns:
The determined second.

getSecond

public static final int getSecond(Date date)
Gets the second from the given date.

Parameters:
date - The date to get the current second from.
Returns:
The determined second.

setSecond

public static final void setSecond(Calendar cal,
                                   int second)
Sets the second at the given calendar.

Parameters:
cal - The calendar to set the current second at.
second - The second to set for the calendar.

setSecond

public static final Date setSecond(Date date,
                                   int second)
Sets the second at the given date.

Parameters:
date - The date to set the current second at.
second - The second to set for the date.
Returns:
The resulting date.

incrSecond

public static final void incrSecond(Calendar cal,
                                    int seconds)
Increments the given calendar by the given amount of seconds.

Parameters:
cal - The calendar to alter.
seconds - The number of seconds to add to the calendar.

incrSecond

public static final Date incrSecond(Date date,
                                    int seconds)
Increments the given date by the given amount of seconds.

Parameters:
date - The date to alter.
seconds - The number of seconds to add to the calendar.
Returns:
The resulting date.

getMillisecond

public static final int getMillisecond(Calendar cal)
Gets the millisecond from the given calendar.

Parameters:
cal - The calendar to get the current millisecond from.
Returns:
The determined millisecond.

getMillisecond

public static final int getMillisecond(Date date)
Gets the millisecond from the given calendar.

Parameters:
date - The date to get the current millisecond from.
Returns:
The determined millisecond.

setMillisecond

public static final void setMillisecond(Calendar cal,
                                        int millisecond)
Sets the millisecond at the given calendar.

Parameters:
cal - The calendar to set the current millisecond at.
millisecond - The millisecond to set for the calendar.

setMillisecond

public static final Date setMillisecond(Date date,
                                        int millisecond)
Sets the millisecond at the given calendar.

Parameters:
date - The date to set the current millisecond at.
millisecond - The millisecond to set for the calendar.
Returns:
The resulting date.

incrMillisecond

public static final void incrMillisecond(Calendar cal,
                                         int milliseconds)
Increments the given calendar by the given amount of milliseconds.

Parameters:
cal - The calendar to alter.
milliseconds - The number of milliseconds to add to the calendar.

incrMillisecond

public static final Date incrMillisecond(Date date,
                                         int milliseconds)
Increments the given calendar by the given amount of milliseconds.

Parameters:
date - The date to alter.
milliseconds - The number of milliseconds to add to the calendar.
Returns:
The resulting date.

min

public static final Date min(Date a,
                             Date b)
Gets the minimum of the given two date values.

Parameters:
a - The first date.
b - The second date.
Returns:
The minimum of a and b.

max

public static final Date max(Date a,
                             Date b)
Gets the minimum of the given two date values.

Parameters:
a - The first date.
b - The second date.
Returns:
The minimum of a and b.

beginOfYear

public static final Date beginOfYear(Date d)
Gets the begin of year for the given date.

Parameters:
d - The date to process.
Returns:
The january 1st in the year of the given date.

endOfYear

public static final Date endOfYear(Date d)
Gets the end of year for the given date.

Parameters:
d - The date to process.
Returns:
The january 1st in the year of the given date.

previousUltimo

public static final Date previousUltimo(Date d)
Gets the previous ultimo for the given date.

Parameters:
d - The date to process.
Returns:
In case the given date equals to the end of a month this date is returned else the end of the previous month is returned.

nextUltimo

public static final Date nextUltimo(Date d)
Gets the next ultimo for the given date.

Parameters:
d - The date to process.
Returns:
Always returns the end of the current month.


Copyright © 2003-2004 UJAC.org. All Rights Reserved.