Class CalendarUtil
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longUseful constant of eight hours (in milliseconds)static final longUseful constant of five minutes (in milliseconds)static final longUseful constant of four hours (in milliseconds)static final longUseful constant of 1 day (in milliseconds)static final longUseful constant of one hour (in milliseconds)static final longUseful constant of 1 minute (in milliseconds)static final longUseful constant of 1 month (in milliseconds) [30 day month]static final longUseful constant of 1 second (in milliseconds)static final longUseful constant of 1 week (in milliseconds)static final longUseful constant of one year (in milliseconds) [365 days]static final longUseful constant of 6 months (in milliseconds) [30 day month]static final longUseful constant of ten minutes (in milliseconds)static final longUseful constant of thirty minutes (in milliseconds)static final longUseful constant of 3 days (in milliseconds)static final longUseful constant of 3 months (in milliseconds)[30 day month]static final longUseful constant of twelve hours (in milliseconds)static final longUseful constant of twenty four hours (in milliseconds)static final longUseful constant of two hours (in milliseconds)static final longUseful constant of 2 minutes (in milliseconds)static final longUseful constant of 2 months (in milliseconds) [30 day month]static final longUseful constant of 2 weeks (in milliseconds) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DateReturns the date representing the addition/subtraction of a number of hours from the specified starting timestamp.static longdaysToMillis(int days) returns the number of millis in the specified number of daysstatic DateReturns the date representing the beginning of the current day (at 12:00:00.000 AM), starting from the current time and the specified calendar.static DateReturns the closest hour, starting from the current time and the specified calendar.static DategetBeginOfDay(Calendar cal, Date d) Returns the date representing the beginning of the specified day (at 12:00:00.000 AM), starting from the current time and the specified calendar.static DategetBeginOfHour(Calendar cal, Date d) Returns the closest hour, starting from the specified time and the specified calendar.static DateReturns the date representing the end of the current day (11:59:59.999 PM), starting from the current time and the specified calendar.static DateReturns the end of the hour, starting from the current time and the specified calendar.static DategetEndOfDay(Calendar cal, Date d) Returns the date representing the end of the specified day (11:59:59.999 PM), starting from the current time and the specified calendar.static DategetEndOfHour(Calendar cal, Date d) Returns the end of the hour, starting from the specified time and the specified calendar.static DateReturns the first day of the current month with the default locale;static DateReturns the date representing first day of the week (at 12:00:00.000 AM), starting from the current time and the specified calendar.static DategetFirstDayOfMonth(Calendar cal, Date d) Returns the first day of the specified month with the default locale;static DategetFirstDayOfWeek(Calendar cal, Date d) Returns the date representing the first day of the week (at 12:00:00.000 AM), using the specified date as the starting point of the week under consideration and the specified calendar.static DateReturns the last day of the current month with the default locale;static DateReturns the date representing the last day of the week (11:59:59.999 PM), starting from the current time and the specified calendar.static DategetLastDayOfMonth(Calendar cal, Date d) Returns the last day of the specified month with the default locale;static DategetLastDayOfWeek(Calendar cal, Date d) Returns the date representing the last day of the week (at 11:59:59.999 PM) using the specified date as the starting point of the week under consideration and the specified calendar.static ListgetLastNMonths(Calendar cal, int nummonths) Returns {#getLastNMonths} with the default locale;static ListgetLastNMonths(Calendar cal, Date startingDate, int nummonths, Locale locale) Returns a List of Date[][], with each item being a month.static Timestampreturns now as java.sql.Timestampstatic inthoursBetween(Date begin, Date end) returns number of hours between begin and end timestamps.static longhoursToMillis(int hours) returns the number of millis in the specified number of hoursstatic voidstatic longminutesToMillis(int minutes) returns the number of millis in the specified hoursstatic Timestampnow()returns now as java.sql.Timestampstatic final voidSets the month in the specified calendar based on the specified 0-based month number.static booleanwithinLastNDays(long time, int n_days) returns true if the specified time is within the specified days, starting from the current time.static booleanwithinLastNDays(Date time, int n_days) returns true if the specified time is within the specified days, starting from the current time.static booleanwithinLastNHours(long time, int n_hours) returns true if the specified time is within the specified hours, starting from the current time.static booleanwithinLastNHours(Date time, int n_hours) returns true if the specified time is within the specified hours, starting from the current time.static booleanwithinLastNMin(Date time, int n_min) returns true if the specified time is within the specified min, starting from the current time.static booleanwithinLastNSeconds(Date time, int n_seconds) returns true if the specified time is within the specified seconds, starting from the current time.
-
Field Details
-
ONE_SEC
-
ONE_MIN
-
TWO_MIN
-
FIVE_MIN
-
TEN_MIN
-
THIRTY_MIN
-
ONE_HOUR
-
TWO_HOUR
-
FOUR_HOUR
-
EIGHT_HOUR
-
TWELVE_HOUR
-
TWENTY_FOUR_HOUR
Useful constant of twenty four hours (in milliseconds)- See Also:
-
ONE_DAY
-
THREE_DAY
-
ONE_WEEK
-
TWO_WEEK
-
ONE_MONTH
Useful constant of 1 month (in milliseconds) [30 day month]- See Also:
-
TWO_MONTH
Useful constant of 2 months (in milliseconds) [30 day month]- See Also:
-
THREE_MONTH
Useful constant of 3 months (in milliseconds)[30 day month]- See Also:
-
SIX_MONTH
Useful constant of 6 months (in milliseconds) [30 day month]- See Also:
-
ONE_YEAR
Useful constant of one year (in milliseconds) [365 days]- See Also:
-
-
Constructor Details
-
CalendarUtil
public CalendarUtil()
-
-
Method Details
-
setMonth
Sets the month in the specified calendar based on the specified 0-based month number. There is no direct setMonth method in Calendar. Therefore, this method is essentially a giant switch statement, like:case 0: cal.set(Calendar.MONTH, Calendar.JANUARY); break; case 1: cal.set(Calendar.MONTH, Calendar.FEBRUARY); break; ... etc... -
addHours
Returns the date representing the addition/subtraction of a number of hours from the specified starting timestamp. Specify a positive number for future/adding hours or negative for past/subtracing hours.The state of the calendar is not affected by the calculations performed by this method.
-
getBeginOfHour
Returns the closest hour, starting from the specified time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.
-
getBeginOfCurrentHour
Returns the closest hour, starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.
-
getEndOfHour
Returns the end of the hour, starting from the specified time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.
-
getEndOfCurrentHour
Returns the end of the hour, starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.
-
getBeginOfDay
Returns the date representing the beginning of the specified day (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getBeginOfCurrentDay
Returns the date representing the beginning of the current day (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getEndOfDay
Returns the date representing the end of the specified day (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getEndOfCurrentDay
Returns the date representing the end of the current day (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getFirstDayOfWeek
Returns the date representing the first day of the week (at 12:00:00.000 AM), using the specified date as the starting point of the week under consideration and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.The first day of week can vary based on the locale associated with the specified calendar (sunday in us, monday in fr, etc).
-
getFirstDayOfCurrentWeek
Returns the date representing first day of the week (at 12:00:00.000 AM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getLastDayOfWeek
Returns the date representing the last day of the week (at 11:59:59.999 PM) using the specified date as the starting point of the week under consideration and the specified calendar. The state of the calendar is not affected by the calculations performed by this method.The last day of week can vary based on the locale associated with the specified calendar (saturday in us, sunday in fr, etc).
-
getLastDayOfCurrentWeek
Returns the date representing the last day of the week (11:59:59.999 PM), starting from the current time and the specified calendar. The state of the calendar is not affected by the calculations performed by this method. -
getFirstDayOfMonth
Returns the first day of the specified month with the default locale; -
getFirstDayOfCurrentMonth
Returns the first day of the current month with the default locale; -
getLastDayOfMonth
Returns the last day of the specified month with the default locale; -
getLastDayOfCurrentMonth
Returns the last day of the current month with the default locale; -
getLastNMonths
Returns {#getLastNMonths} with the default locale; -
getLastNMonths
Returns a List of Date[][], with each item being a month. The nummonths is the number of months to return and the startingDate is the starting month, to count backwards from.In the returned list, date[0] represents the first day, 12:00:00.000 AM and date[1] representing the last day, 11:59:59.999 PM. The starting item in the list is the nearest month and each successive item is the prior month.
For example: To get last 6 months, say: getLastNMonths(cal, today, 6) where cal is a calendar to use and today is today's date.
-
withinLastNDays
returns true if the specified time is within the specified days, starting from the current time. -
withinLastNDays
returns true if the specified time is within the specified days, starting from the current time. -
withinLastNHours
returns true if the specified time is within the specified hours, starting from the current time. -
withinLastNHours
returns true if the specified time is within the specified hours, starting from the current time. -
withinLastNMin
returns true if the specified time is within the specified min, starting from the current time. -
withinLastNSeconds
returns true if the specified time is within the specified seconds, starting from the current time. -
daysToMillis
returns the number of millis in the specified number of days -
hoursToMillis
returns the number of millis in the specified number of hours -
minutesToMillis
returns the number of millis in the specified hours -
hoursBetween
returns number of hours between begin and end timestamps. the order of begin/end is irrelevant, uses absolute value of difference between the two for the calculation. -
getNowTimestamp
returns now as java.sql.Timestamp -
now
-
main
-