public final class DateUtil
extends java.lang.Object
CalendarUtil
Modifier and Type | Method and Description |
---|---|
static java.util.Date |
addDays(java.util.Date date,
int days)
Add the given amount of days to the given date.
|
static java.util.Date |
addHours(java.util.Date date,
int hours)
Add the given amount of hours to the given date.
|
static java.util.Date |
addMillis(java.util.Date date,
int millis)
Add the given amount of millis to the given date.
|
static java.util.Date |
addMinutes(java.util.Date date,
int minutes)
Add the given amount of minutes to the given date.
|
static java.util.Date |
addMonths(java.util.Date date,
int months)
Add the given amount of months to the given date.
|
static java.util.Date |
addSeconds(java.util.Date date,
int seconds)
Add the given amount of seconds to the given date.
|
static java.util.Date |
addYears(java.util.Date date,
int years)
Add the given amount of years to the given date.
|
static java.lang.String |
determineDateFormat(java.lang.String dateString)
Determine SimpleDateFormat pattern matching with the given date string.
|
static int |
elapsedDays(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed days between the two given dates.
|
static int |
elapsedHours(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed hours between the two given dates.
|
static long |
elapsedMillis(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed milliseconds between the two given dates.
|
static int |
elapsedMinutes(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed minutes between the two given dates.
|
static int |
elapsedMonths(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed months between the two given dates.
|
static int |
elapsedSeconds(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed seconds between the two given dates.
|
static int[] |
elapsedTime(java.util.Date before,
java.util.Date after)
Calculate the total of elapsed time from years up to seconds between the two given dates.
|
static int |
elapsedYears(java.util.Date before,
java.util.Date after)
Retrieve the amount of elapsed years between the two given dates.
|
static boolean |
isValidDate(java.lang.String dateString)
Checks whether the actual date of the given date string is valid.
|
static boolean |
isValidDate(java.lang.String dateString,
java.lang.String dateFormat)
Checks whether the actual date of the given date string is valid based on the given date
format pattern.
|
static java.util.Date |
parse(java.lang.String dateString)
Parse the given date string to date object and return a date instance based on the given
date string.
|
static java.util.Date |
parse(java.lang.String dateString,
java.lang.String dateFormat)
Validate the actual date of the given date string based on the given date format pattern and
return a date instance based on the given date string.
|
static boolean |
sameDay(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year, month and day.
|
static boolean |
sameHour(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year, month, day and
hour.
|
static boolean |
sameMinute(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year, month, day, hour
and minute.
|
static boolean |
sameMonth(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year and month.
|
static boolean |
sameSecond(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year, month, day, hour,
minute and second.
|
static boolean |
sameYear(java.util.Date one,
java.util.Date two)
Returns true if the two given dates are dated on the same year.
|
static java.util.Calendar |
toCalendar(java.util.Date date)
Convert the given date to a Calendar object.
|
static java.util.Calendar |
toCalendar(java.util.Date date,
java.util.TimeZone timeZone)
Convert the given date to a Calendar object with the given timezone.
|
public static java.util.Calendar toCalendar(java.util.Date date)
date
- The date to be converted to Calendar.public static java.util.Calendar toCalendar(java.util.Date date, java.util.TimeZone timeZone)
date
- The date to be converted to Calendar.timeZone
- The timezone to be set in the Calendar.public static java.util.Date parse(java.lang.String dateString) throws java.text.ParseException
determineDateFormat(String)
to determine
the SimpleDateFormat pattern to be used for parsing.dateString
- The date string to be parsed to date object.java.text.ParseException
- If the date format pattern of the given date string is unknown, or if
the given date string or its actual date is invalid based on the date format pattern.public static java.util.Date parse(java.lang.String dateString, java.lang.String dateFormat) throws java.text.ParseException
dateString
- The date string.dateFormat
- The date format pattern which should respect the SimpleDateFormat rules.java.text.ParseException
- If the given date string or its actual date is invalid based on the
given date format pattern.SimpleDateFormat
public static boolean isValidDate(java.lang.String dateString)
determineDateFormat(String)
to determine the SimpleDateFormat pattern to be
used for parsing.dateString
- The date string.public static boolean isValidDate(java.lang.String dateString, java.lang.String dateFormat)
dateString
- The date string.dateFormat
- The date format pattern which should respect the SimpleDateFormat rules.SimpleDateFormat
public static java.lang.String determineDateFormat(java.lang.String dateString)
dateString
- The date string to determine the SimpleDateFormat pattern for.SimpleDateFormat
public static java.util.Date addYears(java.util.Date date, int years)
CalendarUtil.addYears(Calendar, int)
and then converts back to date.date
- The date to add the given amount of years to.years
- The amount of years to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addMonths(java.util.Date date, int months)
CalendarUtil.addMonths(Calendar, int)
and then converts back to date.date
- The date to add the given amount of months to.months
- The amount of months to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addDays(java.util.Date date, int days)
CalendarUtil.addDays(Calendar, int)
and then converts back to date.date
- The date to add the given amount of days to.days
- The amount of days to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addHours(java.util.Date date, int hours)
CalendarUtil.addHours(Calendar, int)
and then converts back to date.date
- The date to add the given amount of hours to.hours
- The amount of hours to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addMinutes(java.util.Date date, int minutes)
CalendarUtil.addMinutes(Calendar, int)
and then converts back to date.date
- The date to add the given amount of minutes to.minutes
- The amount of minutes to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addSeconds(java.util.Date date, int seconds)
CalendarUtil.addSeconds(Calendar, int)
and then converts back to date.date
- The date to add the given amount of seconds to.seconds
- The amount of seconds to be added to the given date. Negative values are also
allowed, it will just go back in time.public static java.util.Date addMillis(java.util.Date date, int millis)
CalendarUtil.addMillis(Calendar, int)
and then converts back to date.date
- The date to add the given amount of millis to.millis
- The amount of millis to be added to the given date. Negative values are also
allowed, it will just go back in time.public static boolean sameYear(java.util.Date one, java.util.Date two)
CalendarUtil.sameYear(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameYear(Calendar, Calendar)
public static boolean sameMonth(java.util.Date one, java.util.Date two)
CalendarUtil.sameMonth(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameMonth(Calendar, Calendar)
public static boolean sameDay(java.util.Date one, java.util.Date two)
CalendarUtil.sameDay(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameDay(Calendar, Calendar)
public static boolean sameHour(java.util.Date one, java.util.Date two)
CalendarUtil.sameHour(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameHour(Calendar, Calendar)
public static boolean sameMinute(java.util.Date one, java.util.Date two)
CalendarUtil.sameMinute(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameMinute(Calendar, Calendar)
public static boolean sameSecond(java.util.Date one, java.util.Date two)
CalendarUtil.sameSecond(Calendar, Calendar)
.one
- The one date.two
- The other date.CalendarUtil.sameSecond(Calendar, Calendar)
public static int elapsedYears(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedYears(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedYears(Calendar, Calendar)
public static int elapsedMonths(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedMonths(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedMonths(Calendar, Calendar)
public static int elapsedDays(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedDays(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedDays(Calendar, Calendar)
public static int elapsedHours(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedHours(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedHours(Calendar, Calendar)
public static int elapsedMinutes(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedMinutes(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedMinutes(Calendar, Calendar)
public static int elapsedSeconds(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedSeconds(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedSeconds(Calendar, Calendar)
public static long elapsedMillis(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedMillis(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedMillis(Calendar, Calendar)
public static int[] elapsedTime(java.util.Date before, java.util.Date after)
CalendarUtil.elapsedTime(Calendar, Calendar)
.before
- The first date with expected date before the second date.after
- The second date with expected date after the first date.java.lang.IllegalArgumentException
- If the first date is dated after the second date.CalendarUtil.elapsedTime(Calendar, Calendar)