public class DateUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
MAX_HOUR |
static int |
MAX_MILLISEC |
static int |
MAX_MIN |
static int |
MAX_SEC |
static int |
MIN_DAY |
static int |
MIN_HOUR |
static int |
MIN_MILLISEC |
static int |
MIN_MIN |
static int |
MIN_SEC |
static java.lang.String |
PARAM_VALUE_ALL |
static java.lang.String |
REVISION |
static int |
VALUE_ALL |
Constructor and Description |
---|
DateUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
formatDuration(java.util.Date begin,
java.util.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.
|
static java.lang.String |
formatDuration(java.util.Date begin,
java.util.Date end,
java.util.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 s
|
static java.lang.String |
formatDuration(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.
|
static java.lang.String |
formatDuration(long time,
java.util.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 s
|
static java.lang.String |
formatIso8601Date(java.util.Date date)
Returns a date formated with the ISO 8601 Date Time format (eg
1999-01-01T14:07:00+0100 ).For more details, see http://en.wikipedia.org/wiki/ISO_8601 This method is thread safe. |
static java.lang.String |
formatNoSpaceDate(java.util.Date date)
Returns a date formated with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe. |
static java.lang.String |
formatRfc822Date(java.util.Date date)
Returns a date formated with the RFC 822 Date Time format (eg
Fri, 1 Jan 1999 14:07:00 +0100 ). |
static java.lang.String |
formatW3cDate(java.util.Date date)
Returns a date formated with the W3C Date Time format (eg
1999-01-01T14:07:00+01:00 ).For more details, see http://www.w3.org/TR/NOTE-datetime This method is thread safe. |
static long |
getCurrentTimeWithoutMillis()
Returns the current time without millis.
|
static int |
getDay(java.util.Date date)
Extract the day of month from the given date.
|
static java.util.Date |
getDayEndDate(java.util.Date date,
java.util.Locale locale)
Get the date of the end of the day (23h59 59s 999 ms)
|
static java.util.Date |
getDayStartDate(java.util.Date date,
java.util.Locale locale)
Returns the date corresponding to the day start date of a date
|
static int |
getDayValue(java.lang.String sValue)
Gets the day value according the string value in parameter
|
static java.util.Date |
getFirstDateOfMonth(int month,
int year)
Gets a
Date object representing the first day for the given month and year |
static java.util.Date |
getFirstDateOfNextMonth(int month,
int year)
Gets a
Date object representing the first day of the next month for the given month and year |
static java.util.Date |
getFirstDateOfPreviousMonth(int month,
int year)
Gets a
Date object representing the first day of the previous month for the given month and year |
static java.util.Date |
getFirstDateOfYear(int year) |
static java.util.Date |
getLastDateOfMonth(int month,
int year)
Gets a
Date object representing the last day for the given month and year |
static java.util.Date |
getLastDateOfNextMonth(int month,
int year)
Gets a
Date object representing the last day of the next month for the given month and year |
static java.util.Date |
getLastDateOfPreviousMonth(int month,
int year)
Gets a
Date object representing the last day of the previous month for the given month and year |
static java.util.Date |
getLastDateOfYear(int year) |
static int |
getMonth(java.util.Date date)
Extract the month from the given date.
|
static java.lang.String |
getMonthDisplayName(int value,
int style,
java.util.Locale locale)
Returns the string representation of the month in the given month value, style and locale.
|
static int |
getMonthValue(java.lang.String sValue)
Gets the month value according the string value in parameter
|
static java.util.Date |
getNewDateWithoutMillis()
Returns the current date with milliseconds reset to 0.
|
static java.util.Calendar |
getResetCalendar() |
static long |
getTimeWithoutMillis(long time)
Returns the given time without millis.
|
static int |
getYear(java.util.Date date)
Extract the year from the given date.
|
static int |
getYearValue(java.lang.String sValue)
Gets the year value according the string value in parameter or the current year if 'sValue' in parameter is empty.
|
static boolean |
isCurrentYear(java.util.Date date)
Check if a given date is in the same year as current year
|
static boolean |
isSameDay(java.util.Date date1,
java.util.Date date2)
Check if the 2 given dates are the same day (from 0h00 included to 0h00 excluded)
|
static boolean |
isToday(java.util.Date date)
Check if a given date is the same day as today (from 0h00 included to 0h00 excluded)
|
static boolean |
isTomorrow(java.util.Date date)
Check if a given date is the same day as tomorrow (from 0h00 included to 0h00 excluded)
|
static boolean |
isWeekEndDay(java.util.Date date,
java.util.Locale locale)
Indicates if the provided
Date is a week end (according to providing Locale ) |
static boolean |
isYesterday(java.util.Date date)
Check if a given date is the same day as yesterday (from 0h00 included to 0h00 excluded)
|
static void |
mainTestFormatDuration(java.lang.String[] args) |
static boolean |
overlapWith(java.util.Date startDate,
java.util.Date endDate,
java.util.Date secondStartDate,
java.util.Date secondEndDate)
Indicates if both intervals overlaps or not
|
static boolean |
overlapWith(java.util.Date startDate,
java.util.Date endDate,
java.util.Date secondStartDate,
java.util.Date secondEndDate,
boolean greedy)
Indicates if both intervals overlaps or not
|
static java.util.Date |
parseIso8601Date(java.lang.String source)
Returns the date parsed from the given string with the ISO 8601 Date Time format (eg
1999-01-01T14:07:00+0100 ).For more details, see http://en.wikipedia.org/wiki/ISO_8601 This method is thread safe. |
static java.util.Date |
parseNoSpaceDate(java.lang.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 java.util.Date |
parseRfc822Date(java.lang.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 java.util.Date |
parseW3cDate(java.lang.String source)
Parse the specified string as a W3C Date Time format.
|
static void |
rollAndAdd(java.util.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 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).
|
public static final java.lang.String REVISION
public static final java.lang.String PARAM_VALUE_ALL
public static final int VALUE_ALL
public static final int MAX_HOUR
public static final int MAX_MIN
public static final int MAX_SEC
public static final int MAX_MILLISEC
public static final int MIN_HOUR
public static final int MIN_MIN
public static final int MIN_SEC
public static final int MIN_MILLISEC
public static final int MIN_DAY
public static boolean overlapWith(java.util.Date startDate, java.util.Date endDate, java.util.Date secondStartDate, java.util.Date secondEndDate)
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 intervalpublic static boolean overlapWith(java.util.Date startDate, java.util.Date endDate, java.util.Date secondStartDate, java.util.Date secondEndDate, boolean greedy)
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 boundarypublic static java.lang.String formatDuration(java.util.Date begin, java.util.Date end)
begin
- the begin dateend
- the end datepublic static java.lang.String formatDuration(java.util.Date begin, java.util.Date end, java.util.Locale locale)
begin
- the begin dateend
- the end datelocale
- the locale to usepublic static java.lang.String formatDuration(long time)
time
- the duration to format (in milliseconds)public static java.lang.String formatDuration(long time, java.util.Locale locale)
time
- the duration to format (in milliseconds)locale
- the locale to usepublic static void mainTestFormatDuration(java.lang.String[] args)
public static long roundDuration(long duration)
As of current implementation, the following rules are applied
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
duration
- a duration in millisecondspublic static java.lang.String formatIso8601Date(java.util.Date date)
1999-01-01T14:07:00+0100
).date
- the date to format.parseIso8601Date(String)
public static java.util.Date parseIso8601Date(java.lang.String source)
1999-01-01T14:07:00+0100
).source
- the string to be parsedformatIso8601Date(Date)
public static java.lang.String formatW3cDate(java.util.Date date)
1999-01-01T14:07:00+01:00
).date
- the date to format.parseW3cDate(String)
public static java.util.Date parseW3cDate(java.lang.String source)
Supported format includes the following variations :
YYYY (eg 1997)
YYYY-MM (eg 1997-07)
YYYY-MM-DD (eg 1997-07-16)
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
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-datetime
This method is thread safe.
source
- the string to be parsedformatW3cDate(Date)
public static java.lang.String formatRfc822Date(java.util.Date date)
Fri, 1 Jan 1999 14:07:00 +0100
).
For more details, see http://www.w3.org/Protocols/rfc822/rfc822.txt section 5.1date
- the date to format.public static java.util.Date parseRfc822Date(java.lang.String source)
source
- the string to be parsedpublic static java.lang.String formatNoSpaceDate(java.util.Date date)
date
- the date to format.public static java.util.Date parseNoSpaceDate(java.lang.String source)
source
- the string to be parsedpublic static boolean isToday(java.util.Date date)
date
- the date to checkpublic static boolean isYesterday(java.util.Date date)
date
- the date to checkpublic static boolean isTomorrow(java.util.Date date)
date
- the date to checkpublic static boolean isSameDay(java.util.Date date1, java.util.Date date2)
date1
- the first date to checkdate2
- the second date to checkpublic static boolean isCurrentYear(java.util.Date date)
date
- the date to checkpublic static long getTimeWithoutMillis(long time)
time
- the time to truncate the millispublic static long getCurrentTimeWithoutMillis()
public static java.util.Date getNewDateWithoutMillis()
public static void rollAndAdd(java.util.Calendar calendar, int field, int amount)
calendar
- the calendar to updatefield
- the field to rollamount
- the amount of rollpublic static java.util.Date getDayEndDate(java.util.Date date, java.util.Locale locale)
date
- the datelocale
- the Locale
used to compute calendar objectpublic static java.util.Date getDayStartDate(java.util.Date date, java.util.Locale locale)
date
- the datelocale
- the Locale
used to compute calendar objectpublic static java.lang.String getMonthDisplayName(int value, int style, java.util.Locale locale)
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 of Calendar.SHORT
or Calendar.LONG
.locale
- the locale for the string representationpublic static int getYearValue(java.lang.String sValue)
sValue
- year value in String
or 'all'public static int getMonthValue(java.lang.String sValue)
sValue
- month value in String
or 'all'public static int getDayValue(java.lang.String sValue)
sValue
- day value in String
or 'all'public static int getYear(java.util.Date date)
date
- Date
int
public static int getMonth(java.util.Date date)
Extract the month from the given date.
Note: The month begins to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER).
date
- Date
public static int getDay(java.util.Date date)
date
- Date
public static java.util.Date getFirstDateOfMonth(int month, int year)
Date
object representing the first day for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getLastDateOfMonth(int month, int year)
Date
object representing the last day for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getFirstDateOfPreviousMonth(int month, int year)
Date
object representing the first day of the previous month for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getLastDateOfPreviousMonth(int month, int year)
Date
object representing the last day of the previous month for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getFirstDateOfNextMonth(int month, int year)
Date
object representing the first day of the next month for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getLastDateOfNextMonth(int month, int year)
Date
object representing the last day of the next month for the given month and yearmonth
- value beginning to 1 (eg. with Gregorian Calendar: 1 for JANUARY, 12 for DECEMBER)year
- yearDate
public static java.util.Date getFirstDateOfYear(int year)
public static java.util.Date getLastDateOfYear(int year)
public static java.util.Calendar getResetCalendar()
public static boolean isWeekEndDay(java.util.Date date, java.util.Locale locale)
Date
is a week end (according to providing Locale
)date
- the Date
locale
- the Locale
IllegalArgumentException
if any of the argument are nullCopyright © 2001-2017 Jalios SA. All Rights Reserved.