Class DateUtil

java.lang.Object
org.assertj.core.util.DateUtil

public class DateUtil extends Object
Utility methods related to dates.
Author:
Joel Costigliola, Mikhail Mazursky
  • Constructor Details Link icon

    • DateUtil Link icon

      public DateUtil()
  • Method Details Link icon

    • newIsoDateFormat Link icon

      public static DateFormat newIsoDateFormat()
      ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23
      Returns:
      a yyyy-MM-dd DateFormat
    • newIsoDateTimeWithIsoTimeZoneFormat Link icon

      public static DateFormat newIsoDateTimeWithIsoTimeZoneFormat()
      ISO 8601 date-time format with ISO time zone (yyyy-MM-dd'T'HH:mm:ssX), example : 2003-04-26T03:01:02+00:00
      Returns:
      a yyyy-MM-dd'T'HH:mm:ssX DateFormat
    • newIsoDateTimeFormat Link icon

      public static DateFormat newIsoDateTimeFormat()
      ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss DateFormat
    • newIsoDateTimeWithMsFormat Link icon

      public static DateFormat newIsoDateTimeWithMsFormat()
      ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat
    • newIsoDateTimeWithMsAndIsoTimeZoneFormat Link icon

      public static DateFormat newIsoDateTimeWithMsAndIsoTimeZoneFormat()
      ISO 8601 date-time format with millisecond and ISO time zone (yyyy-MM-dd'T'HH:mm:ss.SSSX), example : 2003-04-26T03:01:02.758+00:00
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss.SSSX DateFormat
    • newTimestampDateFormat Link icon

      public static DateFormat newTimestampDateFormat()
      Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999
      Returns:
      a yyyy-MM-dd HH:mm:ss.SSS DateFormat
    • newIsoDateFormat Link icon

      public static DateFormat newIsoDateFormat(boolean lenientParsing)
      ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd DateFormat
    • newIsoDateTimeWithIsoTimeZoneFormat Link icon

      public static DateFormat newIsoDateTimeWithIsoTimeZoneFormat(boolean lenientParsing)
      ISO 8601 date-time format with ISO time zone (yyyy-MM-dd'T'HH:mm:ssX), example : 2003-04-26T03:01:02+00:00
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd'T'HH:mm:ssX DateFormat
    • newIsoDateTimeFormat Link icon

      public static DateFormat newIsoDateTimeFormat(boolean lenientParsing)
      ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss DateFormat
    • newIsoDateTimeWithMsFormat Link icon

      public static DateFormat newIsoDateTimeWithMsFormat(boolean lenientParsing)
      ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat
    • newIsoDateTimeWithMsAndIsoTimeZoneFormat Link icon

      public static DateFormat newIsoDateTimeWithMsAndIsoTimeZoneFormat(boolean lenientParsing)
      ISO 8601 date-time format with millisecond and ISO time zone (yyyy-MM-dd'T'HH:mm:ss.SSSX), example : 2003-04-26T03:01:02.758+00:00
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd'T'HH:mm:ss.SSSX DateFormat
    • newTimestampDateFormat Link icon

      public static DateFormat newTimestampDateFormat(boolean lenientParsing)
      Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999
      Parameters:
      lenientParsing - whether or not parsing the date is lenient
      Returns:
      a yyyy-MM-dd HH:mm:ss.SSS DateFormat
    • formatAsDatetime Link icon

      public static String formatAsDatetime(Date date)
      Formats the given date using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
      Method is synchronized because SimpleDateFormat is not thread safe (sigh).

      Returns null if given the date is null.

      Parameters:
      date - the date to format.
      Returns:
      the formatted date or null if given the date was null.
    • formatAsDatetimeWithMs Link icon

      public static String formatAsDatetimeWithMs(Date date)
      Formats the given date using the ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss:SSS).
      Method is synchronized because SimpleDateFormat is not thread safe (sigh).

      Returns null if given the date is null.

      Parameters:
      date - the date to format.
      Returns:
      the formatted date or null if given the date was null.
    • formatAsDatetime Link icon

      public static String formatAsDatetime(Calendar calendar)
      Formats the date of the given calendar using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
      Method is thread safe.

      Returns null if the given calendar is null.

      Parameters:
      calendar - the calendar to format.
      Returns:
      the formatted calendar or null if the given calendar was null.
    • parse Link icon

      public static Date parse(String dateAsString)
      Utility method to parse a Date following ISO_DATE_FORMAT, returns null if the given String is null.
      Parameters:
      dateAsString - the string to parse as a Date following ISO_DATE_FORMAT
      Returns:
      the corresponding Date or null if the given String is null.
      Throws:
      RuntimeException - encapsulating ParseException if the string can't be parsed as a Date
    • parseDatetime Link icon

      public static Date parseDatetime(String dateAsString)
      Utility method to parse a Date following ISO_DATE_TIME_FORMAT, returns null if the given String is null.

      Example:

       Date date = parseDatetime("2003-04-26T03:01:02");
      Parameters:
      dateAsString - the string to parse as a Date following ISO_DATE_TIME_FORMAT
      Returns:
      the corresponding Date with time details or null if the given String is null.
      Throws:
      RuntimeException - encapsulating ParseException if the string can't be parsed as a Date
    • parseDatetimeWithMs Link icon

      public static Date parseDatetimeWithMs(String dateAsString)
      Utility method to parse a Date following ISO_DATE_TIME_FORMAT_WITH_MS, returns null if the given String is null.

      Example:

       Date date = parseDatetimeWithMs("2003-04-26T03:01:02.999");
      Parameters:
      dateAsString - the string to parse as a Date following ISO_DATE_TIME_FORMAT_WITH_MS
      Returns:
      the corresponding Date with time details or null if the given String is null.
      Throws:
      RuntimeException - encapsulating ParseException if the string can't be parsed as a Date
    • toCalendar Link icon

      public static Calendar toCalendar(Date date)
      Converts the given Date to Calendar, returns null if the given Date is null.
      Parameters:
      date - the date to convert to a Calendar.
      Returns:
      the Calendar corresponding to the given Date or null if the given Date is null.
    • yearOf Link icon

      public static int yearOf(Date date)
      Extracts the year of the given Date.
      Parameters:
      date - the date to extract the year from - must not be null.
      Returns:
      the year of the given Date
      Throws:
      NullPointerException - if given Date is null
    • monthOf Link icon

      public static int monthOf(Date date)
      Dates Extracts the month of the given Date starting at 1 (January=1, February=2, ...).
      Parameters:
      date - the date to extract the month from - must not be null.
      Returns:
      the month of the given Date starting at 1 (January=1, February=2, ...)
      Throws:
      NullPointerException - if given Date is null
    • dayOfMonthOf Link icon

      public static int dayOfMonthOf(Date date)
      Dates Extracts the day of month of the given Date.
      Parameters:
      date - the date to extract the day of month from - must not be null.
      Returns:
      the day of month of the given Date
      Throws:
      NullPointerException - if given Date is null
    • dayOfWeekOf Link icon

      public static int dayOfWeekOf(Date date)
      Extracts the day of week of the given Date, returned value follows Calendar.DAY_OF_WEEK .
      Parameters:
      date - the date to extract the day of week from - must not be null.
      Returns:
      the day of week of the given Date
      Throws:
      NullPointerException - if given Date is null
    • hourOfDayOf Link icon

      public static int hourOfDayOf(Date date)
      Extracts the hour of day if the given Date (24-hour clock).
      Parameters:
      date - the date to extract the hour of day from - must not be null.
      Returns:
      the hour of day of the given Date (24-hour clock)
      Throws:
      NullPointerException - if given Date is null
    • minuteOf Link icon

      public static int minuteOf(Date date)
      Dates Extracts the minute of the given Date.
      Parameters:
      date - the date to extract the minute from - must not be null.
      Returns:
      the minute of the given Date
      Throws:
      NullPointerException - if given Date is null
    • secondOf Link icon

      public static int secondOf(Date date)
      Extracts the second of the given Date.
      Parameters:
      date - the date to extract the second from - must not be null.
      Returns:
      the second of the given Date
      Throws:
      NullPointerException - if given Date is null
    • millisecondOf Link icon

      public static int millisecondOf(Date date)
      Extracts the millisecond of the given Date.
      Parameters:
      date - the date to extract the millisecond from - must not be null.
      Returns:
      the millisecond of the given Date
      Throws:
      NullPointerException - if given Date is null
    • timeDifference Link icon

      public static long timeDifference(Date date1, Date date2)
      Compute the time difference between the two given dates in milliseconds, it always gives a positive result.
      Parameters:
      date1 - the first date.
      date2 - the second date.
      Returns:
      the difference between the two given dates in milliseconds
      Throws:
      IllegalArgumentException - if one a the given Date is null.
    • truncateTime Link icon

      public static Date truncateTime(Date date)
      Returns a copy of the given date without the time part (which is set to 00:00:00), for example :
      truncateTime(2008-12-29T23:45:12) will give 2008-12-29T00:00:00.

      Returns null if the given Date is null.

      Parameters:
      date - we want to get the day part (the parameter is read only).
      Returns:
      the truncated date.
    • now Link icon

      public static Date now()
    • yesterday Link icon

      public static Date yesterday()
    • tomorrow Link icon

      public static Date tomorrow()
    • formatTimeDifference Link icon

      public static String formatTimeDifference(Date date1, Date date2)
      Utility method to display a human readable time difference.
      Parameters:
      date1 - the first date
      date2 - the second date
      Returns:
      a human readable time difference.