public class DateUtil

  1. Object
  2. DateUtil
Utility class for working with dates and timezones.

Fields

public static final long MILLISECOND = 1L
public static final long SECOND = 1000L
public static final long MINUTE = 60000L
public static final long HOUR = 3600000L
public static final long DAY = 86400000L
public static final long MONTH = 2629800000L
public static final long YEAR = 31557600000L

Constructors

public DateUtil(TimeZone tz)Constructor for timezone.
public DateUtil()Creates DateUtil object in default timezone.

Methods

public static Date min(Date... dates)Returns the earliest of a set of dates.
public static int compare(Date d1, Date d2)Compares two dates.
public static Comparator<Date> compareByDateField(long field)Compares two dates or sorts multiple dates by the granularity of a specific field.
public static Date max(Date... dates)Returns the latest of a set of dates.
public int getOffset(long date)Gets the offset from GMT in milliseconds for the given date.
public boolean inDaylightTime(Date date)Checks whether the given date is in daylight savings time for the given date.
public boolean isSameYear(Calendar c1, Calendar c2)Checks if two times are on the same year using Calendar.
public boolean isSameYear(Date d1, Date d2)Checks if two dates are on the same year in the current timezone.
public boolean isSameMonth(Calendar c1, Calendar c2)Checks if two times are on the same month using Calendar.
public boolean isSameMonth(Date d1, Date d2)Checks if two dates are on the same month in the current timezone.
public boolean isSameDay(Calendar c1, Calendar c2)Checks if two times are on the same day using Calendar.
public boolean isSameDay(Date d1, Date d2)Checks if two dates are on the same day in the current timezone.
public boolean isSameHour(Calendar c1, Calendar c2)Checks if two times are on the same hour using Calendar.
public boolean isSameHour(Date d1, Date d2)Checks if two dates are on the same hour in the current timezone.
public boolean isSameMinute(Calendar c1, Calendar c2)Checks if two times are on the same minute using Calendar.
public boolean isSameMinute(Date d1, Date d2)Checks if two dates are on the same minute in the current timezone.
public boolean isSameSecond(Calendar c1, Calendar c2)Checks if two times are on the same second using Calendar.
public boolean isSameSecond(Date d1, Date d2)Checks if two dates are on the same second in the current timezone.
public boolean isSameTime(Calendar c1, Calendar c2)Checks if two times are on the same time using Calendar.
public boolean isSameTime(Date d1, Date d2)Checks if two dates are on the same time in the current timezone.
public String getTimeAgo(Date date)Gets the date in “time ago” format.

Inherited methods

Field details

MILLISECOND

public static final long MILLISECOND = 1L

SECOND

public static final long SECOND = 1000L

MINUTE

public static final long MINUTE = 60000L

HOUR

public static final long HOUR = 3600000L

DAY

public static final long DAY = 86400000L

MONTH

public static final long MONTH = 2629800000L

YEAR

public static final long YEAR = 31557600000L

Constructor details

DateUtil

public DateUtil(TimeZone tz)
Constructor for timezone.

Parameters

tz TimeZone
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

d1 Date
A date
d2 Date
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

field long
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

date long
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

date Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

c1 Calendar
First time to compare.
c2 Calendar
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

d1 Date
First date to compare.
d2 Date
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

date Date
The date

Returns

String representing how long ago from now the given date is.