Package com.jalios.util
Class DateUtil
java.lang.Object
com.jalios.util.DateUtil
Helper class to work on Dates and Calendars.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final Stringstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DateConverts given instant to a Date.static StringformatDuration(long time) Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s. Use default locale.static StringformatDuration(long time, Locale locale) Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 sstatic StringformatDuration(Date begin, Date end) Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s. Use default locale.static StringformatDuration(Date begin, Date end, Locale locale) Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 sstatic StringformatIso8601Date(Date date) Returns a date formated with the ISO 8601 Date Time format (eg1999-01-01T14:07:00+0100).
For more details, see http://en.wikipedia.org/wiki/ISO_8601
This method is thread safe.static StringformatNoSpaceDate(Date date) Returns a date formated with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe.static StringformatRfc822Date(Date date) Returns a date formated with the RFC 822 Date Time format (egFri, 1 Jan 1999 14:07:00 +0100).static StringformatW3cDate(Date date) Returns a date formated with the W3C Date Time format (eg1999-01-01T14:07:00+01:00).
For more details, see http://www.w3.org/TR/NOTE-datetime
This method is thread safe.static longReturns the current time without millis.static intExtract the day of month from the given date.static DategetDayEndDate(Date date, Locale locale) Get the date of the end of the day (23h59 59s 999 ms)static DategetDayStartDate(Date date, Locale locale) Returns the date corresponding to the day start date of a datestatic intgetDayValue(String sValue) Gets the day value according the string value in parameterstatic DategetFirstDateOfMonth(int month, int year) Gets aDateobject representing the first day for the given month and yearstatic DategetFirstDateOfNextMonth(int month, int year) Gets aDateobject representing the first day of the next month for the given month and yearstatic DategetFirstDateOfPreviousMonth(int month, int year) Gets aDateobject representing the first day of the previous month for the given month and yearstatic DategetFirstDateOfYear(int year) static DategetLastDateOfMonth(int month, int year) Gets aDateobject representing the last day for the given month and yearstatic DategetLastDateOfNextMonth(int month, int year) Gets aDateobject representing the last day of the next month for the given month and yearstatic DategetLastDateOfPreviousMonth(int month, int year) Gets aDateobject representing the last day of the previous month for the given month and yearstatic DategetLastDateOfYear(int year) static intExtract the month from the given date.static StringgetMonthDisplayName(int value, int style, Locale locale) Returns the string representation of the month in the given month value, style and locale.static intgetMonthValue(String sValue) Gets the month value according the string value in parameterstatic DateReturns the current date with milliseconds reset to 0.static Calendarstatic longgetTimeWithoutMillis(long time) Returns the given time without millis.static intExtract the year from the given date.static intgetYearValue(String sValue) Gets the year value according the string value in parameter or the current year if 'sValue' in parameter is empty.static ZonedDateTimegetZonedDateTime(Date date, ZoneId zoneId) Retrieve a ZonedDateTime in the specified zoneid, from the date specified.static booleanisCurrentYear(Date date) Check if a given date is in the same year as current yearstatic booleanCheck if the 2 given dates are the same day (from 0h00 included to 0h00 excluded)static booleanisToday(ZonedDateTime zdt) Check if a given ZonedDateTime is the same day as today in the same timezone of the specified date (from 0h00 included to 0h00 excluded)static booleanCheck if a given date is the same day as today (from 0h00 included to 0h00 excluded)static booleanisTomorrow(ZonedDateTime zdt) Check if a given ZonedDateTime is the same day as tomorrow in the same timezone of the specified date (from 0h00 included to 0h00 excluded)static booleanisTomorrow(Date date) Check if a given date is the same day as tomorrow (from 0h00 included to 0h00 excluded)static booleanisWeekEndDay(Date date, Locale locale) static booleanisYesterday(ZonedDateTime zdt) Check if a given ZonedDateTime is the same day as yesterday in the same timezone of the specified date (from 0h00 included to 0h00 excluded)static booleanisYesterday(Date date) Check if a given date is the same day as yesterday (from 0h00 included to 0h00 excluded)static voidmainTestFormatDuration(String[] args) static booleanoverlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate) Indicates if both intervals overlaps or notstatic booleanoverlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate, boolean greedy) Indicates if both intervals overlaps or notstatic DateparseIso8601Date(String source) Returns the date parsed from the given string with the ISO 8601 Date Time format (eg1999-01-01T14:07:00+0100).
For more details, see http://en.wikipedia.org/wiki/ISO_8601
This method is thread safe.static DateparseNoSpaceDate(String source) Returns the date parsed from the given string with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe.static DateparseRfc822Date(String source) Returns the date parsed from the given string with the RFC 822 Date Time format (eg "Fri, 1 Jan 1999 14:07:00 +0100").static DateparseW3cDate(String source) Parse the specified string as a W3C Date Time format.static voidrollAndAdd(Calendar calendar, int field, int amount) Do a roll on this calendar instance and add or remove one larger field if the roll action return to the boundary of the period Example roll 1 day for 31/12/2010 will give 01/01/2010.static longroundDuration(long duration) Round a duration in order to remove irrelevant information for duration which do not need to be accurate (usually when duration is formatted for the UI).
-
Field Details
-
PARAM_VALUE_ALL
- See Also:
-
VALUE_ALL
public static final int VALUE_ALL- See Also:
-
MAX_HOUR
public static final int MAX_HOUR- See Also:
-
MAX_MIN
public static final int MAX_MIN- See Also:
-
MAX_SEC
public static final int MAX_SEC- See Also:
-
MAX_MILLISEC
public static final int MAX_MILLISEC- See Also:
-
MIN_HOUR
public static final int MIN_HOUR- See Also:
-
MIN_MIN
public static final int MIN_MIN- See Also:
-
MIN_SEC
public static final int MIN_SEC- See Also:
-
MIN_MILLISEC
public static final int MIN_MILLISEC- See Also:
-
MIN_DAY
public static final int MIN_DAY- See Also:
-
-
Constructor Details
-
DateUtil
public DateUtil()
-
-
Method Details
-
overlapWith
public static boolean overlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate) Indicates if both intervals overlaps or not- Parameters:
startDate- the low boundary of the first intervalendDate- the high boundary of the first intervalsecondStartDate- the low boundary of the second intervalsecondEndDate- the high boundary of the second interval- Returns:
- true if both interval overlaps, false otherwise
-
overlapWith
public static boolean overlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate, boolean greedy) Indicates if both intervals overlaps or not- Parameters:
startDate- the low boundary of the first intervalendDate- the high boundary of the first intervalsecondStartDate- the low boundary of the second intervalsecondEndDate- the high boundary of the second intervalgreedy- true if the method shoudl return true when both interval overlap with only one boundary- Returns:
- true if both interval overlaps, false otherwise
-
formatDuration
Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s. Use default locale.- Parameters:
begin- the begin dateend- the end date- Returns:
- the formatted String
-
formatDuration
Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s- Parameters:
begin- the begin dateend- the end datelocale- the locale to use- Returns:
- the formatted String
-
formatDuration
Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s. Use default locale.- Parameters:
time- the duration to format (in milliseconds)- Returns:
- the formatted String
-
formatDuration
Format a duration (i.e split the given time into days, hours, minutes and seconds) E.g.: 124532923 -> 1 day 10 h 35 min 32 s- Parameters:
time- the duration to format (in milliseconds)locale- the locale to use- Returns:
- the formatted String
-
mainTestFormatDuration
-
roundDuration
public static long roundDuration(long duration) Round a duration in order to remove irrelevant information for duration which do not need to be accurate (usually when duration is formatted for the UI).As of current implementation, the following rules are applied
- Seconds are removed for duration longer than 5 minutes.
- Minutes are rounded for duration longer than 2 hours (to current hour if minutes are below 30, to next 1 hour if minutes above 30)
- Minutes are removed for duration longer than a day
- Hours are removed for duration longer than a week
Util.formatDuration(Util.roundDuration(38000)); // 38 s => 38 s Util.formatDuration(Util.roundDuration(76000)); // 1 min 16 s => 1 min 16 s Util.formatDuration(Util.roundDuration(1564512)); // 26 min 4 s => 26 min Util.formatDuration(Util.roundDuration(5890000)); // 1 h 38 min 10 s => 1 h 38 min Util.formatDuration(Util.roundDuration(33165000)); // 9 h 12 min 45 s => 9 h Util.formatDuration(Util.roundDuration(35025000)); // 9 h 43 min 45 s => 10 h Util.formatDuration(Util.roundDuration(130993000)); // 1 day 12 h 23 min 13 s => 1 day 12 h Util.formatDuration(Util.roundDuration(236158000)); // 2 days 17 h 35 min 58 s => 2 days 18 h Util.formatDuration(Util.roundDuration(731675000)); // 8 days 11 h 14 min 35 s => 8 days Util.formatDuration(Util.roundDuration(5934547000L)); // 68 days 16 h 29 min 7 s => 68 days
- Parameters:
duration- a duration in milliseconds- Returns:
- the duration rounded.
- Since:
- jcms-6.0.2
-
formatIso8601Date
Returns a date formated with the ISO 8601 Date Time format (eg1999-01-01T14:07:00+0100).
For more details, see http://en.wikipedia.org/wiki/ISO_8601
This method is thread safe.- Parameters:
date- the date to format.- Returns:
- a String with the formated date.
- Since:
- jcms-5.7.2
- See Also:
-
parseIso8601Date
Returns the date parsed from the given string with the ISO 8601 Date Time format (eg1999-01-01T14:07:00+0100).
For more details, see http://en.wikipedia.org/wiki/ISO_8601
This method is thread safe.- Parameters:
source- the string to be parsed- Returns:
- a Date or null if input could not be parsed
- Since:
- jcms-5.7.2
- See Also:
-
formatW3cDate
Returns a date formated with the W3C Date Time format (eg1999-01-01T14:07:00+01:00).
For more details, see http://www.w3.org/TR/NOTE-datetime
This method is thread safe.- Parameters:
date- the date to format.- Returns:
- a String with the formated date.
- See Also:
-
parseW3cDate
Parse the specified string as a W3C Date Time format.Supported format includes the following variations :
- Year:
YYYY (eg 1997) - Year and month:
YYYY-MM (eg 1997-07) - Complete date:
YYYY-MM-DD (eg 1997-07-16) - Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00) - Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00) - Complete date plus hours, minutes, seconds and a decimal fraction of a second:
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
YYYY = four-digit year MM = two-digit month (01=January, etc.) DD = two-digit day of month (01 through 31) hh = two digits of hour (00 through 23) (am/pm NOT allowed) mm = two digits of minute (00 through 59) ss = two digits of second (00 through 59) s = one or more digits representing a decimal fraction of a second TZD = time zone designator (Z or +hh:mm or -hh:mm)For full details, see http://www.w3.org/TR/NOTE-datetimeThis method is thread safe.
- Parameters:
source- the string to be parsed- Returns:
- a Date, may return null if input was invalid
- Since:
- jcms-5.7.2. Complete W3C date time support since jcms-10.0.0 with issue JCMS-5791
- See Also:
- Year:
-
formatRfc822Date
Returns a date formated with the RFC 822 Date Time format (egFri, 1 Jan 1999 14:07:00 +0100). For more details, see http://www.w3.org/Protocols/rfc822/rfc822.txt section 5.1
This method is thread safe.- Parameters:
date- the date to format.- Returns:
- a String with the formated date.
-
parseRfc822Date
Returns the date parsed from the given string with the RFC 822 Date Time format (eg "Fri, 1 Jan 1999 14:07:00 +0100"). http://www.w3.org/Protocols/rfc822/rfc822.txt section 5.1
This method is thread safe.- Parameters:
source- the string to be parsed- Returns:
- a Date
- Since:
- jcms-5.7.2
-
formatNoSpaceDate
Returns a date formated with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe.- Parameters:
date- the date to format.- Returns:
- a String with the formated date.
- Since:
- jcms-5.0.2
-
parseNoSpaceDate
Returns the date parsed from the given string with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe.- Parameters:
source- the string to be parsed- Returns:
- a Date
- Since:
- jcms-5.7.2
-
isToday
Check if a given date is the same day as today (from 0h00 included to 0h00 excluded)- Parameters:
date- the date to check- Returns:
- true if the given date is today
-
isToday
Check if a given ZonedDateTime is the same day as today in the same timezone of the specified date (from 0h00 included to 0h00 excluded)- Parameters:
zdt- the zoned date to check- Returns:
- true if the given date is today
- Since:
- jcms-10.0.1 / JCMS-4865
-
isYesterday
Check if a given date is the same day as yesterday (from 0h00 included to 0h00 excluded)- Parameters:
date- the date to check- Returns:
- true if the given date is yesterday
- Since:
- jcms-6.0.1
-
isYesterday
Check if a given ZonedDateTime is the same day as yesterday in the same timezone of the specified date (from 0h00 included to 0h00 excluded)- Parameters:
zdt- the zoned date to check- Returns:
- true if the given date is yesterday
- Since:
- jcms-10.0.1 / JCMS-4865
-
isTomorrow
Check if a given date is the same day as tomorrow (from 0h00 included to 0h00 excluded)- Parameters:
date- the date to check- Returns:
- true if the given date is tomorrow
- Since:
- jcms-6.0.1
-
isTomorrow
Check if a given ZonedDateTime is the same day as tomorrow in the same timezone of the specified date (from 0h00 included to 0h00 excluded)- Parameters:
zdt- the zoned date to check- Returns:
- true if the given date is tomorrow
- Since:
- jcms-10.0.1 / JCMS-4865
-
isSameDay
Check if the 2 given dates are the same day (from 0h00 included to 0h00 excluded)- Parameters:
date1- the first date to checkdate2- the second date to check- Returns:
- true if the given date is today
- Since:
- jcms-6.0.1
-
isCurrentYear
Check if a given date is in the same year as current year- Parameters:
date- the date to check- Returns:
- true if the given date is in current year
- Since:
- jcms-9.0.3
-
getTimeWithoutMillis
public static long getTimeWithoutMillis(long time) Returns the given time without millis.- Parameters:
time- the time to truncate the millis- Returns:
- the given time without millis.
- Since:
- jcms-8.0.2
-
getCurrentTimeWithoutMillis
public static long getCurrentTimeWithoutMillis()Returns the current time without millis.- Returns:
- the current time without millis.
- Since:
- jcms-8.0.2
-
getNewDateWithoutMillis
Returns the current date with milliseconds reset to 0.- Returns:
- the current date with milliseconds reset to 0.
- Since:
- jcms-6.0.0
-
rollAndAdd
Do a roll on this calendar instance and add or remove one larger field if the roll action return to the boundary of the period Example roll 1 day for 31/12/2010 will give 01/01/2010. This method will return 01/01/2011- Parameters:
calendar- the calendar to updatefield- the field to rollamount- the amount of roll
-
getDayEndDate
Get the date of the end of the day (23h59 59s 999 ms)- Parameters:
date- the datelocale- theLocaleused to compute calendar object- Returns:
- the end date of the day
-
getDayStartDate
Returns the date corresponding to the day start date of a date- Parameters:
date- the datelocale- theLocaleused to compute calendar object- Returns:
- the date at 0h00:00 000
-
getMonthDisplayName
Returns the string representation of the month in the given month value, style and locale.- Parameters:
value- the month value for which the string representation is returned. beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)style- the style applied to the string representation; one ofCalendar.SHORTorCalendar.LONG.locale- the locale for the string representation- Returns:
- the string representation of the given field in the given style, or null if no string representation is applicable.
-
getYearValue
Gets the year value according the string value in parameter or the current year if 'sValue' in parameter is empty.- Parameters:
sValue- year value inStringor 'all'- Returns:
- int representing the year
-
getMonthValue
Gets the month value according the string value in parameter- Parameters:
sValue- month value inStringor 'all'- Returns:
- int representing the month or -1
-
getDayValue
Gets the day value according the string value in parameter- Parameters:
sValue- day value inStringor 'all'- Returns:
- int representing the day
-
getYear
Extract the year from the given date.- Parameters:
date-Date- Returns:
- year in
int
-
getMonth
Extract the month from the given date.
Note: The month begins to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER).
- Parameters:
date-Date- Returns:
- month beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)
-
getDay
Extract the day of month from the given date.- Parameters:
date-Date- Returns:
- day of month
-
getFirstDateOfMonth
Gets aDateobject representing the first day for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getLastDateOfMonth
Gets aDateobject representing the last day for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getFirstDateOfPreviousMonth
Gets aDateobject representing the first day of the previous month for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getLastDateOfPreviousMonth
Gets aDateobject representing the last day of the previous month for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getFirstDateOfNextMonth
Gets aDateobject representing the first day of the next month for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getLastDateOfNextMonth
Gets aDateobject representing the last day of the next month for the given month and year- Parameters:
month- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year- year- Returns:
Date
-
getFirstDateOfYear
-
getLastDateOfYear
-
getResetCalendar
-
isWeekEndDay
- Parameters:
date- theDatelocale- theLocale- Returns:
- true if the date is a week-end (according to the locale), false otherwise, throw
IllegalArgumentExceptionif any of the argument are null
-
getZonedDateTime
Retrieve a ZonedDateTime in the specified zoneid, from the date specified.- Parameters:
date- a Date at the server time,zoneId- the zone- Returns:
- a ZonedDateTime instance (may return null if null date was specified)
- Since:
- jcms-10.0.1 / JCMS-4865
-
asDate
Converts given instant to a Date.Checks given instant is not null to prevent NPE.
To convert a
DatetoInstant, use nativedate.toInstant()method.- Since:
- jcms-10.0.2
-