public class DateUtils
extends java.lang.Object
A suite of utilities surrounding the use of the
Calendar
and Date
object.
DateUtils contains a lot of common methods considering manipulations of Dates or Calendars. Some methods require some extra explanation. The truncate, ceiling and round methods could be considered the Math.floor(), Math.ceil() or Math.round versions for dates This way date-fields will be ignored in bottom-up order. As a complement to these methods we've introduced some fragment-methods. With these methods the Date-fields will be ignored in top-down order. Since a date without a year is not a valid date, you have to decide in what kind of date-field you want your result, for instance milliseconds or days.
Several methods are provided for adding to Date
objects, of the form
addXXX(Date date, int amount)
. It is important to note these methods
use a Calendar
internally (with default timezone and locale) and may
be affected by changes to daylight saving time (DST).
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_PER_DAY
Number of milliseconds in a standard day.
|
static long |
MILLIS_PER_HOUR
Number of milliseconds in a standard hour.
|
static long |
MILLIS_PER_MINUTE
Number of milliseconds in a standard minute.
|
static long |
MILLIS_PER_SECOND
Number of milliseconds in a standard second.
|
static int |
RANGE_MONTH_MONDAY
A month range, the week starting on Monday.
|
static int |
RANGE_MONTH_SUNDAY
A month range, the week starting on Sunday.
|
static int |
RANGE_WEEK_CENTER
A week range, centered around the day focused.
|
static int |
RANGE_WEEK_MONDAY
A week range, starting on Monday.
|
static int |
RANGE_WEEK_RELATIVE
A week range, starting on the day focused.
|
static int |
RANGE_WEEK_SUNDAY
A week range, starting on Sunday.
|
static int |
SEMI_MONTH
This is half a month, so this represents whether a date is in the top
or bottom half of the month.
|
Constructor and Description |
---|
DateUtils()
DateUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static java.util.Date |
addDays(java.util.Date date,
int amount)
Adds a number of days to a date returning a new object.
|
static java.util.Date |
addHours(java.util.Date date,
int amount)
Adds a number of hours to a date returning a new object.
|
static java.util.Date |
addMilliseconds(java.util.Date date,
int amount)
Adds a number of milliseconds to a date returning a new object.
|
static java.util.Date |
addMinutes(java.util.Date date,
int amount)
Adds a number of minutes to a date returning a new object.
|
static java.util.Date |
addMonths(java.util.Date date,
int amount)
Adds a number of months to a date returning a new object.
|
static java.util.Date |
addSeconds(java.util.Date date,
int amount)
Adds a number of seconds to a date returning a new object.
|
static java.util.Date |
addWeeks(java.util.Date date,
int amount)
Adds a number of weeks to a date returning a new object.
|
static java.util.Date |
addYears(java.util.Date date,
int amount)
Adds a number of years to a date returning a new object.
|
static java.util.Calendar |
ceiling(java.util.Calendar date,
int field)
Gets a date ceiling, leaving the field specified as the most
significant field.
|
static java.util.Date |
ceiling(java.util.Date date,
int field)
Gets a date ceiling, leaving the field specified as the most
significant field.
|
static java.util.Date |
ceiling(java.lang.Object date,
int field)
Gets a date ceiling, leaving the field specified as the most
significant field.
|
static long |
getFragmentInDays(java.util.Calendar calendar,
int fragment)
Returns the number of days within the
fragment.
|
static long |
getFragmentInDays(java.util.Date date,
int fragment)
Returns the number of days within the
fragment.
|
static long |
getFragmentInHours(java.util.Calendar calendar,
int fragment)
Returns the number of hours within the
fragment.
|
static long |
getFragmentInHours(java.util.Date date,
int fragment)
Returns the number of hours within the
fragment.
|
static long |
getFragmentInMilliseconds(java.util.Calendar calendar,
int fragment)
Returns the number of milliseconds within the
fragment.
|
static long |
getFragmentInMilliseconds(java.util.Date date,
int fragment)
Returns the number of milliseconds within the
fragment.
|
static long |
getFragmentInMinutes(java.util.Calendar calendar,
int fragment)
Returns the number of minutes within the
fragment.
|
static long |
getFragmentInMinutes(java.util.Date date,
int fragment)
Returns the number of minutes within the
fragment.
|
static long |
getFragmentInSeconds(java.util.Calendar calendar,
int fragment)
Returns the number of seconds within the
fragment.
|
static long |
getFragmentInSeconds(java.util.Date date,
int fragment)
Returns the number of seconds within the
fragment.
|
static boolean |
isSameDay(java.util.Calendar cal1,
java.util.Calendar cal2)
Checks if two calendar objects are on the same day ignoring time.
|
static boolean |
isSameDay(java.util.Date date1,
java.util.Date date2)
Checks if two date objects are on the same day ignoring time.
|
static boolean |
isSameInstant(java.util.Calendar cal1,
java.util.Calendar cal2)
Checks if two calendar objects represent the same instant in time.
|
static boolean |
isSameInstant(java.util.Date date1,
java.util.Date date2)
Checks if two date objects represent the same instant in time.
|
static boolean |
isSameLocalTime(java.util.Calendar cal1,
java.util.Calendar cal2)
Checks if two calendar objects represent the same local time.
|
static java.util.Iterator<java.util.Calendar> |
iterator(java.util.Calendar focus,
int rangeStyle)
Constructs an
Iterator over each day in a date
range defined by a focus date and range style. |
static java.util.Iterator<java.util.Calendar> |
iterator(java.util.Date focus,
int rangeStyle)
Constructs an
Iterator over each day in a date
range defined by a focus date and range style. |
static java.util.Iterator<?> |
iterator(java.lang.Object focus,
int rangeStyle)
Constructs an
Iterator over each day in a date
range defined by a focus date and range style. |
static java.util.Date |
parseDate(java.lang.String str,
java.util.Locale locale,
java.lang.String... parsePatterns)
Parses a string representing a date by trying a variety of different parsers,
using the default date format symbols for the given locale.
|
static java.util.Date |
parseDate(java.lang.String str,
java.lang.String... parsePatterns)
Parses a string representing a date by trying a variety of different parsers.
|
static java.util.Date |
parseDateStrictly(java.lang.String str,
java.util.Locale locale,
java.lang.String... parsePatterns)
Parses a string representing a date by trying a variety of different parsers,
using the default date format symbols for the given locale..
|
static java.util.Date |
parseDateStrictly(java.lang.String str,
java.lang.String... parsePatterns)
Parses a string representing a date by trying a variety of different parsers.
|
static java.util.Calendar |
round(java.util.Calendar date,
int field)
Rounds a date, leaving the field specified as the most
significant field.
|
static java.util.Date |
round(java.util.Date date,
int field)
Rounds a date, leaving the field specified as the most
significant field.
|
static java.util.Date |
round(java.lang.Object date,
int field)
Rounds a date, leaving the field specified as the most
significant field.
|
static java.util.Date |
setDays(java.util.Date date,
int amount)
Sets the day of month field to a date returning a new object.
|
static java.util.Date |
setHours(java.util.Date date,
int amount)
Sets the hours field to a date returning a new object.
|
static java.util.Date |
setMilliseconds(java.util.Date date,
int amount)
Sets the milliseconds field to a date returning a new object.
|
static java.util.Date |
setMinutes(java.util.Date date,
int amount)
Sets the minute field to a date returning a new object.
|
static java.util.Date |
setMonths(java.util.Date date,
int amount)
Sets the months field to a date returning a new object.
|
static java.util.Date |
setSeconds(java.util.Date date,
int amount)
Sets the seconds field to a date returning a new object.
|
static java.util.Date |
setYears(java.util.Date date,
int amount)
Sets the years field to a date returning a new object.
|
static java.util.Calendar |
toCalendar(java.util.Date date)
Converts a
Date into a Calendar . |
static java.util.Calendar |
toCalendar(java.util.Date date,
java.util.TimeZone tz)
Converts a
Date of a given TimeZone into a Calendar |
static java.util.Calendar |
truncate(java.util.Calendar date,
int field)
Truncates a date, leaving the field specified as the most
significant field.
|
static java.util.Date |
truncate(java.util.Date date,
int field)
Truncates a date, leaving the field specified as the most
significant field.
|
static java.util.Date |
truncate(java.lang.Object date,
int field)
Truncates a date, leaving the field specified as the most
significant field.
|
static int |
truncatedCompareTo(java.util.Calendar cal1,
java.util.Calendar cal2,
int field)
Determines how two calendars compare up to no more than the specified
most significant field.
|
static int |
truncatedCompareTo(java.util.Date date1,
java.util.Date date2,
int field)
Determines how two dates compare up to no more than the specified
most significant field.
|
static boolean |
truncatedEquals(java.util.Calendar cal1,
java.util.Calendar cal2,
int field)
Determines if two calendars are equal up to no more than the specified
most significant field.
|
static boolean |
truncatedEquals(java.util.Date date1,
java.util.Date date2,
int field)
Determines if two dates are equal up to no more than the specified
most significant field.
|
public static final long MILLIS_PER_SECOND
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
public static final int SEMI_MONTH
public static final int RANGE_WEEK_SUNDAY
public static final int RANGE_WEEK_MONDAY
public static final int RANGE_WEEK_RELATIVE
public static final int RANGE_WEEK_CENTER
public static final int RANGE_MONTH_SUNDAY
public static final int RANGE_MONTH_MONDAY
public DateUtils()
DateUtils
instances should NOT be constructed in
standard programming. Instead, the static methods on the class should
be used, such as DateUtils.parseDate(str);
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static boolean isSameDay(java.util.Date date1, java.util.Date date2)
Checks if two date objects are on the same day ignoring time.
28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
date1
- the first date, not altered, not nulldate2
- the second date, not altered, not nulljava.lang.IllegalArgumentException
- if either date is null
public static boolean isSameDay(java.util.Calendar cal1, java.util.Calendar cal2)
Checks if two calendar objects are on the same day ignoring time.
28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
cal1
- the first calendar, not altered, not nullcal2
- the second calendar, not altered, not nulljava.lang.IllegalArgumentException
- if either calendar is null
public static boolean isSameInstant(java.util.Date date1, java.util.Date date2)
Checks if two date objects represent the same instant in time.
This method compares the long millisecond time of the two objects.
date1
- the first date, not altered, not nulldate2
- the second date, not altered, not nulljava.lang.IllegalArgumentException
- if either date is null
public static boolean isSameInstant(java.util.Calendar cal1, java.util.Calendar cal2)
Checks if two calendar objects represent the same instant in time.
This method compares the long millisecond time of the two objects.
cal1
- the first calendar, not altered, not nullcal2
- the second calendar, not altered, not nulljava.lang.IllegalArgumentException
- if either date is null
public static boolean isSameLocalTime(java.util.Calendar cal1, java.util.Calendar cal2)
Checks if two calendar objects represent the same local time.
This method compares the values of the fields of the two objects. In addition, both calendars must be the same of the same type.
cal1
- the first calendar, not altered, not nullcal2
- the second calendar, not altered, not nulljava.lang.IllegalArgumentException
- if either date is null
public static java.util.Date parseDate(java.lang.String str, java.lang.String... parsePatterns) throws java.text.ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn. A parse is only deemed successful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
The parser will be lenient toward the parsed date.str
- the date to parse, not nullparsePatterns
- the date format patterns to use, see SimpleDateFormat, not nulljava.lang.IllegalArgumentException
- if the date string or pattern array is nulljava.text.ParseException
- if none of the date patterns were suitable (or there were none)public static java.util.Date parseDate(java.lang.String str, java.util.Locale locale, java.lang.String... parsePatterns) throws java.text.ParseException
Parses a string representing a date by trying a variety of different parsers, using the default date format symbols for the given locale.
The parse will try each parse pattern in turn. A parse is only deemed successful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
The parser will be lenient toward the parsed date.str
- the date to parse, not nulllocale
- the locale whose date format symbols should be used. If null
,
the system locale is used (as per parseDate(String, String...)
).parsePatterns
- the date format patterns to use, see SimpleDateFormat, not nulljava.lang.IllegalArgumentException
- if the date string or pattern array is nulljava.text.ParseException
- if none of the date patterns were suitable (or there were none)public static java.util.Date parseDateStrictly(java.lang.String str, java.lang.String... parsePatterns) throws java.text.ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn. A parse is only deemed successful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
The parser parses strictly - it does not allow for dates such as "February 942, 1996".str
- the date to parse, not nullparsePatterns
- the date format patterns to use, see SimpleDateFormat, not nulljava.lang.IllegalArgumentException
- if the date string or pattern array is nulljava.text.ParseException
- if none of the date patterns were suitablepublic static java.util.Date parseDateStrictly(java.lang.String str, java.util.Locale locale, java.lang.String... parsePatterns) throws java.text.ParseException
Parses a string representing a date by trying a variety of different parsers, using the default date format symbols for the given locale..
The parse will try each parse pattern in turn. A parse is only deemed successful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
The parser parses strictly - it does not allow for dates such as "February 942, 1996".str
- the date to parse, not nulllocale
- the locale whose date format symbols should be used. If null
,
the system locale is used (as per parseDateStrictly(String, String...)
).parsePatterns
- the date format patterns to use, see SimpleDateFormat, not nulljava.lang.IllegalArgumentException
- if the date string or pattern array is nulljava.text.ParseException
- if none of the date patterns were suitablepublic static java.util.Date addYears(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addMonths(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addWeeks(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addDays(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addHours(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addMinutes(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addSeconds(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date addMilliseconds(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to add, may be negativeDate
with the amount addedjava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setYears(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setMonths(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setDays(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setHours(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setMinutes(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setSeconds(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Date setMilliseconds(java.util.Date date, int amount)
Date
is unchanged.date
- the date, not nullamount
- the amount to setDate
set with the specified valuejava.lang.IllegalArgumentException
- if the date is nullpublic static java.util.Calendar toCalendar(java.util.Date date)
Date
into a Calendar
.date
- the date to convert to a Calendarjava.lang.NullPointerException
- if null is passed inpublic static java.util.Calendar toCalendar(java.util.Date date, java.util.TimeZone tz)
Date
of a given TimeZone
into a Calendar
date
- the date to convert to a Calendartz
- the time zone of the date
java.lang.NullPointerException
- if date
or tz
is nullpublic static java.util.Date round(java.util.Date date, int field)
Rounds a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if this was passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 April 2002 0:00:00.000.
For a date in a timezone that handles the change to daylight saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. Suppose daylight saving time begins at 02:00 on March 30. Rounding a date that crosses this time would produce the following values:
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Calendar round(java.util.Calendar date, int field)
Rounds a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if this was passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 April 2002 0:00:00.000.
For a date in a timezone that handles the change to daylight saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. Suppose daylight saving time begins at 02:00 on March 30. Rounding a date that crosses this time would produce the following values:
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Date round(java.lang.Object date, int field)
Rounds a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if this was passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 April 2002 0:00:00.000.
For a date in a timezone that handles the change to daylight saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. Suppose daylight saving time begins at 02:00 on March 30. Rounding a date that crosses this time would produce the following values:
date
- the date to work with, either Date
or Calendar
, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ClassCastException
- if the object type is not a Date
or Calendar
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Date truncate(java.util.Date date, int field)
Truncates a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Calendar truncate(java.util.Calendar date, int field)
Truncates a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Date truncate(java.lang.Object date, int field)
Truncates a date, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.
date
- the date to work with, either Date
or Calendar
, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ClassCastException
- if the object type is not a Date
or Calendar
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Date ceiling(java.util.Date date, int field)
Gets a date ceiling, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 Apr 2002 0:00:00.000.
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Calendar ceiling(java.util.Calendar date, int field)
Gets a date ceiling, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 Apr 2002 0:00:00.000.
date
- the date to work with, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Date ceiling(java.lang.Object date, int field)
Gets a date ceiling, leaving the field specified as the most significant field.
For example, if you had the date-time of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 Apr 2002 0:00:00.000.
date
- the date to work with, either Date
or Calendar
, not nullfield
- the field from Calendar
or SEMI_MONTH
java.lang.IllegalArgumentException
- if the date is null
java.lang.ClassCastException
- if the object type is not a Date
or Calendar
java.lang.ArithmeticException
- if the year is over 280 millionpublic static java.util.Iterator<java.util.Calendar> iterator(java.util.Date focus, int rangeStyle)
Constructs an Iterator
over each day in a date
range defined by a focus date and range style.
For instance, passing Thursday, July 4, 2002 and a
RANGE_MONTH_SUNDAY
will return an Iterator
that starts with Sunday, June 30, 2002 and ends with Saturday, August 3,
2002, returning a Calendar instance for each intermediate day.
This method provides an iterator that returns Calendar objects.
The days are progressed using Calendar.add(int, int)
.
focus
- the date to work with, not nullrangeStyle
- the style constant to use. Must be one of
RANGE_MONTH_SUNDAY
,
RANGE_MONTH_MONDAY
,
RANGE_WEEK_SUNDAY
,
RANGE_WEEK_MONDAY
,
RANGE_WEEK_RELATIVE
,
RANGE_WEEK_CENTER
java.lang.IllegalArgumentException
- if the date is null
java.lang.IllegalArgumentException
- if the rangeStyle is invalidpublic static java.util.Iterator<java.util.Calendar> iterator(java.util.Calendar focus, int rangeStyle)
Constructs an Iterator
over each day in a date
range defined by a focus date and range style.
For instance, passing Thursday, July 4, 2002 and a
RANGE_MONTH_SUNDAY
will return an Iterator
that starts with Sunday, June 30, 2002 and ends with Saturday, August 3,
2002, returning a Calendar instance for each intermediate day.
This method provides an iterator that returns Calendar objects.
The days are progressed using Calendar.add(int, int)
.
focus
- the date to work with, not nullrangeStyle
- the style constant to use. Must be one of
RANGE_MONTH_SUNDAY
,
RANGE_MONTH_MONDAY
,
RANGE_WEEK_SUNDAY
,
RANGE_WEEK_MONDAY
,
RANGE_WEEK_RELATIVE
,
RANGE_WEEK_CENTER
java.lang.IllegalArgumentException
- if the date is null
java.lang.IllegalArgumentException
- if the rangeStyle is invalidpublic static java.util.Iterator<?> iterator(java.lang.Object focus, int rangeStyle)
Constructs an Iterator
over each day in a date
range defined by a focus date and range style.
For instance, passing Thursday, July 4, 2002 and a
RANGE_MONTH_SUNDAY
will return an Iterator
that starts with Sunday, June 30, 2002 and ends with Saturday, August 3,
2002, returning a Calendar instance for each intermediate day.
focus
- the date to work with, either Date
or Calendar
, not nullrangeStyle
- the style constant to use. Must be one of the range
styles listed for the iterator(Calendar, int)
method.java.lang.IllegalArgumentException
- if the date is null
java.lang.ClassCastException
- if the object type is not a Date
or Calendar
public static long getFragmentInMilliseconds(java.util.Date date, int fragment)
Returns the number of milliseconds within the fragment. All datefields greater than the fragment will be ignored.
Asking the milliseconds of any date will only return the number of milliseconds of the current second (resulting in a number between 0 and 999). This method will retrieve the number of milliseconds for any fragment. For example, if you want to calculate the number of milliseconds past today, your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will be all milliseconds of the past hour(s), minutes(s) and second(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a SECOND field will return 0.
date
- the date to work with, not nullfragment
- the Calendar
field part of date to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInSeconds(java.util.Date date, int fragment)
Returns the number of seconds within the fragment. All datefields greater than the fragment will be ignored.
Asking the seconds of any date will only return the number of seconds of the current minute (resulting in a number between 0 and 59). This method will retrieve the number of seconds for any fragment. For example, if you want to calculate the number of seconds past today, your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will be all seconds of the past hour(s) and minutes(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a SECOND field will return 0.
date
- the date to work with, not nullfragment
- the Calendar
field part of date to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInMinutes(java.util.Date date, int fragment)
Returns the number of minutes within the fragment. All datefields greater than the fragment will be ignored.
Asking the minutes of any date will only return the number of minutes of the current hour (resulting in a number between 0 and 59). This method will retrieve the number of minutes for any fragment. For example, if you want to calculate the number of minutes past this month, your fragment is Calendar.MONTH. The result will be all minutes of the past day(s) and hour(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a MINUTE field will return 0.
date
- the date to work with, not nullfragment
- the Calendar
field part of date to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInHours(java.util.Date date, int fragment)
Returns the number of hours within the fragment. All datefields greater than the fragment will be ignored.
Asking the hours of any date will only return the number of hours of the current day (resulting in a number between 0 and 23). This method will retrieve the number of hours for any fragment. For example, if you want to calculate the number of hours past this month, your fragment is Calendar.MONTH. The result will be all hours of the past day(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a HOUR field will return 0.
date
- the date to work with, not nullfragment
- the Calendar
field part of date to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInDays(java.util.Date date, int fragment)
Returns the number of days within the fragment. All datefields greater than the fragment will be ignored.
Asking the days of any date will only return the number of days of the current month (resulting in a number between 1 and 31). This method will retrieve the number of days for any fragment. For example, if you want to calculate the number of days past this year, your fragment is Calendar.YEAR. The result will be all days of the past month(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a DAY field will return 0.
date
- the date to work with, not nullfragment
- the Calendar
field part of date to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInMilliseconds(java.util.Calendar calendar, int fragment)
Returns the number of milliseconds within the fragment. All datefields greater than the fragment will be ignored.
Asking the milliseconds of any date will only return the number of milliseconds of the current second (resulting in a number between 0 and 999). This method will retrieve the number of milliseconds for any fragment. For example, if you want to calculate the number of seconds past today, your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will be all seconds of the past hour(s), minutes(s) and second(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a MILLISECOND field will return 0.
calendar
- the calendar to work with, not nullfragment
- the Calendar
field part of calendar to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInSeconds(java.util.Calendar calendar, int fragment)
Returns the number of seconds within the fragment. All datefields greater than the fragment will be ignored.
Asking the seconds of any date will only return the number of seconds of the current minute (resulting in a number between 0 and 59). This method will retrieve the number of seconds for any fragment. For example, if you want to calculate the number of seconds past today, your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will be all seconds of the past hour(s) and minutes(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a SECOND field will return 0.
calendar
- the calendar to work with, not nullfragment
- the Calendar
field part of calendar to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInMinutes(java.util.Calendar calendar, int fragment)
Returns the number of minutes within the fragment. All datefields greater than the fragment will be ignored.
Asking the minutes of any date will only return the number of minutes of the current hour (resulting in a number between 0 and 59). This method will retrieve the number of minutes for any fragment. For example, if you want to calculate the number of minutes past this month, your fragment is Calendar.MONTH. The result will be all minutes of the past day(s) and hour(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a MINUTE field will return 0.
calendar
- the calendar to work with, not nullfragment
- the Calendar
field part of calendar to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInHours(java.util.Calendar calendar, int fragment)
Returns the number of hours within the fragment. All datefields greater than the fragment will be ignored.
Asking the hours of any date will only return the number of hours of the current day (resulting in a number between 0 and 23). This method will retrieve the number of hours for any fragment. For example, if you want to calculate the number of hours past this month, your fragment is Calendar.MONTH. The result will be all hours of the past day(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a HOUR field will return 0.
calendar
- the calendar to work with, not nullfragment
- the Calendar
field part of calendar to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static long getFragmentInDays(java.util.Calendar calendar, int fragment)
Returns the number of days within the fragment. All datefields greater than the fragment will be ignored.
Asking the days of any date will only return the number of days of the current month (resulting in a number between 1 and 31). This method will retrieve the number of days for any fragment. For example, if you want to calculate the number of days past this year, your fragment is Calendar.YEAR. The result will be all days of the past month(s).
Valid fragments are: Calendar.YEAR, Calendar.MONTH, both Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND A fragment less than or equal to a DAY field will return 0.
calendar
- the calendar to work with, not nullfragment
- the Calendar
field part of calendar to calculatejava.lang.IllegalArgumentException
- if the date is null
or
fragment is not supportedpublic static boolean truncatedEquals(java.util.Calendar cal1, java.util.Calendar cal2, int field)
cal1
- the first calendar, not null
cal2
- the second calendar, not null
field
- the field from Calendar
true
if equal; otherwise false
java.lang.IllegalArgumentException
- if any argument is null
truncate(Calendar, int)
,
truncatedEquals(Date, Date, int)
public static boolean truncatedEquals(java.util.Date date1, java.util.Date date2, int field)
date1
- the first date, not null
date2
- the second date, not null
field
- the field from Calendar
true
if equal; otherwise false
java.lang.IllegalArgumentException
- if any argument is null
truncate(Date, int)
,
truncatedEquals(Calendar, Calendar, int)
public static int truncatedCompareTo(java.util.Calendar cal1, java.util.Calendar cal2, int field)
cal1
- the first calendar, not null
cal2
- the second calendar, not null
field
- the field from Calendar
java.lang.IllegalArgumentException
- if any argument is null
truncate(Calendar, int)
,
truncatedCompareTo(Date, Date, int)
public static int truncatedCompareTo(java.util.Date date1, java.util.Date date2, int field)
date1
- the first date, not null
date2
- the second date, not null
field
- the field from Calendar
java.lang.IllegalArgumentException
- if any argument is null
truncate(Calendar, int)
,
truncatedCompareTo(Date, Date, int)
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"