public class DateUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static java.text.DateFormat |
ISO_DATE_FORMAT
ISO 8601 date format (yyyy-MM-dd), example :
2003-04-23 |
private static java.text.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 java.text.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 and Description |
---|
DateUtil() |
Modifier and Type | Method and 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 follows
Calendar.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 is 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 is synchronized because SimpleDateFormat is not thread safe (sigh). |
static java.lang.String |
formatTimeDifference(java.util.Date date1,
java.util.Date date2)
Utility method to display a human readable time difference.
|
static int |
hourOfDayOf(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.text.DateFormat |
newIsoDateFormat()
ISO 8601 date format (yyyy-MM-dd), example :
2003-04-23 |
static java.text.DateFormat |
newIsoDateTimeFormat()
ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example :
2003-04-26T13:01:02 |
static java.text.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 |
static java.text.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 |
static java.text.DateFormat |
newIsoDateTimeWithMsFormat()
ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example :
2003-04-26T03:01:02.999 |
static java.text.DateFormat |
newTimestampDateFormat()
Timestamp date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example :
2003-04-26 03:01:02.999 |
static java.util.Date |
now() |
static java.util.Date |
parse(java.lang.String dateAsString)
Utility method to parse a Date following
ISO_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 following
ISO_DATE_TIME_FORMAT , returns null if the given String is null. |
static java.util.Date |
parseDatetimeWithMs(java.lang.String dateAsString)
Utility method to parse a Date following
ISO_DATE_TIME_FORMAT_WITH_MS , returns null if the given String is
null. |
static int |
secondOf(java.util.Date date)
Extracts the second of the given Date.
|
private static java.text.DateFormat |
strictDateFormatForPattern(java.lang.String pattern) |
static long |
timeDifference(java.util.Date date1,
java.util.Date date2)
Compute the time difference between the two given dates in milliseconds, it always gives a positive result.
|
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 |
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 give 2008-12-29T00:00:00 . |
static int |
yearOf(java.util.Date date)
Extracts the year of the given Date.
|
static java.util.Date |
yesterday() |
private static final java.text.DateFormat ISO_DATE_FORMAT
2003-04-23
private static final java.text.DateFormat ISO_DATE_TIME_FORMAT
2003-04-26T13:01:02
private static final java.text.DateFormat ISO_DATE_TIME_FORMAT_WITH_MS
2003-04-26T03:01:02.999
public static java.text.DateFormat newIsoDateFormat()
2003-04-23
yyyy-MM-dd
DateFormat
public static java.text.DateFormat newIsoDateTimeWithIsoTimeZoneFormat()
2003-04-26T03:01:02+00:00
yyyy-MM-dd'T'HH:mm:ssX
DateFormat
public static java.text.DateFormat newIsoDateTimeFormat()
2003-04-26T13:01:02
yyyy-MM-dd'T'HH:mm:ss
DateFormat
public static java.text.DateFormat newIsoDateTimeWithMsFormat()
2003-04-26T03:01:02.999
yyyy-MM-dd'T'HH:mm:ss.SSS
DateFormat
public static java.text.DateFormat newIsoDateTimeWithMsAndIsoTimeZoneFormat()
2003-04-26T03:01:02.758+00:00
yyyy-MM-dd'T'HH:mm:ss.SSSX
DateFormat
public static java.text.DateFormat newTimestampDateFormat()
Timestamp
date-time format with millisecond (yyyy-MM-dd HH:mm:ss.SSS), example :
2003-04-26 03:01:02.999
yyyy-MM-dd HH:mm:ss.SSS
DateFormat
private static java.text.DateFormat strictDateFormatForPattern(java.lang.String pattern)
public static java.lang.String formatAsDatetime(java.util.Date date)
Returns null if given the date is null.
date
- the date to format.public static java.lang.String formatAsDatetimeWithMs(java.util.Date date)
Returns null if given the date is null.
date
- the date to format.public static java.lang.String formatAsDatetime(java.util.Calendar calendar)
Returns null if the given calendar is null.
calendar
- the calendar to format.public static java.util.Date parse(java.lang.String dateAsString)
ISO_DATE_FORMAT
, returns null if the given String is null.dateAsString
- the string to parse as a Date following ISO_DATE_FORMAT
java.lang.RuntimeException
- encapsulating ParseException if the string can't be parsed as a Datepublic static java.util.Date parseDatetime(java.lang.String dateAsString)
ISO_DATE_TIME_FORMAT
, returns null if the given String is null.
Example:
Date date = parseDatetime("2003-04-26T03:01:02");
dateAsString
- the string to parse as a Date following ISO_DATE_TIME_FORMAT
java.lang.RuntimeException
- encapsulating ParseException if the string can't be parsed as a Datepublic static java.util.Date parseDatetimeWithMs(java.lang.String dateAsString)
ISO_DATE_TIME_FORMAT_WITH_MS
, returns null if the given String is
null.
Example:
Date date = parseDatetimeWithMs("2003-04-26T03:01:02.999");
dateAsString
- the string to parse as a Date following ISO_DATE_TIME_FORMAT_WITH_MS
java.lang.RuntimeException
- encapsulating ParseException if the string can't be parsed as a Datepublic static java.util.Calendar toCalendar(java.util.Date date)
date
- the date to convert to a Calendar.public static int yearOf(java.util.Date date)
date
- the date to extract the year from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int monthOf(java.util.Date date)
date
- the date to extract the month from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int dayOfMonthOf(java.util.Date date)
date
- the date to extract the day of month from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int dayOfWeekOf(java.util.Date date)
Calendar.DAY_OF_WEEK
.date
- the date to extract the day of week from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int hourOfDayOf(java.util.Date date)
date
- the date to extract the hour of day from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int minuteOf(java.util.Date date)
date
- the date to extract the minute from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int secondOf(java.util.Date date)
date
- the date to extract the second from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static int millisecondOf(java.util.Date date)
date
- the date to extract the millisecond from - must not be null.java.lang.NullPointerException
- if given Date is nullpublic static long timeDifference(java.util.Date date1, java.util.Date date2)
date1
- the first date.date2
- the second date.java.lang.IllegalArgumentException
- if one a the given Date is null.public static java.util.Date truncateTime(java.util.Date date)
truncateTime(2008-12-29T23:45:12)
will give 2008-12-29T00:00:00
.
Returns null if the given Date is null.
date
- we want to get the day part (the parameter is read only).public static java.util.Date now()
public static java.util.Date yesterday()
public static java.util.Date tomorrow()
public static java.lang.String formatTimeDifference(java.util.Date date1, java.util.Date date2)
date1
- the first datedate2
- the second date