Class CalendarUtil

java.lang.Object
fc.util.CalendarUtil

public final class CalendarUtil extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    Useful constant of eight hours (in milliseconds)
    static final long
    Useful constant of five minutes (in milliseconds)
    static final long
    Useful constant of four hours (in milliseconds)
    static final long
    Useful constant of 1 day (in milliseconds)
    static final long
    Useful constant of one hour (in milliseconds)
    static final long
    Useful constant of 1 minute (in milliseconds)
    static final long
    Useful constant of 1 month (in milliseconds) [30 day month]
    static final long
    Useful constant of 1 second (in milliseconds)
    static final long
    Useful constant of 1 week (in milliseconds)
    static final long
    Useful constant of one year (in milliseconds) [365 days]
    static final long
    Useful constant of 6 months (in milliseconds) [30 day month]
    static final long
    Useful constant of ten minutes (in milliseconds)
    static final long
    Useful constant of thirty minutes (in milliseconds)
    static final long
    Useful constant of 3 days (in milliseconds)
    static final long
    Useful constant of 3 months (in milliseconds)[30 day month]
    static final long
    Useful constant of twelve hours (in milliseconds)
    static final long
    Useful constant of twenty four hours (in milliseconds)
    static final long
    Useful constant of two hours (in milliseconds)
    static final long
    Useful constant of 2 minutes (in milliseconds)
    static final long
    Useful constant of 2 months (in milliseconds) [30 day month]
    static final long
    Useful constant of 2 weeks (in milliseconds)
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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
    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
    Returns the closest hour, starting from the current time and the specified calendar.
    static Date
    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
    Returns the closest hour, starting from the specified time and the specified calendar.
    static Date
    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
    Returns the end of the hour, starting from the current time and the specified calendar.
    static Date
    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
    Returns the end of the hour, starting from the specified time and the specified calendar.
    static Date
    Returns the first day of the current month with the default locale;
    static Date
    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
    Returns the first day of the specified month with the default locale;
    static Date
    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
    Returns the last day of the current month with the default locale;
    static Date
    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
    Returns the last day of the specified month with the default locale;
    static Date
    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, int nummonths)
    Returns {#getLastNMonths} with the default locale;
    static List
    getLastNMonths(Calendar cal, Date startingDate, int nummonths, Locale locale)
    Returns a List of Date[][], with each item being a month.
    static Timestamp
    returns now as java.sql.Timestamp
    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 Timestamp
    now()
    returns now as java.sql.Timestamp
    static final void
    setMonth(Calendar cal, int monthNum)
    Sets the month in the specified calendar based on the specified 0-based month number.
    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
    withinLastNDays(Date time, int n_days)
    returns true if the specified time is within the specified days, 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
    withinLastNHours(Date 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 Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • 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

      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.
    • getNowTimestamp

      public static Timestamp getNowTimestamp()
      returns now as java.sql.Timestamp
    • now

      public static Timestamp now()
      returns now as java.sql.Timestamp
    • main

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