# Class DateUtil

java.lang.Object
com.codename1.util.DateUtil

public class DateUtil extends Object
Utility class for working with dates and timezones.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final long`
  `DAY`
  `static final long`
  `HOUR`
  `static final long`
  `MILLISECOND`
  `static final long`
  `MINUTE`
  `static final long`
  `MONTH`
  `static final long`
  `SECOND`
  `static final long`
  `YEAR`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DateUtil()`
  Creates DateUtil object in default timezone.
  `DateUtil(TimeZone tz)`
  Constructor for timezone.

- ## Method Summary

  Modifier and Type
  Method
  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.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### MILLISECOND

    public static final long MILLISECOND
    See Also:
      - Constant Field Values

  - ### SECOND

    public static final long SECOND
    See Also:
      - Constant Field Values

  - ### MINUTE

    public static final long MINUTE
    See Also:
      - Constant Field Values

  - ### HOUR

    public static final long HOUR
    See Also:
      - Constant Field Values

  - ### DAY

    public static final long DAY
    See Also:
      - Constant Field Values

  - ### MONTH

    public static final long MONTH
    See Also:
      - Constant Field Values

  - ### YEAR

    public static final long YEAR
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### DateUtil

    public DateUtil(TimeZone tz)

    Constructor for timezone.

    ###### Parameters

    - `tz`: 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.

    ###### Parameters

    - `dates`

    ###### Returns

    The earliest of a set of dates.

    ###### Since

    6.0

  - ### compare

    public static int compare(Date d1, Date d2)

    Compares two dates.

    ###### Parameters

    - `d1`: A date

    - `d2`: A date

    ###### Returns

    -1 if first date is earlier. 1 if first date is later. 0 if they are the same.

    ###### Since

    6.0

  - ### 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`: @param field One of the fields:

    - `DateUtil.MILLISECOND`

    - `DateUtil.SECOND`

    - `DateUtil.MINUTE`

    - `DateUtil.HOUR`

    - `DateUtil.DATE`

    - `DateUtil.MONTH`

    - `DateUtil.YEAR`

    ###### Returns

    Returns:

      - > 0 - if first date is earlier.

      - < 0 - if first date is later.

      - == 0 - if they are equal.

      ###### Since

      7.0

  - ### max

    public static Date max(Date... dates)

    Returns the latest of a set of dates.

    ###### Parameters

    - `dates`

    ###### Returns

    The latest of a set of dates.

    ###### Since

    6.0

  - ### getOffset

    public int getOffset(long date)

    Gets the offset from GMT in milliseconds for the given date.

    ###### Parameters

    - `date`: 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`: 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`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same year.

    ###### Since

    7.0

  - ### isSameYear

    public boolean isSameYear(Date d1, Date d2)

    Checks if two dates are on the same year in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same year.

    ###### Since

    7.0

  - ### isSameMonth

    public boolean isSameMonth(Calendar c1, Calendar c2)

    Checks if two times are on the same month using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same month.

    ###### Since

    7.0

  - ### isSameMonth

    public boolean isSameMonth(Date d1, Date d2)

    Checks if two dates are on the same month in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same month.

    ###### Since

    7.0

  - ### isSameDay

    public boolean isSameDay(Calendar c1, Calendar c2)

    Checks if two times are on the same day using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same day.

    ###### Since

    7.0

  - ### isSameDay

    public boolean isSameDay(Date d1, Date d2)

    Checks if two dates are on the same day in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same day.

    ###### Since

    7.0

  - ### isSameHour

    public boolean isSameHour(Calendar c1, Calendar c2)

    Checks if two times are on the same hour using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same hour.

    ###### Since

    7.0

  - ### isSameHour

    public boolean isSameHour(Date d1, Date d2)

    Checks if two dates are on the same hour in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same hour.

    ###### Since

    7.0

  - ### isSameMinute

    public boolean isSameMinute(Calendar c1, Calendar c2)

    Checks if two times are on the same minute using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same minute.

    ###### Since

    7.0

  - ### isSameMinute

    public boolean isSameMinute(Date d1, Date d2)

    Checks if two dates are on the same minute in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same minute.

    ###### Since

    7.0

  - ### isSameSecond

    public boolean isSameSecond(Calendar c1, Calendar c2)

    Checks if two times are on the same second using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same second.

    ###### Since

    7.0

  - ### isSameSecond

    public boolean isSameSecond(Date d1, Date d2)

    Checks if two dates are on the same second in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same second.

    ###### Since

    7.0

  - ### isSameTime

    public boolean isSameTime(Calendar c1, Calendar c2)

    Checks if two times are on the same time using `Calendar`.

    ###### Parameters

    - `c1`: First time to compare.

    - `c2`: Second time to compare.

    ###### Returns

    True if the two times are on the same time.

    ###### Since

    7.0

  - ### isSameTime

    public boolean isSameTime(Date d1, Date d2)

    Checks if two dates are on the same time in the current timezone.

    ###### Parameters

    - `d1`: First date to compare.

    - `d2`: Second date to compare.

    ###### Returns

    True if the two dates are on the same time.

    ###### Since

    7.0

  - ### getTimeAgo

    public String getTimeAgo(Date date)

    Gets the date in "time ago" format. E.g. "Just now", or "1 day ago", etc..

    ###### Parameters

    - `date`: The date

    ###### Returns

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

    ###### Since

    6.0