fc.util
Class CalendarUtil

java.lang.Object
  extended by fc.util.CalendarUtil

public class CalendarUtil
extends Object


Nested Class Summary
static class CalendarUtil.Duration
           
 
Field Summary
static long EIGHT_HOUR
          Useful constant of eight hours (in milliseconds)
static long FIVE_MIN
          Useful constant of five minutes (in milliseconds)
static long FOUR_HOUR
          Useful constant of four hours (in milliseconds)
static long ONE_DAY
          Useful constant of 1 day (in milliseconds)
static long ONE_HOUR
          Useful constant of one hour (in milliseconds)
static long ONE_MIN
          Useful constant of 1 minute (in milliseconds)
static long ONE_MONTH
          Useful constant of 1 month (in milliseconds) [30 day month]
static long ONE_SEC
          Useful constant of 1 second (in milliseconds)
static long ONE_WEEK
          Useful constant of 1 week (in milliseconds)
static long ONE_YEAR
          Useful constant of one year (in milliseconds) [365 days]
static long SIX_MONTH
          Useful constant of 6 months (in milliseconds) [30 day month]
static long TEN_MIN
          Useful constant of ten minutes (in milliseconds)
static long THIRTY_MIN
          Useful constant of thirty minutes (in milliseconds)
static long THREE_DAY
          Useful constant of 3 days (in milliseconds)
static long THREE_MONTH
          Useful constant of 3 months (in milliseconds)[30 day month]
static long TWELVE_HOUR
          Useful constant of twelve hours (in milliseconds)
static long TWENTY_FOUR_HOUR
          Useful constant of twenty four hours (in milliseconds)
static long TWO_HOUR
          Useful constant of two hours (in milliseconds)
static long TWO_MIN
          Useful constant of 2 minutes (in milliseconds)
static long TWO_MONTH
          Useful constant of 2 months (in milliseconds) [30 day month]
static long TWO_WEEK
          Useful constant of 2 weeks (in milliseconds)
 
Constructor Summary
CalendarUtil()
           
 
Method Summary
static Date addHours(Calendar cal, Date d, int hours)
          Returns the date representing the addition/subtraction of a number of hours from the specified starting timestamp.
static long daysToMillis(int days)
          returns the number of millis in the specified number of days
static Date getBeginOfCurrentDay(Calendar cal)
          Returns the date representing the beginning of the current day (at 12:00:00.000 AM), starting from the current time and the specified calendar.
static Date getBeginOfCurrentHour(Calendar cal)
          Returns the closest hour, starting from the current time and the specified calendar.
static Date getBeginOfDay(Calendar cal, Date d)
          Returns the date representing the beginning of the specified day (at 12:00:00.000 AM), starting from the current time and the specified calendar.
static Date getBeginOfHour(Calendar cal, Date d)
          Returns the closest hour, starting from the specified time and the specified calendar.
static Date getEndOfCurrentDay(Calendar cal)
          Returns the date representing the end of the current day (11:59:59.999 PM), starting from the current time and the specified calendar.
static Date getEndOfCurrentHour(Calendar cal)
          Returns the end of the hour, starting from the current time and the specified calendar.
static Date getEndOfDay(Calendar cal, Date d)
          Returns the date representing the end of the specified day (11:59:59.999 PM), starting from the current time and the specified calendar.
static Date getEndOfHour(Calendar cal, Date d)
          Returns the end of the hour, starting from the specified time and the specified calendar.
static Date getFirstDayOfCurrentMonth(Calendar cal)
          Returns the first day of the current month with the default locale;
static Date getFirstDayOfCurrentWeek(Calendar cal)
          Returns the date representing first day of the week (at 12:00:00.000 AM), starting from the current time and the specified calendar.
static Date getFirstDayOfMonth(Calendar cal, Date d)
          Returns the first day of the specified month with the default locale;
static Date getFirstDayOfWeek(Calendar cal, Date d)
          Returns the date representing the first day of the week (at 12:00:00.000 AM), using the specified date as the starting point of the week under consideration and the specified calendar.
static Date getLastDayOfCurrentMonth(Calendar cal)
          Returns the last day of the current month with the default locale;
static Date getLastDayOfCurrentWeek(Calendar cal)
          Returns the date representing the last day of the week (11:59:59.999 PM), starting from the current time and the specified calendar.
static Date getLastDayOfMonth(Calendar cal, Date d)
          Returns the last day of the specified month with the default locale;
static Date getLastDayOfWeek(Calendar cal, Date d)
          Returns the date representing the last day of the week (at 11:59:59.999 PM) using the specified date as the starting point of the week under consideration and the specified calendar.
static List getLastNMonths(Calendar cal, Date startingDate, int nummonths, Locale locale)
          Returns a List of Date[][], with each item being a month.
static List getLastNMonths(Calendar cal, int nummonths)
          Returns {#getLastNMonths} with the default locale;
static int hoursBetween(Date begin, Date end)
          returns number of hours between begin and end timestamps.
static long hoursToMillis(int hours)
          returns the number of millis in the specified number of hours
static void main(String[] args)
           
static long minutesToMillis(int minutes)
          returns the number of millis in the specified hours
static void setMonth(Calendar cal, int monthNum)
          Sets the month in the specified calendar based on the specified 0-based month number.
static boolean withinLastNDays(Date time, int n_days)
          returns true if the specified time is within the specified days, starting from the current time.
static boolean withinLastNDays(long time, int n_days)
          returns true if the specified time is within the specified days, starting from the current time.
static boolean withinLastNHours(Date time, int n_hours)
          returns true if the specified time is within the specified hours, starting from the current time.
static boolean withinLastNHours(long time, int n_hours)
          returns true if the specified time is within the specified hours, starting from the current time.
static boolean withinLastNMin(Date time, int n_min)
          returns true if the specified time is within the specified min, starting from the current time.
static boolean withinLastNSeconds(Date time, int n_seconds)
          returns true if the specified time is within the specified seconds, starting from the current time.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_SEC

public static final long ONE_SEC
Useful constant of 1 second (in milliseconds)

See Also:
Constant Field Values

ONE_MIN

public static final long ONE_MIN
Useful constant of 1 minute (in milliseconds)

See Also:
Constant Field Values

TWO_MIN

public static final long TWO_MIN
Useful constant of 2 minutes (in milliseconds)

See Also:
Constant Field Values

FIVE_MIN

public static final long FIVE_MIN
Useful constant of five minutes (in milliseconds)

See Also:
Constant Field Values

TEN_MIN

public static final long TEN_MIN
Useful constant of ten minutes (in milliseconds)

See Also:
Constant Field Values

THIRTY_MIN

public static final long THIRTY_MIN
Useful constant of thirty minutes (in milliseconds)

See Also:
Constant Field Values

ONE_HOUR

public static final long ONE_HOUR
Useful constant of one hour (in milliseconds)

See Also:
Constant Field Values

TWO_HOUR

public static final long TWO_HOUR
Useful constant of two hours (in milliseconds)

See Also:
Constant Field Values

FOUR_HOUR

public static final long FOUR_HOUR
Useful constant of four hours (in milliseconds)

See Also:
Constant Field Values

EIGHT_HOUR

public static final long EIGHT_HOUR
Useful constant of eight hours (in milliseconds)

See Also:
Constant Field Values

TWELVE_HOUR

public static final long TWELVE_HOUR
Useful constant of twelve hours (in milliseconds)

See Also:
Constant Field Values

TWENTY_FOUR_HOUR

public static final long TWENTY_FOUR_HOUR
Useful constant of twenty four hours (in milliseconds)

See Also:
Constant Field Values

ONE_DAY

public static final long ONE_DAY
Useful constant of 1 day (in milliseconds)

See Also:
Constant Field Values

THREE_DAY

public static final long THREE_DAY
Useful constant of 3 days (in milliseconds)

See Also:
Constant Field Values

ONE_WEEK

public static final long ONE_WEEK
Useful constant of 1 week (in milliseconds)

See Also:
Constant Field Values

TWO_WEEK

public static final long TWO_WEEK
Useful constant of 2 weeks (in milliseconds)

See Also:
Constant Field Values

ONE_MONTH

public static final long ONE_MONTH
Useful constant of 1 month (in milliseconds) [30 day month]

See Also:
Constant Field Values

TWO_MONTH

public static final long TWO_MONTH
Useful constant of 2 months (in milliseconds) [30 day month]

See Also:
Constant Field Values

THREE_MONTH

public static final long THREE_MONTH
Useful constant of 3 months (in milliseconds)[30 day month]

See Also:
Constant Field Values

SIX_MONTH

public static final long SIX_MONTH
Useful constant of 6 months (in milliseconds) [30 day month]

See Also:
Constant Field Values

ONE_YEAR

public static final long ONE_YEAR
Useful constant of one year (in milliseconds) [365 days]

See Also:
Constant Field Values
Constructor Detail

CalendarUtil

public CalendarUtil()
Method Detail

setMonth

public static final void setMonth(Calendar cal,
                                  int monthNum)
Sets the month in the specified calendar based on the specified 0-based month number. There is no direct setMonth method in Calendar. Therefore, this method is essentially a giant switch statement, like:
  case 0: 
          cal.set(Calendar.MONTH, Calendar.JANUARY); 
          break;
  case 1: 
          cal.set(Calendar.MONTH, Calendar.FEBRUARY); 
          break;
  ... etc...


addHours

public static Date addHours(Calendar cal,
                            Date d,
                            int hours)
Returns the date representing the addition/subtraction of a number of hours from the specified starting timestamp. Specify a positive number for future/adding hours or negative for past/subtracing hours.

The state of the calendar is not affected by the calculations performed by this method.


getBeginOfHour

public static Date getBeginOfHour(Calendar cal,
                                  Date d)
Returns the closest hour, starting from the specified time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.


getBeginOfCurrentHour

public static Date getBeginOfCurrentHour(Calendar cal)
Returns the closest hour, starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.


getEndOfHour

public static Date getEndOfHour(Calendar cal,
                                Date d)
Returns the end of the hour, starting from the specified time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.


getEndOfCurrentHour

public static Date getEndOfCurrentHour(Calendar cal)
Returns the end of the hour, starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.


getBeginOfDay

public static Date getBeginOfDay(Calendar cal,
                                 Date d)
Returns the date representing the beginning of the specified day (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getBeginOfCurrentDay

public static Date getBeginOfCurrentDay(Calendar cal)
Returns the date representing the beginning of the current day (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getEndOfDay

public static Date getEndOfDay(Calendar cal,
                               Date d)
Returns the date representing the end of the specified day (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getEndOfCurrentDay

public static Date getEndOfCurrentDay(Calendar cal)
Returns the date representing the end of the current day (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getFirstDayOfWeek

public static Date getFirstDayOfWeek(Calendar cal,
                                     Date d)
Returns the date representing the first day of the week (at 12:00:00.000 AM), using the specified date as the starting point of the week under consideration and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

The first day of week can vary based on the locale associated with the specified calendar (sunday in us, monday in fr, etc).


getFirstDayOfCurrentWeek

public static Date getFirstDayOfCurrentWeek(Calendar cal)
Returns the date representing first day of the week (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getLastDayOfWeek

public static Date getLastDayOfWeek(Calendar cal,
                                    Date d)
Returns the date representing the last day of the week (at 11:59:59.999 PM) using the specified date as the starting point of the week under consideration and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.

The last day of week can vary based on the locale associated with the specified calendar (saturday in us, sunday in fr, etc).


getLastDayOfCurrentWeek

public static Date getLastDayOfCurrentWeek(Calendar cal)
Returns the date representing the last day of the week (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.


getFirstDayOfMonth

public static Date getFirstDayOfMonth(Calendar cal,
                                      Date d)
Returns the first day of the specified month with the default locale;


getFirstDayOfCurrentMonth

public static Date getFirstDayOfCurrentMonth(Calendar cal)
Returns the first day of the current month with the default locale;


getLastDayOfMonth

public static Date getLastDayOfMonth(Calendar cal,
                                     Date d)
Returns the last day of the specified month with the default locale;


getLastDayOfCurrentMonth

public static Date getLastDayOfCurrentMonth(Calendar cal)
Returns the last day of the current month with the default locale;


getLastNMonths

public static List getLastNMonths(Calendar cal,
                                  int nummonths)
Returns {#getLastNMonths} with the default locale;


getLastNMonths

public static List getLastNMonths(Calendar cal,
                                  Date startingDate,
                                  int nummonths,
                                  Locale locale)
Returns a List of Date[][], with each item being a month. The nummonths is the number of months to return and the startingDate is the starting month, to count backwards from.

In the returned list, date[0] represents the first day, 12:00:00.000 AM and date[1] representing the last day, 11:59:59.999 PM. The starting item in the list is the nearest month and each successive item is the prior month.

For example: To get last 6 months, say: getLastNMonths(cal, today, 6) where cal is a calendar to use and today is today's date.


withinLastNDays

public static boolean withinLastNDays(long time,
                                      int n_days)
returns true if the specified time is within the specified days, starting from the current time.


withinLastNDays

public static boolean withinLastNDays(Date time,
                                      int n_days)
returns true if the specified time is within the specified days, starting from the current time.


withinLastNHours

public static boolean withinLastNHours(long time,
                                       int n_hours)
returns true if the specified time is within the specified hours, starting from the current time.


withinLastNHours

public static boolean withinLastNHours(Date time,
                                       int n_hours)
returns true if the specified time is within the specified hours, starting from the current time.


withinLastNMin

public static boolean withinLastNMin(Date time,
                                     int n_min)
returns true if the specified time is within the specified min, starting from the current time.


withinLastNSeconds

public static boolean withinLastNSeconds(Date time,
                                         int n_seconds)
returns true if the specified time is within the specified seconds, starting from the current time.


daysToMillis

public static long daysToMillis(int days)
returns the number of millis in the specified number of days


hoursToMillis

public static long hoursToMillis(int hours)
returns the number of millis in the specified number of hours


minutesToMillis

public static long minutesToMillis(int minutes)
returns the number of millis in the specified hours


hoursBetween

public static int hoursBetween(Date begin,
                               Date end)
returns number of hours between begin and end timestamps. the order of begin/end is irrelevant, uses absolute value of difference between the two for the calculation.


main

public static void main(String[] args)
                 throws Exception
Throws:
Exception