public final class CalendarUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
CalendarUtil.Duration |
Modifier and Type | Field and Description |
---|---|
static long |
EIGHT_HOUR
Useful constant of eight hours (in milliseconds)
|
static long |
FIVE_MIN
Useful constant of five minutes (in milliseconds)
|
static long |
FOUR_HOUR
Useful constant of four hours (in milliseconds)
|
static long |
ONE_DAY
Useful constant of 1 day (in milliseconds)
|
static long |
ONE_HOUR
Useful constant of one hour (in milliseconds)
|
static long |
ONE_MIN
Useful constant of 1 minute (in milliseconds)
|
static long |
ONE_MONTH
Useful constant of 1 month (in milliseconds) [30 day month]
|
static long |
ONE_SEC
Useful constant of 1 second (in milliseconds)
|
static long |
ONE_WEEK
Useful constant of 1 week (in milliseconds)
|
static long |
ONE_YEAR
Useful constant of one year (in milliseconds) [365 days]
|
static long |
SIX_MONTH
Useful constant of 6 months (in milliseconds) [30 day month]
|
static long |
TEN_MIN
Useful constant of ten minutes (in milliseconds)
|
static long |
THIRTY_MIN
Useful constant of thirty minutes (in milliseconds)
|
static long |
THREE_DAY
Useful constant of 3 days (in milliseconds)
|
static long |
THREE_MONTH
Useful constant of 3 months (in milliseconds)[30 day month]
|
static long |
TWELVE_HOUR
Useful constant of twelve hours (in milliseconds)
|
static long |
TWENTY_FOUR_HOUR
Useful constant of twenty four hours (in milliseconds)
|
static long |
TWO_HOUR
Useful constant of two hours (in milliseconds)
|
static long |
TWO_MIN
Useful constant of 2 minutes (in milliseconds)
|
static long |
TWO_MONTH
Useful constant of 2 months (in milliseconds) [30 day month]
|
static long |
TWO_WEEK
Useful constant of 2 weeks (in milliseconds)
|
Constructor and Description |
---|
CalendarUtil() |
Modifier and Type | Method and Description |
---|---|
static Date |
addHours(Calendar cal,
Date d,
int hours)
Returns the date representing the addition/subtraction of a number of hours from the specified
starting timestamp.
|
static long |
daysToMillis(int days)
returns the number of millis in the specified number of days
|
static Date |
getBeginOfCurrentDay(Calendar cal)
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.
|
static Date |
getBeginOfCurrentHour(Calendar cal)
Returns the closest hour, starting from the current time and the specified calendar.
|
static Date |
getBeginOfDay(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 Date |
getBeginOfHour(Calendar cal,
Date d)
Returns the closest hour, starting from the specified time and the specified calendar.
|
static Date |
getEndOfCurrentDay(Calendar cal)
Returns the date representing the end of the current day (11:59:59.999 PM),
starting from the current time and the specified calendar.
|
static Date |
getEndOfCurrentHour(Calendar cal)
Returns the end of the hour, starting from the current time and the specified calendar.
|
static Date |
getEndOfDay(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 Date |
getEndOfHour(Calendar cal,
Date d)
Returns the end of the hour, starting from the specified time and the specified calendar.
|
static Date |
getFirstDayOfCurrentMonth(Calendar cal)
Returns the first day of the current month with the default locale;
|
static Date |
getFirstDayOfCurrentWeek(Calendar cal)
Returns the date representing first day of the week (at 12:00:00.000 AM), starting
from the current time and the specified calendar.
|
static Date |
getFirstDayOfMonth(Calendar cal,
Date d)
Returns the first day of the specified month with the default locale;
|
static Date |
getFirstDayOfWeek(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 Date |
getLastDayOfCurrentMonth(Calendar cal)
Returns the last day of the current month with the default locale;
|
static Date |
getLastDayOfCurrentWeek(Calendar cal)
Returns the date representing the last day of the week (11:59:59.999 PM), starting from the current
time and the specified calendar.
|
static Date |
getLastDayOfMonth(Calendar cal,
Date d)
Returns the last day of the specified month with the default locale;
|
static Date |
getLastDayOfWeek(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 List |
getLastNMonths(Calendar cal,
Date startingDate,
int nummonths,
Locale locale)
Returns a List of Date[][], with each item being a month.
|
static List |
getLastNMonths(Calendar cal,
int nummonths)
Returns {#getLastNMonths} with the default locale;
|
static Timestamp |
getNowTimestamp()
returns now as java.sql.Timestamp
|
static int |
hoursBetween(Date begin,
Date end)
returns number of hours between begin and end timestamps.
|
static long |
hoursToMillis(int hours)
returns the number of millis in the specified number of hours
|
static void |
main(String[] args) |
static long |
minutesToMillis(int minutes)
returns the number of millis in the specified hours
|
static Timestamp |
now()
returns now as java.sql.Timestamp
|
static void |
setMonth(Calendar cal,
int monthNum)
Sets the month in the specified calendar based on the specified 0-based
month number.
|
static boolean |
withinLastNDays(Date time,
int n_days)
returns true if the specified time is within the specified days, starting from
the current time.
|
static boolean |
withinLastNDays(long time,
int n_days)
returns true if the specified time is within the specified days, starting from
the current time.
|
static boolean |
withinLastNHours(Date time,
int n_hours)
returns true if the specified time is within the specified hours, starting from
the current time.
|
static boolean |
withinLastNHours(long time,
int n_hours)
returns true if the specified time is within the specified hours, starting from
the current time.
|
static boolean |
withinLastNMin(Date time,
int n_min)
returns true if the specified time is within the specified min, starting from
the current time.
|
static boolean |
withinLastNSeconds(Date time,
int n_seconds)
returns true if the specified time is within the specified seconds, starting from
the current time.
|
public static final long ONE_SEC
public static final long ONE_MIN
public static final long TWO_MIN
public static final long FIVE_MIN
public static final long TEN_MIN
public static final long THIRTY_MIN
public static final long ONE_HOUR
public static final long TWO_HOUR
public static final long FOUR_HOUR
public static final long EIGHT_HOUR
public static final long TWELVE_HOUR
public static final long TWENTY_FOUR_HOUR
public static final long ONE_DAY
public static final long THREE_DAY
public static final long ONE_WEEK
public static final long TWO_WEEK
public static final long ONE_MONTH
public static final long TWO_MONTH
public static final long THREE_MONTH
public static final long SIX_MONTH
public static final long ONE_YEAR
public CalendarUtil()
public static final void setMonth(Calendar cal, int monthNum)
case 0: cal.set(Calendar.MONTH, Calendar.JANUARY); break; case 1: cal.set(Calendar.MONTH, Calendar.FEBRUARY); break; ... etc...
public static Date addHours(Calendar cal, Date d, int hours)
The state of the calendar is not affected by the calculations performed by this method.
public static Date getBeginOfHour(Calendar cal, Date d)
For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.
public static Date getBeginOfCurrentHour(Calendar cal)
For example, any date with time component 12.30pm returns the same date with a time component 12.00pm.
public static Date getEndOfHour(Calendar cal, Date d)
For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.
public static Date getEndOfCurrentHour(Calendar cal)
For example, any date with time component 12.30pm returns the same date with a time component 1.00pm.
public static Date getBeginOfDay(Calendar cal, Date d)
public static Date getBeginOfCurrentDay(Calendar cal)
public static Date getEndOfDay(Calendar cal, Date d)
public static Date getEndOfCurrentDay(Calendar cal)
public static Date getFirstDayOfWeek(Calendar cal, Date d)
The first day of week can vary based on the locale associated with the specified calendar (sunday in us, monday in fr, etc).
public static Date getFirstDayOfCurrentWeek(Calendar cal)
public static Date getLastDayOfWeek(Calendar cal, Date d)
The last day of week can vary based on the locale associated with the specified calendar (saturday in us, sunday in fr, etc).
public static Date getLastDayOfCurrentWeek(Calendar cal)
public static Date getFirstDayOfMonth(Calendar cal, Date d)
public static Date getFirstDayOfCurrentMonth(Calendar cal)
public static Date getLastDayOfMonth(Calendar cal, Date d)
public static Date getLastDayOfCurrentMonth(Calendar cal)
public static List getLastNMonths(Calendar cal, int nummonths)
public static List getLastNMonths(Calendar cal, Date startingDate, int nummonths, Locale locale)
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.
public static boolean withinLastNDays(long time, int n_days)
public static boolean withinLastNDays(Date time, int n_days)
public static boolean withinLastNHours(long time, int n_hours)
public static boolean withinLastNHours(Date time, int n_hours)
public static boolean withinLastNMin(Date time, int n_min)
public static boolean withinLastNSeconds(Date time, int n_seconds)
public static long daysToMillis(int days)
public static long hoursToMillis(int hours)
public static long minutesToMillis(int minutes)
public static int hoursBetween(Date begin, Date end)
public static Timestamp getNowTimestamp()