Package com.globalmentor.time
Class Calendars
- java.lang.Object
-
- com.globalmentor.time.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>
Cadd(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>
CclearDate(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>
CclearTime(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>
CsetDateTime(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>
CsetTime(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>
CsetTime(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
-
-
-
Field Detail
-
WEEK_DAY_COUNT
public static final int WEEK_DAY_COUNT
The number of days in a week.- See Also:
- Constant Field Values
-
-
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: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
- 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 isnull
.
-
clearTime
public 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
- 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 isnull
.
-
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:Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
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 isnull
.
-
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 isnull
.- 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:Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
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 isnull
.
-
getDayDifference
public static int getDayDifference(java.util.Calendar calendar1, java.util.Calendar calendar2)
Determines the difference between the two calendars. This is equivalent conceptually tocalendar1Days - 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.
-
-