public class DateUtil extends Object
Modifier and Type | Field and Description |
---|---|
static long |
DAY |
static long |
HOUR |
static long |
MILLISECOND |
static long |
MINUTE |
static long |
MONTH |
static long |
SECOND |
static long |
YEAR |
Constructor and Description |
---|
DateUtil()
Creates DateUtil object in default timezone.
|
DateUtil(TimeZone tz)
Constructor for timezone.
|
Modifier and Type | Method and Description |
---|---|
static int |
compare(Date d1,
Date d2)
Compares two dates.
|
static Comparator<Date> |
compareByDateField(long field)
Compares two dates or sorts multiple dates by the granularity of a specific field.
|
int |
getOffset(long date)
Gets the offset from GMT in milliseconds for the given date.
|
String |
getTimeAgo(Date date)
Gets the date in "time ago" format.
|
boolean |
inDaylightTime(Date date)
Checks whether the given date is in daylight savings time for the given date.
|
boolean |
isSameDay(Calendar c1,
Calendar c2)
Checks if two times are on the same day using
Calendar . |
boolean |
isSameDay(Date d1,
Date d2)
Checks if two dates are on the same day in the current timezone.
|
boolean |
isSameHour(Calendar c1,
Calendar c2)
Checks if two times are on the same hour using
Calendar . |
boolean |
isSameHour(Date d1,
Date d2)
Checks if two dates are on the same hour in the current timezone.
|
boolean |
isSameMinute(Calendar c1,
Calendar c2)
Checks if two times are on the same minute using
Calendar . |
boolean |
isSameMinute(Date d1,
Date d2)
Checks if two dates are on the same minute in the current timezone.
|
boolean |
isSameMonth(Calendar c1,
Calendar c2)
Checks if two times are on the same month using
Calendar . |
boolean |
isSameMonth(Date d1,
Date d2)
Checks if two dates are on the same month in the current timezone.
|
boolean |
isSameSecond(Calendar c1,
Calendar c2)
Checks if two times are on the same second using
Calendar . |
boolean |
isSameSecond(Date d1,
Date d2)
Checks if two dates are on the same second in the current timezone.
|
boolean |
isSameTime(Calendar c1,
Calendar c2)
Checks if two times are on the same time using
Calendar . |
boolean |
isSameTime(Date d1,
Date d2)
Checks if two dates are on the same time in the current timezone.
|
boolean |
isSameYear(Calendar c1,
Calendar c2)
Checks if two times are on the same year using
Calendar . |
boolean |
isSameYear(Date d1,
Date d2)
Checks if two dates are on the same year in the current timezone.
|
static Date |
max(Date... dates)
Returns the latest of a set of dates.
|
static Date |
min(Date... dates)
Returns the earliest of a set of dates.
|
public static final long MILLISECOND
public static final long SECOND
public static final long MINUTE
public static final long HOUR
public static final long DAY
public static final long MONTH
public static final long YEAR
public DateUtil(TimeZone tz)
tz
- Timezonepublic DateUtil()
public static Date min(Date... dates)
dates
- public static int compare(Date d1, Date d2)
d1
- A dated2
- A datepublic static Comparator<Date> compareByDateField(long field)
Compare dates:
compareByDateField(DateUtil.MONTH).compare(date1, date2)
Sort dates:
dateList.sort(compareByDateField(DateUtil.MONTH))
field
- One of the fields:
DateUtil.MILLISECOND
DateUtil.SECOND
DateUtil.MINUTE
DateUtil.HOUR
DateUtil.DATE
DateUtil.MONTH
DateUtil.YEAR
public static Date max(Date... dates)
dates
- public int getOffset(long date)
date
- The date at which the offset is calculated.public boolean inDaylightTime(Date date)
date
- the date to checkpublic boolean isSameYear(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameYear(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameMonth(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameMonth(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameDay(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameDay(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameHour(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameHour(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameMinute(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameMinute(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameSecond(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameSecond(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.public boolean isSameTime(Calendar c1, Calendar c2)
Calendar
.c1
- First time to compare.c2
- Second time to compare.public boolean isSameTime(Date d1, Date d2)
d1
- First date to compare.d2
- Second date to compare.