Package org.jdesktop.swingx.calendar
Class CalendarUtils
java.lang.Object
org.jdesktop.swingx.calendar.CalendarUtils
Calendar manipulation.
PENDING: replace by something tested - as is pasted dateUtils
to work on a calendar instead of using long
- Author:
- Jeanette Winzenburg
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Increments the calendar field of the given calendar by amount.static boolean
Checks the given dates for being equal.static void
Adjust the given calendar to the last millisecond of the specified date.static Date
Adjust the given calendar to the last millisecond of the given date.static void
endOfMonth
(Calendar calendar) Adjusts the calendar to the end of the current month.static void
Adjusts the calendar to the end of the current week.static Date
Adjusts the calendar to the end of the current week.static int
Gets the calendar field of the given calendar by amount.static Date
getEndOfDST
(Calendar calendar) Adjusts the Calendar to the end of the day of the last day in DST in the current year or unchanged if not using DST.static Date
getStartOfDST
(Calendar calendar) Adjusts the Calendar to the end of the day of the first day in DST in the current year or unchanged if not using DST.static boolean
isEndOfDay
(Calendar calendar) Returns a boolean indicating if the given calendar represents the end of a day (in the calendar's time zone).static boolean
isEndOfMonth
(Calendar calendar) Returns a boolean indicating if the given calendar represents the end of a month (in the calendar's time zone).static boolean
isEndOfWeek
(Calendar calendar) Returns a boolean indicating if the given calendar represents the end of a week (in the calendar's time zone).static boolean
Returns a boolean to indicate whether the given calendar is flushed.static boolean
Returns a boolean indicating whether the given Date is in the same period as the Date in the calendar, as defined by the calendar field.static boolean
Returns a boolean indicating whether the given Date is the same day as the day in the calendar.static boolean
Returns a boolean indicating if the calendar is set to the start of a period as defined by the given field.static boolean
isStartOfDay
(Calendar calendar) Returns a boolean indicating if the given calendar represents the start of a day (in the calendar's time zone).static boolean
isStartOfDecade
(Calendar calendar) Returns a boolean indicating if the given calendar represents the start of a decade (in the calendar's time zone).static boolean
isStartOfMonth
(Calendar calendar) Returns a boolean indicating if the given calendar represents the start of a month (in the calendar's time zone).static boolean
isStartOfWeek
(Calendar calendar) Returns a boolean indicating if the given calendar represents the start of a month (in the calendar's time zone).static boolean
isStartOfYear
(Calendar calendar) Returns a boolean indicating if the given calendar represents the start of a year (in the calendar's time zone).static void
Sets the calendar field of the given calendar by amount.static void
Adjusts the given calendar to the start of the period as indicated by the given field.static void
startOfDay
(Calendar calendar) Adjust the given calendar to the first millisecond of the current day.static Date
startOfDay
(Calendar calendar, Date date) Adjust the given calendar to the first millisecond of the given date.static void
startOfDecade
(Calendar calendar) Adjusts the given Calendar to the start of the decade.static Date
startOfDecade
(Calendar calendar, Date date) Adjusts the given Calendar to the start of the decade as defined by the given date.static void
startOfMonth
(Calendar calendar) Adjusts the calendar to the start of the current month.static void
startOfWeek
(Calendar calendar) Adjusts the calendar to the start of the current week.static Date
startOfWeek
(Calendar calendar, Date date) Adjusts the calendar to the start of the current week.static void
startOfYear
(Calendar calendar) Adjusts the given Calendar to the start of the year.static Date
startOfYear
(Calendar calendar, Date date) Adjusts the given Calendar to the start of the year as defined by the given date.
-
Field Details
-
ONE_MINUTE
public static final int ONE_MINUTE- See Also:
-
ONE_HOUR
public static final int ONE_HOUR- See Also:
-
THREE_HOURS
public static final int THREE_HOURS- See Also:
-
ONE_DAY
public static final int ONE_DAY- See Also:
-
DECADE
public static final int DECADE- See Also:
-
YEAR_IN_DECADE
public static final int YEAR_IN_DECADE- See Also:
-
-
Constructor Details
-
CalendarUtils
public CalendarUtils()
-
-
Method Details
-
add
Increments the calendar field of the given calendar by amount.- Parameters:
calendar
- Calendarfield
- the field to increment, allowed are all fields known to Calendar plus DECADE.amount
- to increment- Throws:
IllegalArgumentException
- for unsupported field
-
get
Gets the calendar field of the given calendar by amount.- Parameters:
calendar
- Calendarfield
- the field to get, allowed are all fields known to Calendar plus DECADE.- Returns:
- calendar field
- Throws:
IllegalArgumentException
- for unsupported field
-
set
Sets the calendar field of the given calendar by amount.NOTE: the custom field implementations are very naive (JSR-310 will do better) - for decade: value must be positive, value must be a multiple of 10 and is interpreted as the first-year-of-the-decade - for year-in-decade: value is added/substracted to/from the start-of-decade of the date of the given calendar
- Parameters:
calendar
- Calendarfield
- the field to increment, allowed are all fields known to Calendar plus DECADE.value
- the decade to set, must be a- Throws:
IllegalArgumentException
- if the field is unsupported or the value is not dividable by 10 or negative.
-
getEndOfDST
Adjusts the Calendar to the end of the day of the last day in DST in the current year or unchanged if not using DST. Returns the calendar's date or null, if not using DST.- Parameters:
calendar
- the calendar to adjust- Returns:
- the end of day of the last day in DST, or null if not using DST.
-
getStartOfDST
Adjusts the Calendar to the end of the day of the first day in DST in the current year or unchanged if not using DST. Returns the calendar's date or null, if not using DST.Note: the start of the day of the first day in DST is ill-defined!
- Parameters:
calendar
- the calendar to adjust- Returns:
- the start of day of the first day in DST, or null if not using DST.
-
isStartOfDay
Returns a boolean indicating if the given calendar represents the start of a day (in the calendar's time zone). The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the start of the day, false otherwise.
-
isEndOfDay
Returns a boolean indicating if the given calendar represents the end of a day (in the calendar's time zone). The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the end of the day, false otherwise.
-
isStartOfMonth
Returns a boolean indicating if the given calendar represents the start of a month (in the calendar's time zone). Returns true, if the time is the start of the first day of the month, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the start of the first day of the month, false otherwise.
-
isEndOfMonth
Returns a boolean indicating if the given calendar represents the end of a month (in the calendar's time zone). Returns true, if the time is the end of the last day of the month, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the end of the last day of the month, false otherwise.
-
isStartOfWeek
Returns a boolean indicating if the given calendar represents the start of a month (in the calendar's time zone). Returns true, if the time is the start of the first day of the month, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the start of the first day of the month, false otherwise.
-
isEndOfWeek
Returns a boolean indicating if the given calendar represents the end of a week (in the calendar's time zone). Returns true, if the time is the end of the last day of the week, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the end of the last day of the week, false otherwise.
-
startOfWeek
Adjusts the calendar to the start of the current week. That is, first day of the week with all time fields cleared.- Parameters:
calendar
- the calendar to adjust.
-
endOfWeek
Adjusts the calendar to the end of the current week. That is, last day of the week with all time fields at max.- Parameters:
calendar
- the calendar to adjust.
-
endOfWeek
Adjusts the calendar to the end of the current week. That is, last day of the week with all time fields at max. The Date of the adjusted Calendar is returned.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the end of the week of the given date
-
startOfWeek
Adjusts the calendar to the start of the current week. That is, last day of the week with all time fields at max. The Date of the adjusted Calendar is returned.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the start of the week of the given date
-
startOfDecade
Adjusts the given Calendar to the start of the decade.- Parameters:
calendar
- the calendar to adjust.
-
startOfDecade
Adjusts the given Calendar to the start of the decade as defined by the given date. Returns the calendar's Date.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the start of the decade of the given date
-
isStartOfDecade
Returns a boolean indicating if the given calendar represents the start of a decade (in the calendar's time zone). Returns true, if the time is the start of the first day of the decade, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the start of the first day of the month, false otherwise.
-
startOfYear
Adjusts the given Calendar to the start of the year.- Parameters:
calendar
- the calendar to adjust.
-
startOfYear
Adjusts the given Calendar to the start of the year as defined by the given date. Returns the calendar's Date.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the start of the year of the given date
-
isStartOfYear
Returns a boolean indicating if the given calendar represents the start of a year (in the calendar's time zone). Returns true, if the time is the start of the first day of the year, false otherwise. The calendar is unchanged.- Parameters:
calendar
- the calendar to check.- Returns:
- true if the calendar's time is the start of the first day of the month, false otherwise.
-
startOfMonth
Adjusts the calendar to the start of the current month. That is, first day of the month with all time fields cleared.- Parameters:
calendar
- calendar to adjust.
-
endOfMonth
Adjusts the calendar to the end of the current month. That is the last day of the month with all time-fields at max.- Parameters:
calendar
- calendar to adjust.
-
startOfDay
Adjust the given calendar to the first millisecond of the given date. that is all time fields cleared. The Date of the adjusted Calendar is returned.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the start of the day of the given date
-
endOfDay
Adjust the given calendar to the last millisecond of the given date. that is all time fields cleared. The Date of the adjusted Calendar is returned.- Parameters:
calendar
- calendar to adjust.date
- the Date to use.- Returns:
- the end of the day of the given date
-
startOfDay
Adjust the given calendar to the first millisecond of the current day. that is all time fields cleared.- Parameters:
calendar
- calendar to adjust.
-
endOfDay
Adjust the given calendar to the last millisecond of the specified date.- Parameters:
calendar
- calendar to adjust.
-
startOf
Adjusts the given calendar to the start of the period as indicated by the given field. This delegates to startOfDay, -Week, -Month, -Year as appropriate.- Parameters:
calendar
- Calendarfield
- the period to adjust, allowed are Calendar.DAY_OF_MONTH, -.MONTH, -.WEEK and YEAR and CalendarUtils.DECADE.
-
isStartOf
Returns a boolean indicating if the calendar is set to the start of a period as defined by the given field. This delegates to startOfDay, -Week, -Month, -Year as appropriate. The calendar is unchanged.- Parameters:
calendar
- Calendarfield
- the period to adjust, allowed are Calendar.DAY_OF_MONTH, -.MONTH, -.WEEK and YEAR and CalendarUtils.DECADE.- Returns:
- calendar is set to the start of a period
- Throws:
IllegalArgumentException
- if the field is not supported.
-
areEqual
Checks the given dates for being equal.- Parameters:
current
- one of the dates to comparedate
- the otherr of the dates to compare- Returns:
- true if the two given dates both are null or both are not null and equal, false otherwise.
-
isSameDay
Returns a boolean indicating whether the given Date is the same day as the day in the calendar. Calendar and date are unchanged by the check.- Parameters:
today
- the Calendar representing a date, must not be null.now
- the date to compare to, must not be null- Returns:
- true if the calendar and date represent the same day in the given calendar.
-
isSame
Returns a boolean indicating whether the given Date is in the same period as the Date in the calendar, as defined by the calendar field. Calendar and date are unchanged by the check.- Parameters:
today
- the Calendar representing a date, must not be null.now
- the date to compare to, must not be null.field
- the period to adjust, allowed are Calendar.DAY_OF_MONTH, -.MONTH,-.WEEK and YEAR and CalendarUtils.DECADE.- Returns:
- true if the calendar and date represent the same day in the given calendar.
-
isFlushed
Returns a boolean to indicate whether the given calendar is flushed.The only way to guarantee a flushed state is to let client code call getTime or getTimeInMillis.
Note: this is for testing only and not entirely safe!
- Parameters:
calendar
- Calendar- Returns:
- calendar is flushed
-