com.jalios.util
Class DateUtil

java.lang.Object
  extended by com.jalios.util.DateUtil

public class DateUtil
extends Object

Date Util

Since:
JCMS 6.3
Author:
Ludovic SMADJA

Field Summary
static String REVISION
           
 
Constructor Summary
DateUtil()
           
 
Method Summary
static String formatDuration(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.
static String formatDuration(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 s
static 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 String formatDuration(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 s
static String formatIso8601Date(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 String formatNoSpaceDate(Date date)
          Returns a date formated with the following Date Time format: yyyy-MM-dd_hh-mm-ss
This method is thread safe.
static String formatRfc822Date(Date date)
          Returns a date formated with the RFC 822 Date Time format (eg Fri, 1 Jan 1999 14:07:00 +0100).
static String formatW3cDate(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 Date getNewDateWithoutMillis()
          Returns the current date with milliseconds reset to 0.
static boolean isSameDay(Date date1, Date date2)
          Check if the 2 given dates are the same day (from 0h00 included to 0h00 excluded)
static boolean isToday(Date date)
          Check if a given date is the same day as today (from 0h00 included to 0h00 excluded)
static boolean isTomorrow(Date date)
          Check if a given date is the same day as tomorrow (from 0h00 included to 0h00 excluded)
static boolean isYesterday(Date date)
          Check if a given date is the same day as yesterday (from 0h00 included to 0h00 excluded)
static void mainTestFormatDuration(String[] args)
           
static boolean overlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate)
          Indicates if both intervals overlaps or not
static boolean overlapWith(Date startDate, Date endDate, Date secondStartDate, Date secondEndDate, boolean greedy)
          Indicates if both intervals overlaps or not
static Date parseIso8601Date(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 Date parseNoSpaceDate(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 Date parseRfc822Date(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 Date parseW3cDate(String source)
          Returns the date parsed from the given string 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 void rollAndAdd(Calendar calendar, int field, int amount)
          Do a roll on this calendar instance and addor 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).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values
Constructor Detail

DateUtil

public DateUtil()
Method Detail

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 interval
endDate - the high boundary of the first interval
secondStartDate - the low boundary of the second interval
secondEndDate - 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 interval
endDate - the high boundary of the first interval
secondStartDate - the low boundary of the second interval
secondEndDate - the high boundary of the second interval
greedy - true if the method shoudl return true when both interval overlap with only one boundary
Returns:
true if both interval overlaps, false otherwise

formatDuration

public static String formatDuration(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.

Parameters:
begin - the begin date
end - the end date
Returns:
the formatted String

formatDuration

public static String formatDuration(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 s

Parameters:
begin - the begin date
end - the end date
locale - the locale to use
Returns:
the formatted String

formatDuration

public static 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. Use default locale.

Parameters:
time - the duration to format (in milliseconds)
Returns:
the formatted String

formatDuration

public static String formatDuration(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 s

Parameters:
time - the duration to format (in milliseconds)
locale - the locale to use
Returns:
the formatted String

mainTestFormatDuration

public static void mainTestFormatDuration(String[] args)

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

Examples :
  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

public static String formatIso8601Date(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.

Parameters:
date - the date to format.
Returns:
a String with the formated date.
Since:
jcms-5.7.2
See Also:
parseIso8601Date(String)

parseIso8601Date

public static Date parseIso8601Date(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.

Parameters:
source - the string to be parsed
Returns:
a Date
Since:
jcms-5.7.2
See Also:
formatIso8601Date(Date)

formatW3cDate

public static String formatW3cDate(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.

Parameters:
date - the date to format.
Returns:
a String with the formated date.
See Also:
parseW3cDate(String)

parseW3cDate

public static Date parseW3cDate(String source)
Returns the date parsed from the given string 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.

Parameters:
source - the string to be parsed
Returns:
a Date
Since:
jcms-5.7.2
See Also:
formatW3cDate(Date)

formatRfc822Date

public static String formatRfc822Date(Date date)
Returns a date formated with the RFC 822 Date Time format (eg Fri, 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

public static Date parseRfc822Date(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"). 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

public static String formatNoSpaceDate(Date date)
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

public static Date parseNoSpaceDate(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.

Parameters:
source - the string to be parsed
Returns:
a Date
Since:
jcms-5.7.2

isToday

public static boolean isToday(Date date)
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

isYesterday

public static boolean isYesterday(Date date)
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

isTomorrow

public static boolean isTomorrow(Date date)
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

isSameDay

public static boolean isSameDay(Date date1,
                                Date date2)
Check if the 2 given dates are the same day (from 0h00 included to 0h00 excluded)

Parameters:
date1 - the first date to check
date2 - the second date to check
Returns:
true if the given date is today
Since:
jcms-6.0.1

getNewDateWithoutMillis

public static Date 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

public static void rollAndAdd(Calendar calendar,
                              int field,
                              int amount)
Do a roll on this calendar instance and addor 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 update
field - the field to roll
amount - the amount of roll


Copyright © 2001-2010 Jalios SA. All Rights Reserved.