Package de.aristaflow.adept2.util.time
Class CalendarTools
- java.lang.Object
-
- de.aristaflow.adept2.util.time.CalendarTools
-
public class CalendarTools extends Object
CalendarTools
provides methods useful for determining public holidays and calculating dates.- Author:
- Ulrich Kreher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CalendarTools.DayOfYear
Represents a day of an unspecific year, that is just a month and a day.
-
Constructor Summary
Constructors Constructor Description CalendarTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Calendar
addDays(Calendar day, int daysToAdd)
Adds (or subtracts) the designated amount of days to the designated day.static Calendar
createUtcCalendar(boolean setMidnight)
Returns a newCalendar
with UTC as its time zone, the current date and either the current time or midnight.static boolean
equals(Calendar c1, Calendar c2, int field)
Compares two calendars and returns whether they are equal with respect to the designated field.static Calendar
getEasterSunday(Calendar year)
Returns the day of the easter sunday in the designated year, determined by the algorithm in Butchers "Ecclesiastical Calendar".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.static long
setDateInMillis(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 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.static long
setTimeInMillis(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 Calendar
weekdayRelativeTo(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.
-
-
-
Method Detail
-
equals
public static boolean equals(Calendar c1, Calendar c2, int field)
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.SECOND
andCalendar.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_WEEK
yields 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
public static Calendar addDays(Calendar day, int daysToAdd)
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
public static Calendar weekdayRelativeTo(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. 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 week
of 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
public static Calendar getEasterSunday(Calendar year)
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. Iftime
contains 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
public static long setDateInMillis(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. Iftime
contains 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. Ifdate
contains 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
public static long setTimeInMillis(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. Ifdate
contains 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
public static Calendar createUtcCalendar(boolean setMidnight)
Returns a newCalendar
with 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
Calendar
with UTC as its time zone, the current date and either the current time or midnight.
-
-