Class Calendars


  • public class Calendars
    extends java.lang.Object
    Constants and utilities for working with dates and times.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int WEEK_DAY_COUNT
      The number of days in a week.
    • Constructor Summary

      Constructors 
      Constructor Description
      Calendars()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <C extends java.util.Calendar>
      C
      add​(C calendar, int field, int amount)
      Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules, and returns the given calendar.
      static <C extends java.util.Calendar>
      C
      clearDate​(C calendar)
      Clears the date-related calendar fields: Calendar.ERA Calendar.YEAR Calendar.MONTH Calendar.WEEK_OF_YEAR Calendar.WEEK_OF_MONTH Calendar.DAY_OF_MONTH Calendar.DAY_OF_YEAR Calendar.DAY_OF_WEEK Calendar.DAY_OF_WEEK_IN_MONTH
      static <C extends java.util.Calendar>
      C
      clearTime​(C calendar)
      Clears the time-related calendar fields: Calendar.HOUR_OF_DAY Calendar.HOUR Calendar.AM_PM Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
      static int getDayDifference​(java.util.Calendar calendar1, java.util.Calendar calendar2)
      Determines the difference between the two calendars.
      static <C extends java.util.Calendar>
      C
      setDateTime​(C calendar, int year, int month, int date, int hour, int minute, int second, int millisecond)
      Sets the date and time-related fields, including milliseconds.
      static <C extends java.util.Calendar>
      C
      setTime​(C calendar, int hour, int minute, int second, int millisecond)
      Sets the time-related calendar fields: Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
      static <C extends java.util.Calendar>
      C
      setTime​(C calendar, java.util.Calendar fromCalendar)
      Sets the following time-related calendar fields from a given calendar: Calendar.HOUR_OF_DAY Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • WEEK_DAY_COUNT

        public static final int WEEK_DAY_COUNT
        The number of days in a week.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Calendars

        public Calendars()
    • Method Detail

      • add

        public static <C extends java.util.Calendar> C add​(C calendar,
                                                           int field,
                                                           int amount)
        Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules, and returns the given calendar.

        This is equivalent to calling Calendar.add(int, int) and returning the given calendar.

        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar to which to add the value.
        field - The calendar field.
        amount - The amount of date or time to be added to the field.
        Returns:
        The calendar after the modifications on the given amount of time.
        See Also:
        Calendar.add(int, int)
      • clearDate

        public static <C extends java.util.Calendar> C clearDate​(C calendar)
        Clears the date-related calendar fields:
        1. Calendar.ERA
        2. Calendar.YEAR
        3. Calendar.MONTH
        4. Calendar.WEEK_OF_YEAR
        5. Calendar.WEEK_OF_MONTH
        6. Calendar.DAY_OF_MONTH
        7. Calendar.DAY_OF_YEAR
        8. Calendar.DAY_OF_WEEK
        9. Calendar.DAY_OF_WEEK_IN_MONTH
        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar the time of which to reset.
        Returns:
        The calendar being modified.
        Throws:
        java.lang.NullPointerException - if the given calendar is null.
      • clearTime

        public static <C extends java.util.Calendar> C clearTime​(C calendar)
        Clears the time-related calendar fields:
        1. Calendar.HOUR_OF_DAY
        2. Calendar.HOUR
        3. Calendar.AM_PM
        4. Calendar.MINUTE
        5. Calendar.SECOND
        6. Calendar.MILLISECOND
        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar the time of which to reset.
        Returns:
        The calendar being modified.
        Throws:
        java.lang.NullPointerException - if the given calendar is null.
      • setTime

        public static <C extends java.util.Calendar> C setTime​(C calendar,
                                                               java.util.Calendar fromCalendar)
        Sets the following time-related calendar fields from a given calendar:
        1. Calendar.HOUR_OF_DAY
        2. Calendar.MINUTE
        3. Calendar.SECOND
        4. Calendar.MILLISECOND
        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar the time of which to set.
        fromCalendar - The calendar from which to get the time.
        Returns:
        The calendar after the time being set up.
        Throws:
        java.lang.NullPointerException - if either of the given calendars is null.
      • setDateTime

        public static <C extends java.util.Calendar> C setDateTime​(C calendar,
                                                                   int year,
                                                                   int month,
                                                                   int date,
                                                                   int hour,
                                                                   int minute,
                                                                   int second,
                                                                   int millisecond)
        Sets the date and time-related fields, including milliseconds.
        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar to be set up the date time.
        year - The year.
        month - The zero-based month.
        date - The day of the month.
        hour - The hour of the day.
        minute - The minute of the hour.
        second - The second of the minute.
        millisecond - The millisecond of the second.
        Returns:
        The calendar being modified.
        Throws:
        java.lang.NullPointerException - if the given calendar is null.
        See Also:
        Calendar.set(int, int, int, int, int, int)
      • setTime

        public static <C extends java.util.Calendar> C setTime​(C calendar,
                                                               int hour,
                                                               int minute,
                                                               int second,
                                                               int millisecond)
        Sets the time-related calendar fields:
        1. Calendar.HOUR_OF_DAY
        2. Calendar.MINUTE
        3. Calendar.SECOND
        4. Calendar.MILLISECOND
        Type Parameters:
        C - The type of the calendar.
        Parameters:
        calendar - The calendar the time of which to set.
        hour - The hour of the day.
        minute - The minute of the hour.
        second - The second of the minute.
        millisecond - The millisecond of the second.
        Returns:
        The calendar being modified.
        Throws:
        java.lang.NullPointerException - if the given calendar is null.
      • getDayDifference

        public static int getDayDifference​(java.util.Calendar calendar1,
                                           java.util.Calendar calendar2)
        Determines the difference between the two calendars. This is equivalent conceptually to calendar1Days - calendar2Days. If the first calendar date is smaller than the second, a negative number will be returned. The time zones of each calendar are left as they are.

        This method correctly works with leap days.

        Parameters:
        calendar1 - The first calendar of the difference.
        calendar2 - The first calendar of the difference.
        Returns:
        The difference between the calendars, in days.
        Throws:
        java.lang.IllegalArgumentException - if the number of days between the calendars would be greater than an integer.