Package com.day.cq.commons.date
Class DateUtil
- java.lang.Object
-
- com.day.cq.commons.date.DateUtil
-
public class DateUtil extends Object
DateUtilis a utility class that provides easy access to commonly used dates and for parsing/reading ISO8601 date strings.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DateFormatgetDateFormat(String pattern, String defaultPattern, Locale locale)Returns a date format object, based on either a custom date pattern or a default date pattern, and aLocalestatic DateFormatgetDateFormat(String pattern, DateFormat defaultDateFormat, Locale locale)Returns a date format object, based on either a custom date pattern or a default date pattern, and aLocalestatic DateFormatgetDateFormat(String pattern, Locale locale)Returns a date format object, based on a date pattern and aLocalestatic StringgetISO8601Date(Calendar calendar)Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".static StringgetISO8601Date(Date date)Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".static StringgetISO8601Date(Date date, TimeZone timeZone)Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".static StringgetISO8601DateAndTimeNoMillis(Calendar calendar)Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".static StringgetISO8601DateAndTimeNoMillis(Date date)Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".static StringgetISO8601DateAndTimeNoMillis(Date date, TimeZone timeZone)Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".static StringgetISO8601DateNoTime(Calendar calendar)Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD"static StringgetISO8601DateNoTime(Date date)Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".static StringgetISO8601DateNoTime(Date date, TimeZone timeZone)Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".CalendargetLastYearStart()CalendargetMonthStart()CalendargetNow()CalendargetThreeMonthsAgo()CalendargetToday()CalendargetWeekStart()CalendargetYearStart()static CalendarparseISO8601(String iso8601Date)Parse the given string in ISO 8601 format and build a Calendar object.static CalendarparseISO8601(String iso8601Date, TimeZone defaultTimeZone)Parse the given string in ISO 8601 format and build a Calendar object.
-
-
-
Constructor Detail
-
DateUtil
public DateUtil()
Initializes a DateUtil with the first day of week being the Monday.
-
DateUtil
public DateUtil(int firstDayOfWeek)
Initializes a DateUtil with a custom first day of the week.- Parameters:
firstDayOfWeek- the first day of the week.- See Also:
Calendar.DAY_OF_WEEK
-
-
Method Detail
-
getLastYearStart
public final Calendar getLastYearStart()
- Returns:
- calendar that represents the beginning of the last year.
-
getYearStart
public final Calendar getYearStart()
- Returns:
- calendar that represents the beginning of this year.
-
getThreeMonthsAgo
public final Calendar getThreeMonthsAgo()
- Returns:
- calendar that represents the date three months ago.
-
getMonthStart
public final Calendar getMonthStart()
- Returns:
- calendar that represents the beginning of this month.
-
getWeekStart
public final Calendar getWeekStart()
- Returns:
- calendar that represents the start of this week. Depends on
Calendar.setFirstDayOfWeek(int)!
-
getToday
public final Calendar getToday()
- Returns:
- calendar that represents the current date.
-
getNow
public final Calendar getNow()
- Returns:
- calendar that represents the current time.
-
parseISO8601
public static Calendar parseISO8601(String iso8601Date) throws InvalidDateException
Parse the given string in ISO 8601 format and build a Calendar object. If no timezone is given in the string, it will use the system's default timezone.- Parameters:
iso8601Date- the date in ISO 8601 format- Returns:
- a Calendar instance
- Throws:
InvalidDateException- if the date string is not valid
-
parseISO8601
public static Calendar parseISO8601(String iso8601Date, TimeZone defaultTimeZone) throws InvalidDateException
Parse the given string in ISO 8601 format and build a Calendar object. Allows to define the timezone if the string does not contain a timezone reference. Otherwise the returned calendar will always have the timezone that is in the ISO 8601 string.- Parameters:
iso8601Date- the date in ISO 8601 formatdefaultTimeZone- the timezone to use when the timezone is not specified in the iso-8601 string (ifnull, the system default timezone will be used)- Returns:
- a Calendar instance
- Throws:
InvalidDateException- if the date string is not valid
-
getISO8601Date
public static String getISO8601Date(Calendar calendar)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".- Parameters:
calendar- a Calendar instance- Returns:
- a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"
-
getISO8601Date
public static String getISO8601Date(Date date)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).- Parameters:
date- a Date instance representing a UTC time- Returns:
- a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"
-
getISO8601Date
public static String getISO8601Date(Date date, TimeZone timeZone)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ". It uses the given timezone for interpreting the date value.- Parameters:
date- a Date instancetimeZone- the timeZone of the date- Returns:
- a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"
-
getISO8601DateAndTimeNoMillis
public static String getISO8601DateAndTimeNoMillis(Calendar calendar)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".- Parameters:
calendar- a Calendar instance- Returns:
- a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"
-
getISO8601DateAndTimeNoMillis
public static String getISO8601DateAndTimeNoMillis(Date date)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).- Parameters:
date- a Date instance representing a UTC time- Returns:
- a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"
-
getISO8601DateAndTimeNoMillis
public static String getISO8601DateAndTimeNoMillis(Date date, TimeZone timeZone)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ". It uses the given timezone for interpreting the date value.- Parameters:
date- a Date instancetimeZone- the timeZone of the date- Returns:
- a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"
-
getISO8601DateNoTime
public static String getISO8601DateNoTime(Calendar calendar)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD"- Parameters:
calendar- a Calendar instance- Returns:
- a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"
-
getISO8601DateNoTime
public static String getISO8601DateNoTime(Date date)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).- Parameters:
date- a Date instance representing a UTC time.- Returns:
- a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"
-
getISO8601DateNoTime
public static String getISO8601DateNoTime(Date date, TimeZone timeZone)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD". It uses the given timezone for interpreting the date value.- Parameters:
date- a Date instance representing a datetimeZone- the timeZone of the date- Returns:
- a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"
-
getDateFormat
public static DateFormat getDateFormat(String pattern, Locale locale)
Returns a date format object, based on a date pattern and aLocale- Parameters:
pattern- Date patternlocale- Date format localisation- Returns:
- Date format or
null
-
getDateFormat
public static DateFormat getDateFormat(String pattern, String defaultPattern, Locale locale)
Returns a date format object, based on either a custom date pattern or a default date pattern, and aLocale- Parameters:
pattern- User provided date patterndefaultPattern- Default date format used if the user provided date pattern was not validlocale- Date format localisation- Returns:
- Custom date format (if valid) or default date format
-
getDateFormat
public static DateFormat getDateFormat(String pattern, DateFormat defaultDateFormat, Locale locale)
Returns a date format object, based on either a custom date pattern or a default date pattern, and aLocale- Parameters:
pattern- User provided date patterndefaultDateFormat- Default date format used if the user provided date pattern was not validlocale- Date format localisation- Returns:
- Custom date format (if valid) or default date format
-
-