Class Calendars

java.lang.Object
com.globalmentor.time.Calendars

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

    • WEEK_DAY_COUNT

      public static final int WEEK_DAY_COUNT
      The number of days in a week.
      See Also:
  • Constructor Details

    • Calendars

      public Calendars()
  • Method Details

    • add

      public static <C extends 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:
    • clearDate

      public static <C extends Calendar> C clearDate(C calendar)
      Type Parameters:
      C - The type of the calendar.
      Parameters:
      calendar - The calendar the time of which to reset.
      Returns:
      The calendar being modified.
      Throws:
      NullPointerException - if the given calendar is null.
    • clearTime

      public static <C extends Calendar> C clearTime(C calendar)
      Type Parameters:
      C - The type of the calendar.
      Parameters:
      calendar - The calendar the time of which to reset.
      Returns:
      The calendar being modified.
      Throws:
      NullPointerException - if the given calendar is null.
    • setTime

      public static <C extends Calendar> C setTime(C calendar, 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:
      NullPointerException - if either of the given calendars is null.
    • setDateTime

      public static <C extends 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:
      NullPointerException - if the given calendar is null.
      See Also:
    • setTime

      public static <C extends Calendar> C setTime(C calendar, int hour, int minute, int second, int 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:
      NullPointerException - if the given calendar is null.
    • getDayDifference

      public static int getDayDifference(Calendar calendar1, 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:
      IllegalArgumentException - if the number of days between the calendars would be greater than an integer.