Package org.silverpeas.core.util
Class DateUtil
- java.lang.Object
-
- org.silverpeas.core.util.DateUtil
-
public class DateUtil extends Object
DateUtil is an helper class for date manipulation.- Author:
- squere
-
-
Field Summary
Fields Modifier and Type Field Description static DateTimeFormatter
LUCENE_FORMATTER
static Date
MAXIMUM_DATE
static Date
MINIMUM_DATE
static org.apache.commons.lang3.time.FastDateFormat
TIME_FORMATTER
SimpleDateFormat
timeParser
Format and parse dates.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addDaysExceptWeekEnds(Calendar calendar, int nbDay)
Compute a new date by adding the specified number of days without couting week-ends.static int
compareTo(Date date1, Date date2)
Compare 2 dates on year/month/day only.static int
compareTo(Date date1, Date date2, boolean aForceResetHour)
Compare 2 dates.static Calendar
convert(Date curDate)
Convert Date to Calendarstatic Calendar
convert(Date curDate, String language)
Convert Date to Calendarstatic String
date2SQLDate(String date, String language)
static String
date2SQLDate(Date date)
Converts the specified date to a SQL formatted datestatic boolean
datesAreEqual(Date date1, Date date2)
Test if two dates are equalstatic 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
formatAsLuceneDate(LocalDate date)
static String
formatDate(LocalDate date)
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 datestatic Date
getDate()
Gets the current date with reset 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 datestatic 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 localeCode)
Gets the first day of weeks of the calendar with 1 meaning for sunday, 2 meaning for monday, and so on.static int
getFirstDayOfWeek(Locale locale)
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(String dateDB, String language)
static String
getInputDate(LocalDate date, String language)
static String
getInputDate(Date date, String language)
static DateTimeFormatter
getLocalDateInputFormat(String language)
static LocalizationBundle
getLocalizedProperties(String locale)
Get all the localized date's properties.static Date
getNow()
Gets the current date and hour.static String
getOutputDate(String dateDB, String language)
static String
getOutputDate(Date date, String language)
static String
getOutputDateAndHour(String dateDB, String language)
static String
getOutputDateAndHour(Date date, String language)
static String
getOutputHour(String dateDB, String language)
static String
getOutputHour(Date date, 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 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 LocalDate
parseFromLucene(String 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 LocalDate
stringToLocalDate(String string, String language)
static String
temporal2SQLDate(Temporal temporal)
Converts the specified date to a SQL formatted datestatic String
today2SQLDate()
static LocalDate
toLocalDate(String date)
static LocalDate
toLocalDate(Date date)
-
-
-
Field Detail
-
MINIMUM_DATE
public static final Date MINIMUM_DATE
-
MAXIMUM_DATE
public static final Date MAXIMUM_DATE
-
LUCENE_FORMATTER
public static final DateTimeFormatter LUCENE_FORMATTER
-
timeParser
public final SimpleDateFormat timeParser
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(String dateDB, String language) throws ParseException
- Throws:
ParseException
-
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
-
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 Datelanguage
- 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:
- the current date and hour.
-
getDate
public static Date getDate()
Gets the current date with reset hour.- Returns:
- the current date with reset 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.
-
getLocalDateInputFormat
public static DateTimeFormatter getLocalDateInputFormat(String language)
-
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.
-
getLocalizedProperties
public static LocalizationBundle getLocalizedProperties(String locale)
Get all the localized date's properties. These properties contain day and month labels, common date format, and week first day.- Parameters:
locale
- the locale to use.- Returns:
- The localization bundle containing all date localized date's 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
- if the date is malformed.
-
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/ddformat
- (String) the whished format in according to the date parameter- Returns:
- a java object Date
- Throws:
ParseException
- if the date is malformed.
-
datesAreEqual
public static boolean datesAreEqual(Date date1, Date date2)
Test if two dates are equal- Parameters:
date1
- first datedate2
- 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
- the first date to compare with the second one.date2
- the second date to compare with the first one.- Returns:
- int the distance between the two dates. A negative value means the first date is anterior the second one whereas a positive value means the first date is posterior the second date. Zero means the two dates are equal.
-
compareTo
public static int compareTo(Date date1, Date date2, boolean aForceResetHour)
Compare 2 dates.- Parameters:
date1
- the first date to compare with the second one.date2
- the second date to compare with the first one.aForceResetHour
- has the hour to be reset when comparing the two dates?- Returns:
- int the distance between the two dates. A negative value means the first date is anterior the second one whereas a positive value means the first date is posterior the second date. Zero means the two dates are equal.
-
today2SQLDate
public static String today2SQLDate()
-
date2SQLDate
public static String date2SQLDate(Date date)
Converts the specified date to a SQL formatted 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
-
temporal2SQLDate
public static String temporal2SQLDate(Temporal temporal)
Converts the specified date to a SQL formatted date- Parameters:
temporal
- the temporal object 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)
-
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
- if the date is malformed
-
resetHour
public static Calendar resetHour(Calendar date)
Reset hour of a date (00:00:00.000)- Parameters:
date
- a date- Returns:
- a
Calendar
instance representing the specified date without the time set.
-
resetHour
public static Date resetHour(Date date)
Reset hour of a date (00:00:00.000)- Parameters:
date
- a date- Returns:
- a
Date
instance representing the specified date without the time set.
-
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
- if the date is malformed
-
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
- if the date is malformed
-
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.
-
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 localeCode)
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 i18n code of the country; for example the first day of weeks is monday for France and UK whereas it is sunday for USA.- Parameters:
localeCode
- the l10n or the i18n code of the locale (fr for the french or fr_FR for France for example).- Returns:
- the first day of week.
-
getFirstDayOfWeek
public static int getFirstDayOfWeek(Locale locale)
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:
locale
- the locale to take into account.- 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
- the specified calendarnbDay
- the number of days to add
-
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
- a date- Returns:
- the index of the day of the week
-
getDayNumberBetween
public static int getDayNumberBetween(Date date1, Date date2)
Get the number of days between two dates.- Parameters:
date1
- the first datedate2
- the second date- Returns:
- int the interval in days between the two dates
-
convert
public static Calendar convert(Date curDate)
Convert Date to Calendar- Parameters:
curDate
- the date to convert- Returns:
- Calendar the calendar representation of the date
-
convert
public static Calendar convert(Date curDate, String language)
Convert Date to Calendar- Parameters:
curDate
- the date to convertlanguage
- the locale to take into account (fr for the french locale (fr_FR) for example).- Returns:
- Calendar the calendar representation of the date
-
-