Class DateUtil


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

      Constructors 
      Constructor Description
      DateUtil()
      Initializes a DateUtil with the first day of week being the Monday.
      DateUtil​(int firstDayOfWeek)
      Initializes a DateUtil with a custom first day of the week.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.text.DateFormat getDateFormat​(java.lang.String pattern, java.lang.String defaultPattern, java.util.Locale locale)
      Returns a date format object, based on either a custom date pattern or a default date pattern, and a Locale
      static java.text.DateFormat getDateFormat​(java.lang.String pattern, java.text.DateFormat defaultDateFormat, java.util.Locale locale)
      Returns a date format object, based on either a custom date pattern or a default date pattern, and a Locale
      static java.text.DateFormat getDateFormat​(java.lang.String pattern, java.util.Locale locale)
      Returns a date format object, based on a date pattern and a Locale
      static java.lang.String getISO8601Date​(java.util.Calendar calendar)
      Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
      static java.lang.String getISO8601Date​(java.util.Date date)
      Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
      static java.lang.String getISO8601Date​(java.util.Date date, java.util.TimeZone timeZone)
      Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
      static java.lang.String getISO8601DateAndTimeNoMillis​(java.util.Calendar calendar)
      Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
      static java.lang.String getISO8601DateAndTimeNoMillis​(java.util.Date date)
      Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
      static java.lang.String getISO8601DateAndTimeNoMillis​(java.util.Date date, java.util.TimeZone timeZone)
      Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
      static java.lang.String getISO8601DateNoTime​(java.util.Calendar calendar)
      Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD"
      static java.lang.String getISO8601DateNoTime​(java.util.Date date)
      Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".
      static java.lang.String getISO8601DateNoTime​(java.util.Date date, java.util.TimeZone timeZone)
      Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".
      java.util.Calendar getLastYearStart()  
      java.util.Calendar getMonthStart()  
      java.util.Calendar getNow()  
      java.util.Calendar getThreeMonthsAgo()  
      java.util.Calendar getToday()  
      java.util.Calendar getWeekStart()  
      java.util.Calendar getYearStart()  
      static java.util.Calendar parseISO8601​(java.lang.String iso8601Date)
      Parse the given string in ISO 8601 format and build a Calendar object.
      static java.util.Calendar parseISO8601​(java.lang.String iso8601Date, java.util.TimeZone defaultTimeZone)
      Parse the given string in ISO 8601 format and build a Calendar object.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 java.util.Calendar getLastYearStart()
        Returns:
        calendar that represents the beginning of the last year.
      • getYearStart

        public final java.util.Calendar getYearStart()
        Returns:
        calendar that represents the beginning of this year.
      • getThreeMonthsAgo

        public final java.util.Calendar getThreeMonthsAgo()
        Returns:
        calendar that represents the date three months ago.
      • getMonthStart

        public final java.util.Calendar getMonthStart()
        Returns:
        calendar that represents the beginning of this month.
      • getWeekStart

        public final java.util.Calendar getWeekStart()
        Returns:
        calendar that represents the start of this week. Depends on Calendar.setFirstDayOfWeek(int)!
      • getToday

        public final java.util.Calendar getToday()
        Returns:
        calendar that represents the current date.
      • getNow

        public final java.util.Calendar getNow()
        Returns:
        calendar that represents the current time.
      • parseISO8601

        public static java.util.Calendar parseISO8601​(java.lang.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 java.util.Calendar parseISO8601​(java.lang.String iso8601Date,
                                                      java.util.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 java.lang.String getISO8601Date​(java.util.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 java.lang.String getISO8601Date​(java.util.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 java.lang.String getISO8601Date​(java.util.Date date,
                                                      java.util.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 java.lang.String getISO8601DateAndTimeNoMillis​(java.util.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 java.lang.String getISO8601DateAndTimeNoMillis​(java.util.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 java.lang.String getISO8601DateAndTimeNoMillis​(java.util.Date date,
                                                                     java.util.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 java.lang.String getISO8601DateNoTime​(java.util.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 java.lang.String getISO8601DateNoTime​(java.util.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 java.lang.String getISO8601DateNoTime​(java.util.Date date,
                                                            java.util.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 java.text.DateFormat getDateFormat​(java.lang.String pattern,
                                                         java.util.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 java.text.DateFormat getDateFormat​(java.lang.String pattern,
                                                         java.lang.String defaultPattern,
                                                         java.util.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 java.text.DateFormat getDateFormat​(java.lang.String pattern,
                                                         java.text.DateFormat defaultDateFormat,
                                                         java.util.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