Package de.aristaflow.adept2.util.time
Class CalendarTools
java.lang.Object
de.aristaflow.adept2.util.time.CalendarTools
CalendarTools provides methods useful for determining
public holidays and calculating dates.- Author:
- Ulrich Kreher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a day of an unspecific year, that is just a month and a day. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CalendarAdds (or subtracts) the designated amount of days to the designated day.static CalendarcreateUtcCalendar(boolean setMidnight) Returns a newCalendarwith UTC as its time zone, the current date and either the current time or midnight.static booleanCompares two calendars and returns whether they are equal with respect to the designated field.static CalendargetEasterSunday(Calendar year) Returns the day of the easter sunday in the designated year, determined by the algorithm in Butchers "Ecclesiastical Calendar".static longsetDateInMillis(long time, int year, int month, int day) Sets the designated year, month and day in the designated time (of long) and returns the new date/time.static longsetDateInMillis(long time, int year, int month, int day, Locale locale) Sets the designated year, month and day in the designated time (of long) and returns the new date/time.static longsetTimeInMillis(long date, int hours, int minutes, int seconds) Sets the designated hours, minutes and seconds in the designated date (of long) and returns the new date/time.static longsetTimeInMillis(long date, int hours, int minutes, int seconds, Locale locale) Sets the designated hours, minutes and seconds in the designated date (of long) and returns the new date/time.static CalendarweekdayRelativeTo(int weekday, Calendar day, boolean before) Gets the day which represents the first weekday relative the designated day, for instance, a wednesday before a specific date.
-
Constructor Details
-
CalendarTools
public CalendarTools()
-
-
Method Details
-
equals
Compares two calendars and returns whether they are equal with respect to the designated field. Allowed fields are:Calendar.YEAR,Calendar.MONTH,Calendar.WEEK_OF_YEAR,Calendar.DATE,Calendar.HOUR_OF_DAY,Calendar.MINUTE,Calendar.SECONDandCalendar.MILLISECOND. Smaller units include the bigger units, for instance when respecting minutes, day (date), month, year and era are also considered while seconds (and milliseconds) are ignored. No other information is considered, for instance time zones and calendar style.
Due to the consistent values of the constants declared inCalendar, one can also use units between the ones listed above. For instance, specifyingCalendar.DAY_OF_WEEKyields the same result asCalendar.DATE.- Parameters:
c1- The calendars to compare with.c2- The other calendar to compare.field- The field which is the least unit to be considered in the comparison.- Returns:
- Whether the two designated calendars are equal with respect to at least the designated field (unit).
-
addDays
Adds (or subtracts) the designated amount of days to the designated day.- Parameters:
day- The day to add the designated amount of days to.daysToAdd- The number of days to add (or subtract) to (or from) the designated day.- Returns:
- The resulting day as calendar.
-
weekdayRelativeTo
Gets the day which represents the first weekday relative the designated day, for instance, a wednesday before a specific date. The returned day is before or after the designated day but not the day itself in case it is the same weekday.- Parameters:
weekday- Theday of weekof the desired day.day- The day determining the desired day.before- Whether the desired day is before the designated day.- Returns:
- The day being the designated week day before or after the designated day.
-
getEasterSunday
Returns the day of the easter sunday in the designated year, determined by the algorithm in Butchers "Ecclesiastical Calendar". Easter sunday is a major reference date for floating holidays in many calendar systems. An UTC calendar will be used, setting the time to one minute after midnight.- Parameters:
year- The year of which to retrieve the easter sunday.- Returns:
- The day being the easter sunday in the designated year as UTC calendar with a time of one minute after midnight.
-
setDateInMillis
public static long setDateInMillis(long time, int year, int month, int day) Sets the designated year, month and day in the designated time (of long) and returns the new date/time. Iftimecontains time information (hours, minutes, seconds, milliseconds), this will be retained. If you want to set a completely new date, use 0 fortime.
For adapting the date, the calendar for the default locale will be used.- Parameters:
time- The time for which to set the year, month and day.year- The year which to set.month- The month which to set (0-based, that is 0-11 are valid months).day- The day which to set.- Returns:
- The date as long having the designated year, month and day and the time.
-
setDateInMillis
Sets the designated year, month and day in the designated time (of long) and returns the new date/time. Iftimecontains time information (hours, minutes, seconds, milliseconds), this will be retained. If you want to set a completely new date, use 0 fortime.
The designated locale will be used for the calendar for adapting the date.- Parameters:
time- The time for which to set the year, month and day.year- The year which to set.month- The month which to set (0-based, that is 0-11 are valid months).day- The day which to set.locale- The locale for the calendar to adapt the date.- Returns:
- The date as long having the designated year, month and day and the time.
-
setTimeInMillis
public static long setTimeInMillis(long date, int hours, int minutes, int seconds) Sets the designated hours, minutes and seconds in the designated date (of long) and returns the new date/time. Ifdatecontains date information (year, month, day), this will be retained. If you want to set a completely new time, use 0 fordate.
For adapting the time, the calendar for the default locale will be used.- Parameters:
date- The time for which to set the hours, minutes and seconds.hours- The hours which to set (0-based, 24-hour clock, 0-23 are valid hours).minutes- The minutes which to set (0-based, that is 0-59 are valid minutes).seconds- The minutes which to set (0-based, that is 0-59 are valid seconds).- Returns:
- The time as long having the designated hours, minutes and seconds and the date.
-
setTimeInMillis
Sets the designated hours, minutes and seconds in the designated date (of long) and returns the new date/time. Ifdatecontains date information (year, month, day), this will be retained. If you want to set a completely new time, use 0 fordate.
The designated locale will be used for the calendar for adapting the time.- Parameters:
date- The time for which to set the hours, minutes and seconds.hours- The hours which to set (0-based, 24-hour clock, 0-23 are valid hours).minutes- The minutes which to set (0-based, that is 0-59 are valid minutes).seconds- The minutes which to set (0-based, that is 0-59 are valid seconds).locale- The locale for the calendar to adapt the time.- Returns:
- The time as long having the designated hours, minutes and seconds and the date.
-
createUtcCalendar
Returns a newCalendarwith UTC as its time zone, the current date and either the current time or midnight.- Parameters:
setMidnight- Whether to set the time to midnight of the current date.- Returns:
- a new
Calendarwith UTC as its time zone, the current date and either the current time or midnight.
-