public abstract class DateUtils
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_DATE_TIME_FORMAT |
static int |
HOURS_PER_DAY
Number of hours in a day
|
static long |
MILLIS_PER_DAY
Number of milliseconds in a day.
|
static long |
MILLIS_PER_HOUR
Number of milliseconds in a hour.
|
static long |
MILLIS_PER_MINUTE
Number of milliseconds in a minute.
|
static long |
MILLIS_PER_SECOND
Number of milliseconds in a second.
|
static long |
MILLIS_PER_WEEK
Number of milliseconds in a week.
|
static int |
SECONDS_PER_DAY
Seconds in a day
|
static int |
SECONDS_PER_HOUR
Seconds in an hour
|
static int |
SECONDS_PER_MINUTE
Seconds in a minute
|
static int |
WEEK
Seconds in a week
|
| Constructor and Description |
|---|
DateUtils() |
| Modifier and Type | Method and Description |
|---|---|
static java.util.Date |
addDays(java.util.Date date,
int days) |
static java.util.Date |
addHours(java.util.Date date,
int hours) |
static java.util.Date |
addMonths(java.util.Date date,
int days) |
static java.util.Date |
addSeconds(java.util.Date date,
int seconds) |
static java.util.Date |
addYears(java.util.Date date,
int years) |
static boolean |
containsPeriod(java.util.Date startDate1,
java.util.Date endDate1,
java.util.Date startDate2,
java.util.Date endDate2)
Returns true if the first period contains the second periods
|
static java.lang.Integer |
differenceInDays(java.util.Date fromDate,
java.util.Date toDate) |
static double |
differenceInHours(java.util.Date fromDate,
java.util.Date toDate) |
static java.lang.Integer |
differenceInMinutes(java.util.Date fromDate,
java.util.Date toDate) |
static java.lang.String |
format(java.util.Date date,
java.text.DateFormat dateFormat)
Transform the
Date to a String using the received SimpleDateFormat |
static java.lang.String |
format(java.util.Date date,
java.text.DateFormat dateFormat,
boolean useUtc) |
static java.lang.String |
format(java.util.Date date,
java.lang.String dateFormat) |
static java.lang.String |
format(java.util.Date date,
java.lang.String dateFormat,
boolean useUtc) |
static java.lang.String |
formatDate(java.util.Date date) |
static java.lang.String |
formatDateTime(java.util.Date date) |
static java.lang.String |
formatDuration(long duration) |
static java.lang.String |
formatTime(java.util.Date date) |
static java.lang.String |
formatToCardinal(java.util.Date date)
Transform the
Date to a String with a format like Friday November 5th |
static java.util.Date |
getDate(java.util.Date date,
java.util.Date time) |
static java.util.Date |
getDate(java.util.Date date,
java.util.Date time,
java.lang.Boolean is24Hour) |
static java.util.Date |
getDate(int year,
int monthOfYear,
int dayOfMonth)
Creates a
Date for the specified day |
static java.util.Date |
getDate(long milliseconds) |
static int |
getDayOfMonth(java.util.Date date) |
static DayOfWeek |
getDayOfWeek() |
static DayOfWeek |
getDayOfWeek(java.util.Date date) |
static int |
getHour(java.util.Date date,
java.lang.Boolean is24Hour) |
static int |
getHour(java.util.Date date,
java.util.TimeZone timeZone,
java.lang.Boolean is24Hour) |
static java.util.Date |
getLastDayOfMonth(java.util.Date date) |
static int |
getMinute(java.util.Date date) |
static int |
getMinute(java.util.Date date,
java.util.TimeZone timeZone) |
static int |
getMonth(java.util.Date date) |
static java.util.Date |
getTime(int hour,
int minutes) |
static java.util.Date |
getTime(int hour,
int minutes,
java.lang.Boolean is24Hour) |
static int |
getYear() |
static int |
getYear(java.util.Date date) |
static void |
init() |
static boolean |
isAfter(java.util.Date date,
java.util.Date dateToCompare)
Tests if the date is after than the specified dateToCompare.
|
static boolean |
isAfterEquals(java.util.Date date,
java.util.Date dateToCompare)
Tests if the date is after or equals than the specified dateToCompare.
|
static boolean |
isBefore(java.util.Date date,
java.util.Date dateToCompare)
Tests if the date is before than the specified dateToCompare.
|
static boolean |
isBeforeEquals(java.util.Date date,
java.util.Date dateToCompare)
Tests if the date is before or equals than the specified dateToCompare.
|
static boolean |
isBetween(java.util.Date date,
java.util.Date startDate,
java.util.Date endDate) |
static boolean |
isDateOnWeekend(java.util.Date date) |
static java.lang.Long |
millisecondsToDays(java.lang.Long timestamp) |
static java.util.Date |
monthsAway(int months) |
static java.util.Date |
now() |
static long |
nowMillis() |
static java.util.Date |
oneMonthInFuture() |
static java.util.Date |
oneMonthInPast() |
static java.util.Date |
parse(java.lang.String dateFormatted,
java.text.SimpleDateFormat dateFormat) |
static java.util.Date |
parse(java.lang.String dateFormatted,
java.text.SimpleDateFormat dateFormat,
boolean useUtc) |
static java.util.Date |
parse(java.lang.String dateFormatted,
java.lang.String dateFormat) |
static java.util.Date |
parse(java.lang.String dateFormatted,
java.lang.String dateFormat,
boolean useUtc) |
static boolean |
periodsOverlap(java.util.Date startDate1,
java.util.Date endDate1,
java.util.Date startDate2,
java.util.Date endDate2)
Returns true if two periods overlap
|
static void |
setFakeNow(java.util.Date fakeNow) |
static java.util.Date |
setHour(java.util.Date date,
int hours,
java.lang.Boolean is24Hour) |
static java.util.Date |
today() |
static java.util.Calendar |
todayCalendar() |
static java.util.Date |
tomorrow() |
static void |
truncateDate(java.util.Calendar calendar)
Truncate the
Calendar date asigning it to 1st of January of 1980 |
static java.util.Date |
truncateDate(java.util.Date date)
Truncate the date asigning it to 1st of January of 1980
|
static void |
truncateTime(java.util.Calendar calendar)
Truncate the
Calendar removing hours, minutes, seconds and milliseconds |
static java.util.Date |
truncateTime(java.util.Date date)
Truncate the date removing hours, minutes, seconds and milliseconds
|
static java.util.Date |
yesterday() |
public static final int SECONDS_PER_MINUTE
public static final int SECONDS_PER_HOUR
public static final int SECONDS_PER_DAY
public static final int WEEK
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 long MILLIS_PER_WEEK
public static final int HOURS_PER_DAY
public static java.lang.String DEFAULT_DATE_TIME_FORMAT
public static void init()
public static java.util.Date parse(java.lang.String dateFormatted,
java.lang.String dateFormat)
dateFormatted - The formatted string to parsedateFormat - public static java.util.Date parse(java.lang.String dateFormatted,
java.lang.String dateFormat,
boolean useUtc)
public static java.util.Date parse(java.lang.String dateFormatted,
java.text.SimpleDateFormat dateFormat)
dateFormatted - The formatted string to parsedateFormat - public static java.util.Date parse(java.lang.String dateFormatted,
java.text.SimpleDateFormat dateFormat,
boolean useUtc)
public static java.lang.String formatToCardinal(java.util.Date date)
Date to a String with a format like Friday November 5thdate - The Date to be formattedpublic static java.lang.String format(java.util.Date date,
java.lang.String dateFormat)
date - The Date to be formatteddateFormat - The DateFormat used to format the Datepublic static java.lang.String format(java.util.Date date,
java.lang.String dateFormat,
boolean useUtc)
public static java.lang.String format(java.util.Date date,
java.text.DateFormat dateFormat)
Date to a String using the received SimpleDateFormatdate - The Date to be formatteddateFormat - The DateFormat used to format the Datepublic static java.lang.String format(java.util.Date date,
java.text.DateFormat dateFormat,
boolean useUtc)
public static java.lang.String formatDateTime(java.util.Date date)
public static java.lang.String formatDate(java.util.Date date)
public static java.lang.String formatTime(java.util.Date date)
public static java.util.Date getDate(int year,
int monthOfYear,
int dayOfMonth)
Date for the specified dayyear - The yearmonthOfYear - The month number (starting on 0)dayOfMonth - The day of the monthDatepublic static java.util.Date getDate(java.util.Date date,
java.util.Date time,
java.lang.Boolean is24Hour)
public static java.util.Date getDate(java.util.Date date,
java.util.Date time)
public static java.util.Date getDate(long milliseconds)
public static java.util.Date getTime(int hour,
int minutes)
public static java.util.Date getTime(int hour,
int minutes,
java.lang.Boolean is24Hour)
public static int getYear()
public static int getYear(java.util.Date date)
public static int getMonth(java.util.Date date)
public static int getDayOfMonth(java.util.Date date)
public static int getHour(java.util.Date date,
java.lang.Boolean is24Hour)
public static int getHour(java.util.Date date,
java.util.TimeZone timeZone,
java.lang.Boolean is24Hour)
public static int getMinute(java.util.Date date)
public static int getMinute(java.util.Date date,
java.util.TimeZone timeZone)
public static DayOfWeek getDayOfWeek()
public static DayOfWeek getDayOfWeek(java.util.Date date)
public static boolean isDateOnWeekend(java.util.Date date)
public static java.util.Date setHour(java.util.Date date,
int hours,
java.lang.Boolean is24Hour)
public static java.util.Date addSeconds(java.util.Date date,
int seconds)
public static java.util.Date addHours(java.util.Date date,
int hours)
public static java.util.Date addDays(java.util.Date date,
int days)
public static java.util.Date addMonths(java.util.Date date,
int days)
public static java.util.Date addYears(java.util.Date date,
int years)
public static java.util.Date truncateDate(java.util.Date date)
date - The Date to truncateDatepublic static void truncateDate(java.util.Calendar calendar)
Calendar date asigning it to 1st of January of 1980calendar - The Calendar to truncatepublic static java.util.Date truncateTime(java.util.Date date)
date - The Date to truncateDatepublic static void truncateTime(java.util.Calendar calendar)
Calendar removing hours, minutes, seconds and millisecondscalendar - The Calendar to truncatepublic static java.util.Date now()
public static long nowMillis()
public static boolean isBetween(java.util.Date date,
java.util.Date startDate,
java.util.Date endDate)
date - The date to comparestartDate - The left between' sideendDate - The right between's sidetrue if the date is in the middle of startDate and endDatepublic static boolean isBefore(java.util.Date date,
java.util.Date dateToCompare)
date - the date to compare with the dateToCompare.dateToCompare - the date to compare with the date.true if the instant of time represented by date object is earlier than the
instant represented by dateToCompare; false otherwise.public static boolean isBeforeEquals(java.util.Date date,
java.util.Date dateToCompare)
date - the date to compare with the dateToCompare.dateToCompare - the date to compare with the date.true if the instant of time represented by date object is earlier or equal than
the instant represented by dateToCompare; false otherwise.public static boolean isAfterEquals(java.util.Date date,
java.util.Date dateToCompare)
date - the date to compare with the dateToCompare.dateToCompare - the date to compare with the date.true if the instant of time represented by date object is later or equal than
the instant represented by dateToCompare; false otherwise.public static boolean isAfter(java.util.Date date,
java.util.Date dateToCompare)
date - the date to compare with the dateToCompare.dateToCompare - the date to compare with the date.true if the instant of time represented by date object is later than the
instant represented by dateToCompare; false otherwise.public static boolean periodsOverlap(java.util.Date startDate1,
java.util.Date endDate1,
java.util.Date startDate2,
java.util.Date endDate2)
startDate1 - the period one start dateendDate1 - the period one end datestartDate2 - the period two start dateendDate2 - the period two end datepublic static boolean containsPeriod(java.util.Date startDate1,
java.util.Date endDate1,
java.util.Date startDate2,
java.util.Date endDate2)
startDate1 - the period one start dateendDate1 - the period one end datestartDate2 - the period two start dateendDate2 - the period two end datepublic static java.util.Calendar todayCalendar()
public static java.util.Date tomorrow()
public static java.util.Date today()
public static java.util.Date yesterday()
public static java.util.Date monthsAway(int months)
months - amount of months to move the calendarmonths in the future/past. Use negative values for past dates.public static java.util.Date oneMonthInFuture()
public static java.util.Date oneMonthInPast()
public static java.util.Date getLastDayOfMonth(java.util.Date date)
date - Date that includes the desired month in order to calculate the last day of that monthpublic static java.lang.Integer differenceInDays(java.util.Date fromDate,
java.util.Date toDate)
fromDate - the start datetoDate - the end datepublic static double differenceInHours(java.util.Date fromDate,
java.util.Date toDate)
fromDate - the start datetoDate - the end datepublic static java.lang.Integer differenceInMinutes(java.util.Date fromDate,
java.util.Date toDate)
fromDate - the start datetoDate - the end datepublic static java.lang.String formatDuration(long duration)
public static java.lang.Long millisecondsToDays(java.lang.Long timestamp)
public static void setFakeNow(java.util.Date fakeNow)