com.stratelia.webactiv.util
Class DateUtil

java.lang.Object
  extended by com.stratelia.webactiv.util.DateUtil

public class DateUtil
extends Object

DateUtil is an helper class for date manipulation.

Author:
squere

Field Summary
static org.apache.commons.lang3.time.FastDateFormat DATE_FORMATTER
           
static SimpleDateFormat DATE_PARSER
          Format and parse dates.
static org.apache.commons.lang3.time.FastDateFormat DATETIME_FORMATTER
           
static SimpleDateFormat DATETIME_PARSER
           
static org.apache.commons.lang3.time.FastDateFormat ICALDATE_FORMATTER
           
static org.apache.commons.lang3.time.FastDateFormat ICALDAY_FORMATTER
           
static org.apache.commons.lang3.time.FastDateFormat ICALUTCDATE_FORMATTER
           
static org.apache.commons.lang3.time.FastDateFormat ISO8601_FORMATTER
           
static org.apache.commons.lang3.time.FastDateFormat ISO8601DATE_FORMATTER
           
static org.apache.commons.lang3.time.FastDateFormat ISO8601DAY_FORMATTER
           
static Date MAXIMUM_DATE
           
static Date MINIMUM_DATE
           
static org.apache.commons.lang3.time.FastDateFormat TIME_FORMATTER
           
static SimpleDateFormat TIME_PARSER
          Format and parse dates.
 
Method Summary
static void addDaysExceptWeekEnds(Calendar calendar, int nbDay)
          Compute a new date by adding the specified number of days without couting week-ends.
static Datable<?> asDatable(Date aDate, boolean withTime)
          Converts the specified date as a Datable object with the time set or not.
static int compareTo(Date date1, Date date2)
          Compare 2 dates on year/month/day only.
static int compareTo(Date date1, Date aDate2, boolean aForceResetHour)
          Compare 2 dates.
static Calendar convert(Date curDate)
          Convert Date to Calendar
static Calendar convert(Date curDate, String language)
          Convert Date to Calendar
static String date2SQLDate(Date date)
           
static String date2SQLDate(String date, String language)
           
static boolean datesAreEqual(Date date1, Date date2)
          Test if two dates are equal
static String dateToString(Date date, String language)
          Display the date in a language specific standard format.
static int extractHour(String hour)
           
static int extractMinutes(String hour)
           
static String firstDayOfMonth2SQLDate(Date date)
          Returns the first date of month according to given date (ex. 26/08/2010)
static String formatAsICalDate(Date date)
          Formats the specified date according to the ISO 8601 format of the iCal format (in the timezone of the date).
static String formatAsICalDay(Date date)
          Formats the specified date according to the short ISO 8601 format (only the day date is rendered) used in the iCal format.
static String formatAsICalUTCDate(Date date)
          Formats the specified date according to the ISO 8601 format of the iCal format (in UTC).
static String formatAsISO8601Date(Date date)
          Formats the specified date according to the ISO 8601 format.
static String formatAsISO8601Day(Date date)
          Formats the specified date according to the short ISO 8601 format (only the day date is rendered).
static String formatDate(Calendar calend)
          Format a Calendar to a String of format yyyy/MM/dd.
static String formatDate(Date date)
          Format a Date to a String of format yyyy/MM/dd.
static String formatDate(Date date, String pattern)
          Formats the specified date according to the specified date pattern.
static String formatDuration(long duration)
           
static String formatTime(Calendar calend)
          Format a Calendar to a String of format HH:mm.
static String formatTime(Date date)
          Format a Date to a String of format HH:mm.
static Date getBeginOfDay(Date curDate)
          Get first hour, minute, second, millisecond of the specified date
static Date getDate()
          Gets the current date with reseted hour.
static Date getDate(Date date, String hour)
           
static SimpleDateFormat getDateAndHourInputFormat(String lang)
          Get the date language specific standard input format.
static SimpleDateFormat getDateInputFormat(String language)
          Get the date language specific standard input format.
static org.apache.commons.lang3.time.FastDateFormat getDateOutputFormat(String lang)
          Get the date language specific standard output format.
static int getDayNumberBetween(Date date1, Date date2)
          Get the number of days between two dates.
static int getDayNumberInWeek(Date curDate)
          Return the day index of the week from a given date.
static Date getEndDateOfMonth(Date date)
          Compute the date of the last day in the month of the specified date.
static Date getEndDateOfWeek(Date date, String language)
          Compute the date of the last day in the week of the specified date.
static Date getEndDateOfYear(Date date)
          Compute the date of the last day in the year of the specified date.
static Date getEndOfDay(Date curDate)
          Get last hour, minute, second, millisecond of the specified date
static Date getFirstDateOfMonth(Date date)
          Compute the date of the first day in the month of the specified date.
static Date getFirstDateOfWeek(Date date, String language)
          Compute the date of the first day in the week of the specified date.
static Date getFirstDateOfYear(Date date)
          Compute the date of the first day in the year of the specified date.
static int getFirstDayOfWeek(String language)
          Gets the first day of weeks of the calendar with 1 meaning for sunday, 2 meaning for monday, and so on.
static String getFormattedTime(Date date)
           
static SimpleDateFormat getHourInputFormat(String language)
          Get the hour (from a date) language specific standard input format.
static org.apache.commons.lang3.time.FastDateFormat getHourOutputFormat(String lang)
          Get the hour (from a date) language specific standard output format.
static String getInputDate(Date date, String language)
           
static String getInputDate(String dateDB, String language)
           
static ResourceLocator getMultilangProperties(String language)
          Get all date multilang properties.
static Date getNow()
          Gets the current date and hour.
static String getOutputDate(Date date, String language)
           
static String getOutputDate(String dateDB, String language)
           
static String getOutputDateAndHour(Date date, String language)
           
static String getOutputDateAndHour(String dateDB, String language)
           
static String getOutputHour(Date date, String language)
           
static String getOutputHour(String dateDB, String language)
           
static Date parse(String date)
          Parse a special String into a Date.
static Date parse(String date, String format)
          Parse a special String into a Date.
static Calendar parseCalendar(String date)
          Parse a String of format yyyy/MM/dd and return the corresponding Date.
static Date parseDate(String date)
          Parse a String of format yyyy/MM/dd and return the corresponding Date.
static Date parseDateTime(String date)
          Parse a String of format yyyy/MM/dd hh:mm and return the corresponding Date.
static Date parseISO8601Date(String date)
          Parses the specified ISO 8601 formatted date and returns it as a Date instance.
static Date parseTime(String time)
          Parse a String of format yyyy/MM/dd hh:mm and return the corresponding Date.
static Calendar resetHour(Calendar date)
          Reset hour of a date (00:00:00.000)
static Date resetHour(Date date)
          Reset hour of a date (00:00:00.000)
static void setAtBeginOfDay(Calendar calendar)
          Set the first hour, minute, second, millisecond of the specified calendar to 0.
static void setTime(Calendar calend, String time)
          Parse a String of format HH:mm and set the corresponding hours and minutes to the specified Calendar.
static Date stringToDate(String string, String language)
          Parse the date in a language specific standard format.
static Date stringToDate(String date, String hour, String language)
           
static String today2SQLDate()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MINIMUM_DATE

public static final Date MINIMUM_DATE

MAXIMUM_DATE

public static final Date MAXIMUM_DATE

DATE_PARSER

public static final SimpleDateFormat DATE_PARSER
Format and parse dates.


DATE_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat DATE_FORMATTER

DATETIME_PARSER

public static final SimpleDateFormat DATETIME_PARSER

DATETIME_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat DATETIME_FORMATTER

ISO8601DATE_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ISO8601DATE_FORMATTER

ISO8601DAY_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ISO8601DAY_FORMATTER

ICALDAY_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ICALDAY_FORMATTER

ICALDATE_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ICALDATE_FORMATTER

ICALUTCDATE_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ICALUTCDATE_FORMATTER

ISO8601_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat ISO8601_FORMATTER

TIME_PARSER

public static final SimpleDateFormat TIME_PARSER
Format and parse dates.


TIME_FORMATTER

public static final org.apache.commons.lang3.time.FastDateFormat TIME_FORMATTER
Method Detail

dateToString

public static String dateToString(Date date,
                                  String language)
Display the date in a language specific standard format.

Parameters:
date - the date to convert.
language - The current user's language
Returns:
A String representation of the date in the language specific format.

getOutputDate

public static String getOutputDate(Date date,
                                   String language)

getOutputDate

public static String getOutputDate(String dateDB,
                                   String language)
                            throws ParseException
Throws:
ParseException

getOutputHour

public static String getOutputHour(Date date,
                                   String language)

getOutputHour

public static String getOutputHour(String dateDB,
                                   String language)
                            throws ParseException
Throws:
ParseException

getOutputDateAndHour

public static String getOutputDateAndHour(String dateDB,
                                          String language)
                                   throws ParseException
Throws:
ParseException

getOutputDateAndHour

public static String getOutputDateAndHour(Date date,
                                          String language)

getInputDate

public static String getInputDate(Date date,
                                  String language)

getInputDate

public static String getInputDate(String dateDB,
                                  String language)
                           throws ParseException
Throws:
ParseException

stringToDate

public static Date stringToDate(String string,
                                String language)
                         throws ParseException
Parse the date in a language specific standard format.

Parameters:
string - The String to convert in Date
language - The current user's language
Returns:
A Date representation of the String in the language specific format.
Throws:
ParseException - if the input String is null, empty, or just in an incorrect format.

stringToDate

public static Date stringToDate(String date,
                                String hour,
                                String language)
                         throws ParseException
Throws:
ParseException

getNow

public static Date getNow()
Gets the current date and hour.

Returns:

getDate

public static Date getDate()
Gets the current date with reseted hour.

Returns:

getDate

public static Date getDate(Date date,
                           String hour)

extractHour

public static int extractHour(String hour)

extractMinutes

public static int extractMinutes(String hour)

getDateOutputFormat

public static org.apache.commons.lang3.time.FastDateFormat getDateOutputFormat(String lang)
Get the date language specific standard output format.

Parameters:
lang - The current user's language
Returns:
A SimpleDateFormat initialized with the language specific output format.

getDateInputFormat

public static SimpleDateFormat getDateInputFormat(String language)
Get the date language specific standard input format.

Parameters:
language - The current user's language
Returns:
A SimpleDateFormat initialized with the language specific input format.

getHourOutputFormat

public static org.apache.commons.lang3.time.FastDateFormat getHourOutputFormat(String lang)
Get the hour (from a date) language specific standard output format.

Parameters:
lang - The current user's language
Returns:
A SimpleDateFormat initialized with the language specific output format.

getHourInputFormat

public static SimpleDateFormat getHourInputFormat(String language)
Get the hour (from a date) language specific standard input format.

Parameters:
language - The current user's language
Returns:
A SimpleDateFormat initialized with the language specific input format.

getDateAndHourInputFormat

public static SimpleDateFormat getDateAndHourInputFormat(String lang)
Get the date language specific standard input format.

Parameters:
lang - The current user's language
Returns:
A SimpleDateFormat initialized with the language specific input format.

getMultilangProperties

public static ResourceLocator getMultilangProperties(String language)
Get all date multilang properties. This properties contains day and month labels, common date format, and week first day.

Parameters:
language - The current user's language.
Returns:
The ResourceLocator containing all date multilang properties

parse

public static Date parse(String date)
                  throws ParseException
Parse a special String into a Date.

Parameters:
date - (String) the format of this date must be yyyy/MM/dd
Returns:
a java object Date
Throws:
ParseException

parse

public static Date parse(String date,
                         String format)
                  throws ParseException
Parse a special String into a Date.

Parameters:
date - (String) the format of this date must be yyyy/MM/dd
format - (String) the whished format in according to the date parameter
Returns:
a java object Date
Throws:
ParseException

datesAreEqual

public static boolean datesAreEqual(Date date1,
                                    Date date2)
Test if two dates are equal

Parameters:
date1 - first date
date2 - second date
Returns:
true if both dates defined the same date

compareTo

public static int compareTo(Date date1,
                            Date date2)
Compare 2 dates on year/month/day only.

Parameters:
date1 -
date2 -
Returns:
int

compareTo

public static int compareTo(Date date1,
                            Date aDate2,
                            boolean aForceResetHour)
Compare 2 dates.

Parameters:
date1 -
aDate2 -
Returns:
int

today2SQLDate

public static String today2SQLDate()

date2SQLDate

public static String date2SQLDate(Date date)
Parameters:
date - the date to transform
Returns:
a String representing the given date in a yyyy/MM/dd format or null if given date is null

date2SQLDate

public static String date2SQLDate(String date,
                                  String language)
                           throws ParseException
Throws:
ParseException

firstDayOfMonth2SQLDate

public static String firstDayOfMonth2SQLDate(Date date)
Returns the first date of month according to given date (ex. 26/08/2010)

Parameters:
date - any date
Returns:
first date of month in sql format (ie. 2010/08/01)

formatDuration

public static String formatDuration(long duration)

getFormattedTime

public static String getFormattedTime(Date date)

parseDate

public static Date parseDate(String date)
                      throws ParseException
Parse a String of format yyyy/MM/dd and return the corresponding Date.

Parameters:
date - the String to be parsed.
Returns:
the corresponding date.
Throws:
ParseException

resetHour

public static Calendar resetHour(Calendar date)
Reset hour of a date (00:00:00.000)

Parameters:
date -
Returns:

resetHour

public static Date resetHour(Date date)
Reset hour of a date (00:00:00.000)

Parameters:
date -
Returns:

parseDateTime

public static Date parseDateTime(String date)
                          throws ParseException
Parse a String of format yyyy/MM/dd hh:mm and return the corresponding Date.

Parameters:
date - the String to be parsed.
Returns:
the corresponding date.
Throws:
ParseException

parseTime

public static Date parseTime(String time)
                      throws ParseException
Parse a String of format yyyy/MM/dd hh:mm and return the corresponding Date.

Parameters:
time - the String to be parsed.
Returns:
the corresponding date.
Throws:
ParseException

parseCalendar

public static Calendar parseCalendar(String date)
                              throws ParseException
Parse a String of format yyyy/MM/dd and return the corresponding Date.

Parameters:
date - the String to be parsed.
Returns:
the corresponding date.
Throws:
ParseException

formatDate

public static String formatDate(Date date)
Format a Date to a String of format yyyy/MM/dd.

Parameters:
date - the date to be formatted.
Returns:
the formatted String.

formatDate

public static String formatDate(Calendar calend)
Format a Calendar to a String of format yyyy/MM/dd.

Parameters:
calend - the date to be formatted.
Returns:
the formatted String.

formatDate

public static String formatDate(Date date,
                                String pattern)
Formats the specified date according to the specified date pattern.

Parameters:
date - the date to format into a string.
pattern - the pattern to apply in the format.
Returns:
a string representation of the specified date and in the specified pattern.

setTime

public static void setTime(Calendar calend,
                           String time)
Parse a String of format HH:mm and set the corresponding hours and minutes to the specified Calendar.

Parameters:
time - the String to be parsed.
calend - the calendar to be updated.

formatTime

public static String formatTime(Date date)
Format a Date to a String of format HH:mm.

Parameters:
date - the date to be formatted.
Returns:
the formatted String.

formatTime

public static String formatTime(Calendar calend)
Format a Calendar to a String of format HH:mm.

Parameters:
calend - the date to be formatted.
Returns:
the formatted String.

formatAsISO8601Date

public static String formatAsISO8601Date(Date date)
Formats the specified date according to the ISO 8601 format.

Parameters:
date - the date to format.
Returns:
a String representation of the date in one of the ISO 8601 format (down to the minute and without the UTC offset).

formatAsISO8601Day

public static String formatAsISO8601Day(Date date)
Formats the specified date according to the short ISO 8601 format (only the day date is rendered).

Parameters:
date - the date to format.
Returns:
a String representation of the date in one of the short ISO 8601 format (yyyy-MM-dd).

formatAsICalDate

public static String formatAsICalDate(Date date)
Formats the specified date according to the ISO 8601 format of the iCal format (in the timezone of the date).

Parameters:
date - the date to format.
Returns:
a String representation of the date the ISO 8601 format of the iCal format (down to the second).

formatAsICalUTCDate

public static String formatAsICalUTCDate(Date date)
Formats the specified date according to the ISO 8601 format of the iCal format (in UTC).

Parameters:
date - the date to format.
Returns:
a String representation of the date the ISO 8601 format of the iCal format (down to the second in UTC).

formatAsICalDay

public static String formatAsICalDay(Date date)
Formats the specified date according to the short ISO 8601 format (only the day date is rendered) used in the iCal format.

Parameters:
date - the date to format.
Returns:
a String representation of the date in one of the short ISO 8601 format (yyyyMMdd).

parseISO8601Date

public static Date parseISO8601Date(String date)
                             throws ParseException
Parses the specified ISO 8601 formatted date and returns it as a Date instance.

Parameters:
date - the date to parse (must satisfy one of the following pattern yyyy-MM-ddTHH:mm or yyyy-MM-dd).
Returns:
a date object, resulting of the parsing.
Throws:
ParseException - if the specified date is not in the one of the expected formats.

asDatable

public static Datable<?> asDatable(Date aDate,
                                   boolean withTime)
Converts the specified date as a Datable object with the time set or not.

Parameters:
aDate - a Java date to convert.
withTime - the time in the Java date has to be taken into account.
Returns:
a Datable object.

getFirstDateOfYear

public static Date getFirstDateOfYear(Date date)
Compute the date of the first day in the year of the specified date.

Parameters:
date - the specified date.
Returns:
a date for the first day of the year of the specified date.

getEndDateOfYear

public static Date getEndDateOfYear(Date date)
Compute the date of the last day in the year of the specified date.

Parameters:
date - the specified date.
Returns:
a date for the last day of the year of the specified date.

getFirstDateOfMonth

public static Date getFirstDateOfMonth(Date date)
Compute the date of the first day in the month of the specified date.

Parameters:
date - the specified date.
Returns:
a date for the first day of the month of the specified date.

getEndDateOfMonth

public static Date getEndDateOfMonth(Date date)
Compute the date of the last day in the month of the specified date.

Parameters:
date - the specified date.
Returns:
a date for the last day of the month of the specified date.

getFirstDateOfWeek

public static Date getFirstDateOfWeek(Date date,
                                      String language)
Compute the date of the first day in the week of the specified date.

Parameters:
date - the specified date.
language - the locale to take into account (fr for the french locale (fr_FR) for example).
Returns:
a date for the first day of the week of the specified date.

getEndDateOfWeek

public static Date getEndDateOfWeek(Date date,
                                    String language)
Compute the date of the last day in the week of the specified date.

Parameters:
date - the specified date.
language - the locale to take into account (fr for the french locale (fr_FR) for example).
Returns:
a date for the last day of the week of the specified date.

getFirstDayOfWeek

public static int getFirstDayOfWeek(String language)
Gets the first day of weeks of the calendar with 1 meaning for sunday, 2 meaning for monday, and so on. The first day of weeks depends on the locale; the first day of weeks is monday for french whereas it is for sunday for US.

Parameters:
language - the locale to take into account (fr for the french locale (fr_FR) for example).
Returns:
the first day of week.

getEndOfDay

public static Date getEndOfDay(Date curDate)
Get last hour, minute, second, millisecond of the specified date

Parameters:
curDate - the specified date
Returns:
a date at last hour, minute, second and millisecond of the specified date

getBeginOfDay

public static Date getBeginOfDay(Date curDate)
Get first hour, minute, second, millisecond of the specified date

Parameters:
curDate - the specified date
Returns:
a date at last hour, minute, second and millisecond of the specified date

setAtBeginOfDay

public static void setAtBeginOfDay(Calendar calendar)
Set the first hour, minute, second, millisecond of the specified calendar to 0.

Parameters:
calendar - the specified calendar.

addDaysExceptWeekEnds

public static void addDaysExceptWeekEnds(Calendar calendar,
                                         int nbDay)
Compute a new date by adding the specified number of days without couting week-ends.

Parameters:
calendar -
nbDay -

getDayNumberInWeek

public static int getDayNumberInWeek(Date curDate)
Return the day index of the week from a given date. This index is that of Calendar monday, tuesday, ... Use Calendar.MONDAY, Calendar.TUESDAY, ...

Parameters:
curDate -
Returns:
String

getDayNumberBetween

public static int getDayNumberBetween(Date date1,
                                      Date date2)
Get the number of days between two dates.

Parameters:
date1 -
date2 -
Returns:
int

convert

public static Calendar convert(Date curDate)
Convert Date to Calendar

Parameters:
curDate -
Returns:
Calendar

convert

public static Calendar convert(Date curDate,
                               String language)
Convert Date to Calendar

Parameters:
curDate -
language - the locale to take into account (fr for the french locale (fr_FR) for example).
Returns:
Calendar


Copyright © 2016 Silverpeas. All Rights Reserved.