public class DateUtil
- Object
- DateUtil
Utility class for working with dates and timezones.
Fields
Constructors
public DateUtil(TimeZone tz) | Constructor for timezone. |
public DateUtil() | Creates DateUtil object in default timezone. |
Methods
Inherited methods
Field details
MILLISECOND
public static final long MILLISECOND = 1LSECOND
public static final long SECOND = 1000LMINUTE
public static final long MINUTE = 60000LHOUR
public static final long HOUR = 3600000LDAY
public static final long DAY = 86400000LMONTH
public static final long MONTH = 2629800000LYEAR
public static final long YEAR = 31557600000LConstructor details
DateUtil
public DateUtil(TimeZone tz)Constructor for timezone.
Parameters
tzTimeZone- Timezone
DateUtil
public DateUtil()Creates DateUtil object in default timezone.
Method details
min
public static Date min(Date... dates)Returns the earliest of a set of dates.
Returns
The earliest of a set of dates.
compare
public static int compare(Date d1, Date d2)Compares two dates.
Parameters
d1Date- A date
d2Date- A date
Returns
-1 if first date is earlier. 1 if first date is later. 0 if they are the same.
compareByDateField
public static Comparator<Date> compareByDateField(long field)Compares two dates or sorts multiple dates by the granularity of a specific field.
Compare dates:
compareByDateField(DateUtil.MONTH).compare(date1, date2)
Sort dates:
dateList.sort(compareByDateField(DateUtil.MONTH))
Parameters
fieldlong- One of the fields:
Returns
0 - if first date is earlier.
< 0 - if first date is later.
== 0 - if they are equal.
max
public static Date max(Date... dates)Returns the latest of a set of dates.
Returns
The latest of a set of dates.
getOffset
public int getOffset(long date)Gets the offset from GMT in milliseconds for the given date.
Parameters
datelong- The date at which the offset is calculated.
Returns
Millisecond offset from GMT in the current timezone for the given date.
inDaylightTime
public boolean inDaylightTime(Date date)Checks whether the given date is in daylight savings time for the given date.
Parameters
dateDate- the date to check
Returns
True if date is in daylight savings time
isSameYear
public boolean isSameYear(Calendar c1, Calendar c2)Checks if two times are on the same year using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same year.
isSameYear
public boolean isSameYear(Date d1, Date d2)Checks if two dates are on the same year in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same year.
isSameMonth
public boolean isSameMonth(Calendar c1, Calendar c2)Checks if two times are on the same month using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same month.
isSameMonth
public boolean isSameMonth(Date d1, Date d2)Checks if two dates are on the same month in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same month.
isSameDay
public boolean isSameDay(Calendar c1, Calendar c2)Checks if two times are on the same day using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same day.
isSameDay
public boolean isSameDay(Date d1, Date d2)Checks if two dates are on the same day in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same day.
isSameHour
public boolean isSameHour(Calendar c1, Calendar c2)Checks if two times are on the same hour using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same hour.
isSameHour
public boolean isSameHour(Date d1, Date d2)Checks if two dates are on the same hour in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same hour.
isSameMinute
public boolean isSameMinute(Calendar c1, Calendar c2)Checks if two times are on the same minute using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same minute.
isSameMinute
public boolean isSameMinute(Date d1, Date d2)Checks if two dates are on the same minute in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same minute.
isSameSecond
public boolean isSameSecond(Calendar c1, Calendar c2)Checks if two times are on the same second using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same second.
isSameSecond
public boolean isSameSecond(Date d1, Date d2)Checks if two dates are on the same second in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same second.
isSameTime
public boolean isSameTime(Calendar c1, Calendar c2)Checks if two times are on the same time using
Calendar.Parameters
c1Calendar- First time to compare.
c2Calendar- Second time to compare.
Returns
True if the two times are on the same time.
isSameTime
public boolean isSameTime(Date d1, Date d2)Checks if two dates are on the same time in the current timezone.
Parameters
d1Date- First date to compare.
d2Date- Second date to compare.
Returns
True if the two dates are on the same time.
getTimeAgo
public String getTimeAgo(Date date)Gets the date in “time ago” format. E.g. “Just now”, or “1 day ago”, etc..
Parameters
dateDate- The date
Returns
String representing how long ago from now the given date is.