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
     
    static final long
     
    static final long
     
    static final long
     
    static final long
     
    static final long
     
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates DateUtil object in default timezone.
    Constructor for timezone.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compare(Date d1, Date d2)
    Compares two dates.
    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.
    Gets the date in "time ago" format.
    boolean
    Checks whether the given date is in daylight savings time for the given date.
    boolean
    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
    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
    Checks if two times are on the same minute using Calendar.
    boolean
    Checks if two dates are on the same minute in the current timezone.
    boolean
    Checks if two times are on the same month using Calendar.
    boolean
    Checks if two dates are on the same month in the current timezone.
    boolean
    Checks if two times are on the same second using Calendar.
    boolean
    Checks if two dates are on the same second in the current timezone.
    boolean
    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
    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, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
     
    boolean
    Indicates whether some other object is "equal to" this one.
    final Class
    Returns the runtime class of an object.
    int
    Returns a hash code value for the object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    Returns a string representation of the object.
    final void
    Causes current thread to wait until another thread invokes the method or the method for this object.
    final void
    wait(long timeout)
    Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
    final void
    wait(long timeout, int nanos)
    Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
  • Field Details

  • 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