Class DateUtils


  • public final class DateUtils
    extends java.lang.Object
    Parses and formats RFC 822 dates. This class is thread-safe.
    Since:
    2.7
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DATE_FORMAT  
      static java.lang.String DATETIME_FORMAT  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.time.Instant addDays​(java.time.Instant instant, int numberOfDays)  
      static java.util.Date addDays​(java.util.Date date, int numberOfDays)
      Adds a number of days to a date returning a new object.
      static java.lang.Long dateToLong​(java.util.Date date)  
      static java.lang.String formatDate​(java.time.Instant d)
      Warning: relies on default timezone!
      static java.lang.String formatDate​(java.util.Date d)
      Warning: relies on default timezone!
      static java.lang.String formatDateTime​(long ms)
      Warning: relies on default timezone!
      static java.lang.String formatDateTime​(java.time.OffsetDateTime dt)  
      static java.lang.String formatDateTime​(java.util.Date d)
      Warning: relies on default timezone!
      static java.lang.String formatDateTimeNullSafe​(java.util.Date date)
      Warning: relies on default timezone!
      static java.util.Date longToDate​(java.lang.Long time)  
      static java.util.Date parseDate​(java.lang.String s)
      Return a date at the start of day.
      static java.util.Date parseDateOrDateTime​(java.lang.String stringDate)
      Warning: rely on default timezone!
      static java.util.Date parseDateOrDateTime​(java.lang.String stringDate, java.time.ZoneId timeZone)
      Parse either a full date time (using RFC-822 TZ format), or a local date.
      static java.util.Date parseDateQuietly​(java.lang.String s)
      Parse format DATE_FORMAT.
      static java.util.Date parseDateTime​(java.lang.String s)  
      static java.util.Date parseDateTimeQuietly​(java.lang.String s)
      Parse format DATETIME_FORMAT.
      static java.util.Date parseEndingDateOrDateTime​(java.lang.String stringDate)
      Warning: rely on default timezone for local dates!
      static java.util.Date parseEndingDateOrDateTime​(java.lang.String stringDate, java.time.ZoneId timeZone)
      Return the datetime if @param stringDate is a datetime, local date + 1 day if stringDate is a local date.
      static java.time.LocalDate parseLocalDate​(java.lang.String s)  
      static java.time.LocalDate parseLocalDateQuietly​(java.lang.String s)
      Parse format DATE_FORMAT.
      static java.time.OffsetDateTime parseOffsetDateTime​(java.lang.String s)  
      static java.time.OffsetDateTime parseOffsetDateTimeQuietly​(java.lang.String s)
      Parse format DATETIME_FORMAT.
      static java.util.Date parseStartingDateOrDateTime​(java.lang.String stringDate)
      Warning: rely on default timezone for local dates!
      static java.util.Date parseStartingDateOrDateTime​(java.lang.String stringDate, java.time.ZoneId timeZone)  
      static long truncateToSeconds​(long dateTime)  
      static java.util.Date truncateToSeconds​(java.util.Date d)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • formatDate

        public static java.lang.String formatDate​(java.util.Date d)
        Warning: relies on default timezone!
      • formatDate

        public static java.lang.String formatDate​(java.time.Instant d)
        Warning: relies on default timezone!
        Since:
        7.6
      • formatDateTime

        public static java.lang.String formatDateTime​(java.util.Date d)
        Warning: relies on default timezone!
      • formatDateTime

        public static java.lang.String formatDateTime​(long ms)
        Warning: relies on default timezone!
      • formatDateTime

        public static java.lang.String formatDateTime​(java.time.OffsetDateTime dt)
        Since:
        6.6
      • formatDateTimeNullSafe

        public static java.lang.String formatDateTimeNullSafe​(@Nullable
                                                              java.util.Date date)
        Warning: relies on default timezone!
      • longToDate

        @CheckForNull
        public static java.util.Date longToDate​(@Nullable
                                                java.lang.Long time)
      • dateToLong

        @CheckForNull
        public static java.lang.Long dateToLong​(@Nullable
                                                java.util.Date date)
      • parseDate

        public static java.util.Date parseDate​(java.lang.String s)
        Return a date at the start of day.
        Parameters:
        s - string in format DATE_FORMAT
        Throws:
        SonarException - when string cannot be parsed
      • parseDateQuietly

        @CheckForNull
        public static java.util.Date parseDateQuietly​(@Nullable
                                                      java.lang.String s)
        Parse format DATE_FORMAT. This method never throws exception.
        Parameters:
        s - any string
        Returns:
        the date, null if parsing error or if parameter is null
        Since:
        3.0
      • parseLocalDate

        public static java.time.LocalDate parseLocalDate​(java.lang.String s)
        Since:
        6.6
      • parseLocalDateQuietly

        @CheckForNull
        public static java.time.LocalDate parseLocalDateQuietly​(@Nullable
                                                                java.lang.String s)
        Parse format DATE_FORMAT. This method never throws exception.
        Parameters:
        s - any string
        Returns:
        the date, null if parsing error or if parameter is null
        Since:
        6.6
      • parseDateTime

        public static java.util.Date parseDateTime​(java.lang.String s)
        Parameters:
        s - string in format DATETIME_FORMAT
        Throws:
        SonarException - when string cannot be parsed
      • parseOffsetDateTime

        public static java.time.OffsetDateTime parseOffsetDateTime​(java.lang.String s)
        Parameters:
        s - string in format DATETIME_FORMAT
        Throws:
        SonarException - when string cannot be parsed
        Since:
        6.6
      • parseDateTimeQuietly

        @CheckForNull
        public static java.util.Date parseDateTimeQuietly​(@Nullable
                                                          java.lang.String s)
        Parse format DATETIME_FORMAT. This method never throws exception.
        Parameters:
        s - any string
        Returns:
        the datetime, null if parsing error or if parameter is null
      • parseOffsetDateTimeQuietly

        @CheckForNull
        public static java.time.OffsetDateTime parseOffsetDateTimeQuietly​(@Nullable
                                                                          java.lang.String s)
        Parse format DATETIME_FORMAT. This method never throws exception.
        Parameters:
        s - any string
        Returns:
        the datetime, null if parsing error or if parameter is null
        Since:
        6.6
      • parseDateOrDateTime

        @CheckForNull
        public static java.util.Date parseDateOrDateTime​(@Nullable
                                                         java.lang.String stringDate)
        Warning: rely on default timezone!
        Since:
        6.1
        See Also:
        parseDateOrDateTime(String, ZoneId)
      • parseDateOrDateTime

        @CheckForNull
        public static java.util.Date parseDateOrDateTime​(@Nullable
                                                         java.lang.String stringDate,
                                                         java.time.ZoneId timeZone)
        Parse either a full date time (using RFC-822 TZ format), or a local date. For local dates, the returned Date will be set at the beginning of the day, in the provided timezone.
        Returns:
        the datetime, null if stringDate is null
        Throws:
        java.lang.IllegalArgumentException - if stringDate is not a correctly formed date or datetime
        Since:
        8.6
      • parseStartingDateOrDateTime

        @CheckForNull
        public static java.util.Date parseStartingDateOrDateTime​(@Nullable
                                                                 java.lang.String stringDate)
        Warning: rely on default timezone for local dates!
        See Also:
        parseDateOrDateTime(String)
      • parseStartingDateOrDateTime

        @CheckForNull
        public static java.util.Date parseStartingDateOrDateTime​(@Nullable
                                                                 java.lang.String stringDate,
                                                                 java.time.ZoneId timeZone)
        See Also:
        parseDateOrDateTime(String, ZoneId)
      • parseEndingDateOrDateTime

        @CheckForNull
        public static java.util.Date parseEndingDateOrDateTime​(@Nullable
                                                               java.lang.String stringDate)
        Warning: rely on default timezone for local dates!
        Since:
        6.1
        See Also:
        parseEndingDateOrDateTime(String, ZoneId)
      • parseEndingDateOrDateTime

        @CheckForNull
        public static java.util.Date parseEndingDateOrDateTime​(@Nullable
                                                               java.lang.String stringDate,
                                                               java.time.ZoneId timeZone)
        Return the datetime if @param stringDate is a datetime, local date + 1 day if stringDate is a local date. So '2016-09-01' would return a date equivalent to '2016-09-02T00:00:00' in the provided timezone
        Returns:
        the datetime, null if stringDate is null
        Throws:
        java.lang.IllegalArgumentException - if stringDate is not a correctly formed date or datetime
        Since:
        8.6
      • addDays

        public static java.util.Date addDays​(java.util.Date date,
                                             int numberOfDays)
        Adds a number of days to a date returning a new object. The original date object is unchanged.
        Parameters:
        date - the date, not null
        numberOfDays - the amount to add, may be negative
        Returns:
        the new date object with the amount added
      • addDays

        public static java.time.Instant addDays​(java.time.Instant instant,
                                                int numberOfDays)
        Since:
        7.6
      • truncateToSeconds

        @CheckForNull
        public static java.util.Date truncateToSeconds​(@Nullable
                                                       java.util.Date d)
      • truncateToSeconds

        public static long truncateToSeconds​(long dateTime)