Class DateUtil

java.lang.Object
com.day.cq.commons.date.DateUtil

public class DateUtil extends Object
DateUtil is a utility class that provides easy access to commonly used dates and for parsing/reading ISO8601 date strings.
  • Constructor Details

    • 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:
  • Method Details

    • 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 format
      defaultTimeZone - the timezone to use when the timezone is not specified in the iso-8601 string (if null, 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 instance
      timeZone - 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 instance
      timeZone - 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 date
      timeZone - 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 a Locale
      Parameters:
      pattern - Date pattern
      locale - 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 a Locale
      Parameters:
      pattern - User provided date pattern
      defaultPattern - Default date format used if the user provided date pattern was not valid
      locale - 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 a Locale
      Parameters:
      pattern - User provided date pattern
      defaultDateFormat - Default date format used if the user provided date pattern was not valid
      locale - Date format localisation
      Returns:
      Custom date format (if valid) or default date format