Package org.fest.util
Class Dates
- java.lang.Object
-
- org.fest.util.Dates
-
public class Dates extends java.lang.Object
Utility methods related to dates.- Author:
- Joel Costigliola
-
-
Field Summary
Fields Modifier and Type Field Description static java.text.DateFormat
ISO_DATE_FORMAT
ISO 8601 date format (yyyy-MM-dd), example :2003-04-23
static java.text.DateFormat
ISO_DATE_TIME_FORMAT
ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example :2003-04-26T13:01:02
static java.text.DateFormat
ISO_DATE_TIME_FORMAT_WITH_MS
ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example :2003-04-26T03:01:02.999
-
Constructor Summary
Constructors Constructor Description Dates()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
dayOfMonthOf(java.util.Date date)
Dates Extracts the day of month of the given Date.static int
dayOfWeekOf(java.util.Date date)
Extracts the day of week of the given Date, returned value followsCalendar.DAY_OF_WEEK
.static java.lang.String
formatAsDatetime(java.util.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.static java.lang.String
formatAsDatetime(java.util.Date date)
Formats the given date using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
Method in synchronized because SimpleDateFormat is not thread safe (sigh).static java.lang.String
formatAsDatetimeWithMs(java.util.Date date)
Formats the given date using the ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss:SSS).
Method in synchronized because SimpleDateFormat is not thread safe (sigh).static int
hourOfDay(java.util.Date date)
Extracts the hour of day if the given Date (24-hour clock).static int
millisecondOf(java.util.Date date)
Extracts the millisecond of the given Date.static int
minuteOf(java.util.Date date)
Dates Extracts the minute of the given Date.static int
monthOf(java.util.Date date)
Dates Extracts the month of the given Date starting at 1 (January=1, February=2, ...).static java.util.Date
parse(java.lang.String dateAsString)
Utility method to parse a Date followingISO_DATE_FORMAT
, returns null if the given String is null.static java.util.Date
parseDatetime(java.lang.String dateAsString)
Utility method to parse a Date followingISO_DATE_TIME_FORMAT
, returns null if the given String is null.static int
secondOf(java.util.Date date)
Extracts the second of the given Date.static java.util.Calendar
toCalendar(java.util.Date date)
Converts the given Date to Calendar, returns null if the given Date is null.static java.util.Date
today()
static java.util.Date
tomorrow()
static java.util.Date
truncateTime(java.util.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 give2008-12-29T00:00:00
.static int
yearOf(java.util.Date date)
Extracts the year of the given Date.static java.util.Date
yesterday()
-
-
-
Field Detail
-
ISO_DATE_FORMAT
public static final java.text.DateFormat ISO_DATE_FORMAT
ISO 8601 date format (yyyy-MM-dd), example :2003-04-23
-
ISO_DATE_TIME_FORMAT
public static final java.text.DateFormat ISO_DATE_TIME_FORMAT
ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example :2003-04-26T13:01:02
-
ISO_DATE_TIME_FORMAT_WITH_MS
public static final java.text.DateFormat ISO_DATE_TIME_FORMAT_WITH_MS
ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example :2003-04-26T03:01:02.999
-
-
Method Detail
-
formatAsDatetime
public static java.lang.String formatAsDatetime(java.util.Date date)
Formats the given date using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
Method in 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
public static java.lang.String formatAsDatetimeWithMs(java.util.Date date)
Formats the given date using the ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss:SSS).
Method in 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
public static java.lang.String formatAsDatetime(java.util.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
public static java.util.Date parse(java.lang.String dateAsString)
Utility method to parse a Date followingISO_DATE_FORMAT
, returns null if the given String is null.- Parameters:
dateAsString
- the string to parse as a Date followingISO_DATE_FORMAT
- Returns:
- the corrresponding Date or null if the given String is null.
- Throws:
java.lang.RuntimeException
- encapsulating ParseException if the string can't be parsed as a Date
-
parseDatetime
public static java.util.Date parseDatetime(java.lang.String dateAsString)
Utility method to parse a Date followingISO_DATE_TIME_FORMAT
, returns null if the given String is null.- Parameters:
dateAsString
- the string to parse as a Date followingISO_DATE_TIME_FORMAT
- Returns:
- the corrresponding Date with time details or null if the given String is null.
- Throws:
java.lang.RuntimeException
- encapsulating ParseException if the string can't be parsed as a Date
-
toCalendar
public static java.util.Calendar toCalendar(java.util.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
public static int yearOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
monthOf
public static int monthOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
dayOfMonthOf
public static int dayOfMonthOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
dayOfWeekOf
public static int dayOfWeekOf(java.util.Date date)
Extracts the day of week of the given Date, returned value followsCalendar.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:
java.lang.NullPointerException
- if given Date is null
-
hourOfDay
public static int hourOfDay(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
minuteOf
public static int minuteOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
secondOf
public static int secondOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
millisecondOf
public static int millisecondOf(java.util.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:
java.lang.NullPointerException
- if given Date is null
-
truncateTime
public static java.util.Date truncateTime(java.util.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 give2008-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.
-
today
public static java.util.Date today()
-
yesterday
public static java.util.Date yesterday()
-
tomorrow
public static java.util.Date tomorrow()
-
-