Top Description Fields Constructors Methods
org.assertj.core.util

public Class DateUtil

extends Object
Class Inheritance
Static Imports
java.lang.String.format, java.util.concurrent.TimeUnit.DAYS, .TimeUnit.HOURS, .TimeUnit.MINUTES, .TimeUnit.SECONDS, org.assertj.core.util.Preconditions.checkArgument

Utility methods related to dates.
Authors
Joel Costigliola, Mikhail Mazursky

Field Summary

Modifier and TypeField and Description
private static final DateFormat
ISO_DATE_FORMAT

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

private static final DateFormat
ISO_DATE_TIME_FORMAT

ISO 8601 local date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02

private static final DateFormat
ISO_DATE_TIME_FORMAT_WITH_MS

ISO 8601 local date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static DateFormat
dateFormatForPattern(String pattern, boolean lenient)

public static int

Returns:

the day of month of the given Date
dayOfMonthOf
(Date
the date to extract the day of month from - must not be null.
date
)

Dates Extracts the day of month of the given Date.

public static int

Returns:

the day of week of the given Date
dayOfWeekOf
(Date
the date to extract the day of week from - must not be null.
date
)

Extracts the day of week of the given Date, returned value follows Calendar#DAY_OF_WEEK .

public static synchronized String

Returns:

the formatted date or null if given the date was null.
formatAsDatetime
(Date
the date to format.
date
)

Formats the given date using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).

public static String

Returns:

the formatted calendar or null if the given calendar was null.
formatAsDatetime
(Calendar
the calendar to format.
calendar
)

Formats the date of the given calendar using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).

public static synchronized String

Returns:

the formatted date or null if given the date was null.
formatAsDatetimeWithMs
(Date
the date to format.
date
)

Formats the given date using the ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss:SSS).

public static String

Returns:

a human readable time difference.
formatTimeDifference
(final Date
the first date
date1
,
final Date
the second date
date2
)

Utility method to display a human readable time difference.

public static int

Returns:

the hour of day of the given Date (24-hour clock)
hourOfDayOf
(Date
the date to extract the hour of day from - must not be null.
date
)

Extracts the hour of day if the given Date (24-hour clock).

public static int

Returns:

the millisecond of the given Date
millisecondOf
(Date
the date to extract the millisecond from - must not be null.
date
)

Extracts the millisecond of the given Date.

public static int

Returns:

the minute of the given Date
minuteOf
(Date
the date to extract the minute from - must not be null.
date
)

Dates Extracts the minute of the given Date.

public static int

Returns:

the month of the given Date starting at 1 (January=1, February=2, ...)
monthOf
(Date
the date to extract the month from - must not be null.
date
)

Dates Extracts the month of the given Date starting at 1 (January=1, February=2, ...).

public static DateFormat

Returns:

a yyyy-MM-dd DateFormat
newIsoDateFormat
()

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

public static DateFormat

Returns:

a yyyy-MM-dd DateFormat
newIsoDateFormat
(boolean
whether or not parsing the date is lenient
lenientParsing
)

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss DateFormat
newIsoDateTimeFormat
()

ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss DateFormat
newIsoDateTimeFormat
(boolean
whether or not parsing the date is lenient
lenientParsing
)

ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ssX 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

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ssX DateFormat
newIsoDateTimeWithIsoTimeZoneFormat
(boolean
whether or not parsing the date is lenient
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

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss.SSSX 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

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss.SSSX DateFormat
newIsoDateTimeWithMsAndIsoTimeZoneFormat
(boolean
whether or not parsing the date is lenient
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

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat
newIsoDateTimeWithMsFormat
()

ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999

public static DateFormat

Returns:

a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat
newIsoDateTimeWithMsFormat
(boolean
whether or not parsing the date is lenient
lenientParsing
)

ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999

public static DateFormat

Returns:

a yyyy-MM-dd HH:mm:ss.SSS DateFormat
newTimestampDateFormat
()

java.sql.Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999

public static DateFormat

Returns:

a yyyy-MM-dd HH:mm:ss.SSS DateFormat
newTimestampDateFormat
(boolean
whether or not parsing the date is lenient
lenientParsing
)

java.sql.Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999

public static Date
now()

public static synchronized Date

Returns:

the corresponding Date or null if the given String is null.
parse
(String
the string to parse as a Date following ISO_DATE_FORMAT
dateAsString
)

Utility method to parse a Date following ISO_DATE_FORMAT, returns null if the given String is null.

public static synchronized Date

Returns:

the corresponding Date with time details or null if the given String is null.
parseDatetime
(String
the string to parse as a Date following ISO_DATE_TIME_FORMAT
dateAsString
)

Utility method to parse a Date following ISO_DATE_TIME_FORMAT, returns null if the given String is null.

public static synchronized Date

Returns:

the corresponding Date with time details or null if the given String is null.
parseDatetimeWithMs
(String
the string to parse as a Date following ISO_DATE_TIME_FORMAT_WITH_MS
dateAsString
)

Utility method to parse a Date following ISO_DATE_TIME_FORMAT_WITH_MS, returns null if the given String is null.

public static int

Returns:

the second of the given Date
secondOf
(Date
the date to extract the second from - must not be null.
date
)

Extracts the second of the given Date.

public static long

Returns:

the difference between the two given dates in milliseconds
timeDifference
(Date
the first date.
date1
,
Date
the second date.
date2
)

Compute the time difference between the two given dates in milliseconds, it always gives a positive result.

public static Calendar

Returns:

the Calendar corresponding to the given Date or null if the given Date is null.
toCalendar
(Date
the date to convert to a Calendar.
date
)

Converts the given Date to Calendar, returns null if the given Date is null.

public static Date
public static Date

Returns:

the truncated date.
truncateTime
(Date
we want to get the day part (the parameter is read only).
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.

public static int

Returns:

the year of the given Date
yearOf
(Date
the date to extract the year from - must not be null.
date
)

Extracts the year of the given Date.

public static Date
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ISO_DATE_FORMATback to summary
private static final DateFormat ISO_DATE_FORMAT

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

ISO_DATE_TIME_FORMATback to summary
private static final DateFormat ISO_DATE_TIME_FORMAT

ISO 8601 local date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02

ISO_DATE_TIME_FORMAT_WITH_MSback to summary
private static final DateFormat ISO_DATE_TIME_FORMAT_WITH_MS

ISO 8601 local date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999

Constructor Detail

DateUtilback to summary
public DateUtil()

Method Detail

dateFormatForPatternback to summary
private static DateFormat dateFormatForPattern(String pattern, boolean lenient)
dayOfMonthOfback to summary
public static int dayOfMonthOf(Date date)

Dates Extracts the day of month of the given Date.

Parameters
date:Date

the date to extract the day of month from - must not be null.

Returns:int

the day of month of the given Date

Exceptions
NullPointerException:
if given Date is null
dayOfWeekOfback to summary
public static int dayOfWeekOf(Date date)

Extracts the day of week of the given Date, returned value follows Calendar#DAY_OF_WEEK .

Parameters
date:Date

the date to extract the day of week from - must not be null.

Returns:int

the day of week of the given Date

Exceptions
NullPointerException:
if given Date is null
formatAsDatetimeback to summary
public static synchronized 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:Date

the date to format.

Returns:String

the formatted date or null if given the date was null.

formatAsDatetimeback to summary
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:Calendar

the calendar to format.

Returns:String

the formatted calendar or null if the given calendar was null.

formatAsDatetimeWithMsback to summary
public static synchronized 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:Date

the date to format.

Returns:String

the formatted date or null if given the date was null.

formatTimeDifferenceback to summary
public static String formatTimeDifference(final Date date1, final Date date2)

Utility method to display a human readable time difference.

Parameters
date1:Date

the first date

date2:Date

the second date

Returns:String

a human readable time difference.

hourOfDayOfback to summary
public static int hourOfDayOf(Date date)

Extracts the hour of day if the given Date (24-hour clock).

Parameters
date:Date

the date to extract the hour of day from - must not be null.

Returns:int

the hour of day of the given Date (24-hour clock)

Exceptions
NullPointerException:
if given Date is null
millisecondOfback to summary
public static int millisecondOf(Date date)

Extracts the millisecond of the given Date.

Parameters
date:Date

the date to extract the millisecond from - must not be null.

Returns:int

the millisecond of the given Date

Exceptions
NullPointerException:
if given Date is null
minuteOfback to summary
public static int minuteOf(Date date)

Dates Extracts the minute of the given Date.

Parameters
date:Date

the date to extract the minute from - must not be null.

Returns:int

the minute of the given Date

Exceptions
NullPointerException:
if given Date is null
monthOfback to summary
public static int monthOf(Date date)

Dates Extracts the month of the given Date starting at 1 (January=1, February=2, ...).

Parameters
date:Date

the date to extract the month from - must not be null.

Returns:int

the month of the given Date starting at 1 (January=1, February=2, ...)

Exceptions
NullPointerException:
if given Date is null
newIsoDateFormatback to summary
public static DateFormat newIsoDateFormat()

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

Returns:DateFormat

a yyyy-MM-dd DateFormat

newIsoDateFormatback to summary
public static DateFormat newIsoDateFormat(boolean lenientParsing)

ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23

Parameters
lenientParsing:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd DateFormat

newIsoDateTimeFormatback to summary
public static DateFormat newIsoDateTimeFormat()

ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02

Returns:DateFormat

a yyyy-MM-dd'T'HH:mm:ss DateFormat

newIsoDateTimeFormatback to summary
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:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd'T'HH:mm:ss DateFormat

newIsoDateTimeWithIsoTimeZoneFormatback to summary
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:DateFormat

a yyyy-MM-dd'T'HH:mm:ssX DateFormat

newIsoDateTimeWithIsoTimeZoneFormatback to summary
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:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd'T'HH:mm:ssX DateFormat

newIsoDateTimeWithMsAndIsoTimeZoneFormatback to summary
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:DateFormat

a yyyy-MM-dd'T'HH:mm:ss.SSSX DateFormat

newIsoDateTimeWithMsAndIsoTimeZoneFormatback to summary
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:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd'T'HH:mm:ss.SSSX DateFormat

newIsoDateTimeWithMsFormatback to summary
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:DateFormat

a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat

newIsoDateTimeWithMsFormatback to summary
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:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd'T'HH:mm:ss.SSS DateFormat

newTimestampDateFormatback to summary
public static DateFormat newTimestampDateFormat()

java.sql.Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999

Returns:DateFormat

a yyyy-MM-dd HH:mm:ss.SSS DateFormat

newTimestampDateFormatback to summary
public static DateFormat newTimestampDateFormat(boolean lenientParsing)

java.sql.Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example : 2003-04-26 03:01:02.999

Parameters
lenientParsing:boolean

whether or not parsing the date is lenient

Returns:DateFormat

a yyyy-MM-dd HH:mm:ss.SSS DateFormat

nowback to summary
public static Date now()
parseback to summary
public static synchronized Date parse(String dateAsString)

Utility method to parse a Date following ISO_DATE_FORMAT, returns null if the given String is null.

Parameters
dateAsString:String

the string to parse as a Date following ISO_DATE_FORMAT

Returns:Date

the corresponding Date or null if the given String is null.

Exceptions
RuntimeException:
encapsulating ParseException if the string can't be parsed as a Date
parseDatetimeback to summary
public static synchronized 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:String

the string to parse as a Date following ISO_DATE_TIME_FORMAT

Returns:Date

the corresponding Date with time details or null if the given String is null.

Exceptions
RuntimeException:
encapsulating ParseException if the string can't be parsed as a Date
parseDatetimeWithMsback to summary
public static synchronized 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:String

the string to parse as a Date following ISO_DATE_TIME_FORMAT_WITH_MS

Returns:Date

the corresponding Date with time details or null if the given String is null.

Exceptions
RuntimeException:
encapsulating ParseException if the string can't be parsed as a Date
secondOfback to summary
public static int secondOf(Date date)

Extracts the second of the given Date.

Parameters
date:Date

the date to extract the second from - must not be null.

Returns:int

the second of the given Date

Exceptions
NullPointerException:
if given Date is null
timeDifferenceback to summary
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:Date

the first date.

date2:Date

the second date.

Returns:long

the difference between the two given dates in milliseconds

Exceptions
IllegalArgumentException:
if one a the given Date is null.
toCalendarback to summary
public static Calendar toCalendar(Date date)

Converts the given Date to Calendar, returns null if the given Date is null.

Parameters
date:Date

the date to convert to a Calendar.

Returns:Calendar

the Calendar corresponding to the given Date or null if the given Date is null.

tomorrowback to summary
public static Date tomorrow()
truncateTimeback to summary
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:Date

we want to get the day part (the parameter is read only).

Returns:Date

the truncated date.

yearOfback to summary
public static int yearOf(Date date)

Extracts the year of the given Date.

Parameters
date:Date

the date to extract the year from - must not be null.

Returns:int

the year of the given Date

Exceptions
NullPointerException:
if given Date is null
yesterdayback to summary
public static Date yesterday()