Class DateUtils


  • public class DateUtils
    extends java.lang.Object
    Generic date parsing and conversion utilities Note: Consider at some point removing dependency on joda-time and switching to Java 8 time.
    See Also:
    http://time4j.net/tutorial/appendix.html
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DateUtils.EpochUnits  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.time.format.DateTimeFormatter EEE_M_D_YY
      Thread-safe Sat M/D/YY formatter
      static java.time.format.DateTimeFormatter H_MM_ampm
      Thread-safe HH:MM am/pm
      static java.time.format.DateTimeFormatter M_D_YY
      Thread-safe M/D/YY formatter
      static java.time.format.DateTimeFormatter MMDDYYYY_HHMM
      Thread-safe Date with time (without seconds)
      static java.time.format.DateTimeFormatter MMDDYYYY_HHMMSS
      Thread-safe Date with time (including seconds)
      static java.time.ZoneId UTC_ZONE  
      static java.time.format.DateTimeFormatter YYYY_MM
      Thread-safe YYYY/MM/DD formatter
      static java.time.format.DateTimeFormatter YYYY_MM_DD
      Thread-safe YYYY/MM/DD formatter
    • Constructor Summary

      Constructors 
      Constructor Description
      DateUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.time.ZonedDateTime atStartOfDay​(java.time.ZonedDateTime date)  
      static int compare​(java.util.Date d1, java.util.Date d2)  
      static java.time.format.DateTimeFormatter dayMonthYear​(java.time.format.TextStyle monthStyle, java.lang.String daySeparator, java.lang.String monthSeparator, int yearDigits)  
      static boolean hasSameDate​(java.time.ZonedDateTime lhs, java.time.ZonedDateTime rhs)  
      static boolean hasTimeComponent​(java.time.ZonedDateTime dateTime)  
      static boolean hasTimeComponent​(java.util.Date date)  
      static boolean isUtc​(java.time.ZonedDateTime date)  
      static java.time.format.DateTimeFormatter monthDayYear​(java.time.format.TextStyle monthStyle, java.lang.String monthSeparator, java.lang.String daySeparator, int yearDigits, boolean time)  
      static java.time.ZonedDateTime nowWithZoneUtc()  
      static <T> T parse​(java.lang.String text, java.time.ZoneId zoneIfNotSpecified, java.time.temporal.TemporalQuery<T> query)  
      static <T> T parseChecked​(java.lang.String text, java.time.ZoneId zoneIfNotSpecified, java.time.temporal.TemporalQuery<T> query)  
      static java.time.ZonedDateTime randomZonedDateTimeUtc​(java.time.ZonedDateTime from, java.time.ZonedDateTime to)  
      static java.time.ZonedDateTime randomZonedDateTimeUtc​(java.util.Random random, java.time.ZonedDateTime from, java.time.ZonedDateTime to)  
      static long timeBetween​(java.time.ZonedDateTime firstDate, java.time.ZonedDateTime secondDate, java.time.temporal.ChronoUnit units)  
      static java.util.Date toDate​(java.time.Instant instant)  
      static java.util.Date toDate​(java.time.LocalDateTime date)  
      static java.util.Date toDate​(java.time.LocalDateTime date, java.time.ZoneOffset offset)  
      static java.util.Date toDate​(java.time.ZonedDateTime date)  
      static long toEpochMillisUtc​(java.time.ZonedDateTime date)  
      static java.time.LocalDateTime toLocalDateTime​(java.lang.String text)  
      static java.time.LocalDateTime toLocalDateTimeChecked​(java.lang.String text)  
      static long toNanos​(int millis)  
      static java.lang.String toStringIsoFormat​(java.lang.String date)  
      static java.lang.String toStringIsoFormat​(java.time.ZonedDateTime date)
      Returns the given ZonedDateTime in ISO format that readable by a wide range of clients.
      static java.time.ZonedDateTime toUtc​(java.time.ZonedDateTime date)  
      static java.time.ZonedDateTime toZonedDateTimeUtc​(int year, java.time.Month month, int dayOfMonth)  
      static java.time.ZonedDateTime toZonedDateTimeUtc​(long epoch)
      Returns a ZonedDateTime from the given epoch value.
      static java.time.ZonedDateTime toZonedDateTimeUtc​(long epochValue, DateUtils.EpochUnits units)
      Returns a ZonedDateTime from the given epoch value.
      static java.time.ZonedDateTime toZonedDateTimeUtc​(java.lang.String text)  
      static java.time.ZonedDateTime toZonedDateTimeUtc​(java.time.Instant instant)  
      static java.time.ZonedDateTime toZonedDateTimeUtc​(java.time.LocalDate date)  
      static java.time.ZonedDateTime toZonedDateTimeUtc​(java.util.Date date)  
      static java.time.ZonedDateTime toZonedDateTimeUtcChecked​(java.lang.String text)  
      static java.time.ZonedDateTime withDatePrecision​(java.time.ZonedDateTime date)  
      static java.time.format.DateTimeFormatter yearMonthDay​(java.time.format.TextStyle monthStyle, java.lang.String yearSeparator, java.lang.String monthSeparator, int yearDigits, boolean time)  
      • Methods inherited from class java.lang.Object

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

      • MMDDYYYY_HHMM

        public static final java.time.format.DateTimeFormatter MMDDYYYY_HHMM
        Thread-safe Date with time (without seconds)
      • MMDDYYYY_HHMMSS

        public static final java.time.format.DateTimeFormatter MMDDYYYY_HHMMSS
        Thread-safe Date with time (including seconds)
      • YYYY_MM

        public static final java.time.format.DateTimeFormatter YYYY_MM
        Thread-safe YYYY/MM/DD formatter
      • YYYY_MM_DD

        public static final java.time.format.DateTimeFormatter YYYY_MM_DD
        Thread-safe YYYY/MM/DD formatter
      • M_D_YY

        public static final java.time.format.DateTimeFormatter M_D_YY
        Thread-safe M/D/YY formatter
      • EEE_M_D_YY

        public static final java.time.format.DateTimeFormatter EEE_M_D_YY
        Thread-safe Sat M/D/YY formatter
      • H_MM_ampm

        public static final java.time.format.DateTimeFormatter H_MM_ampm
        Thread-safe HH:MM am/pm
      • UTC_ZONE

        public static final java.time.ZoneId UTC_ZONE
    • Constructor Detail

      • DateUtils

        public DateUtils()
    • Method Detail

      • atStartOfDay

        public static java.time.ZonedDateTime atStartOfDay​(java.time.ZonedDateTime date)
      • compare

        public static int compare​(java.util.Date d1,
                                  java.util.Date d2)
      • dayMonthYear

        public static java.time.format.DateTimeFormatter dayMonthYear​(java.time.format.TextStyle monthStyle,
                                                                      java.lang.String daySeparator,
                                                                      java.lang.String monthSeparator,
                                                                      int yearDigits)
      • hasSameDate

        public static boolean hasSameDate​(java.time.ZonedDateTime lhs,
                                          java.time.ZonedDateTime rhs)
      • hasTimeComponent

        public static boolean hasTimeComponent​(java.util.Date date)
      • hasTimeComponent

        public static boolean hasTimeComponent​(java.time.ZonedDateTime dateTime)
      • isUtc

        public static boolean isUtc​(java.time.ZonedDateTime date)
      • monthDayYear

        public static java.time.format.DateTimeFormatter monthDayYear​(java.time.format.TextStyle monthStyle,
                                                                      java.lang.String monthSeparator,
                                                                      java.lang.String daySeparator,
                                                                      int yearDigits,
                                                                      boolean time)
      • nowWithZoneUtc

        public static java.time.ZonedDateTime nowWithZoneUtc()
      • parse

        public static <T> T parse​(java.lang.String text,
                                  java.time.ZoneId zoneIfNotSpecified,
                                  java.time.temporal.TemporalQuery<T> query)
      • parseChecked

        public static <T> T parseChecked​(java.lang.String text,
                                         java.time.ZoneId zoneIfNotSpecified,
                                         java.time.temporal.TemporalQuery<T> query)
                                  throws java.time.format.DateTimeParseException
        Throws:
        java.time.format.DateTimeParseException
      • randomZonedDateTimeUtc

        public static java.time.ZonedDateTime randomZonedDateTimeUtc​(java.util.Random random,
                                                                     java.time.ZonedDateTime from,
                                                                     java.time.ZonedDateTime to)
      • randomZonedDateTimeUtc

        public static java.time.ZonedDateTime randomZonedDateTimeUtc​(java.time.ZonedDateTime from,
                                                                     java.time.ZonedDateTime to)
      • timeBetween

        public static long timeBetween​(java.time.ZonedDateTime firstDate,
                                       java.time.ZonedDateTime secondDate,
                                       java.time.temporal.ChronoUnit units)
      • toDate

        public static java.util.Date toDate​(java.time.Instant instant)
      • toDate

        public static java.util.Date toDate​(java.time.LocalDateTime date)
      • toDate

        public static java.util.Date toDate​(java.time.LocalDateTime date,
                                            java.time.ZoneOffset offset)
      • toDate

        public static java.util.Date toDate​(java.time.ZonedDateTime date)
      • toEpochMillisUtc

        public static long toEpochMillisUtc​(java.time.ZonedDateTime date)
      • toLocalDateTime

        public static java.time.LocalDateTime toLocalDateTime​(java.lang.String text)
      • toLocalDateTimeChecked

        public static java.time.LocalDateTime toLocalDateTimeChecked​(java.lang.String text)
                                                              throws java.time.format.DateTimeParseException
        Throws:
        java.time.format.DateTimeParseException
      • toNanos

        public static long toNanos​(int millis)
      • toStringIsoFormat

        public static java.lang.String toStringIsoFormat​(java.lang.String date)
      • toStringIsoFormat

        public static java.lang.String toStringIsoFormat​(java.time.ZonedDateTime date)

        Returns the given ZonedDateTime in ISO format that readable by a wide range of clients.

        In general the format has these components:

        • A date: yyyy '-' MM '-' dd
        • A time: 'T' HH ':' mm ':' ss
        • Milliseconds: ('.' | ',') digit+
        • UTC offset: 'Z'
        Example: 2017-12-29T03:21:24.564000000Z
        Parameters:
        date - a zoned date/time value
        Returns:
        the given ZonedDateTime in ISO format
        See Also:
        dateOptionalTimeParser, strict_date_optional_time
      • toUtc

        public static java.time.ZonedDateTime toUtc​(java.time.ZonedDateTime date)
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(java.util.Date date)
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(java.time.Instant instant)
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(int year,
                                                                 java.time.Month month,
                                                                 int dayOfMonth)
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(java.time.LocalDate date)
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(long epoch)
        Returns a ZonedDateTime from the given epoch value. Note that this implementation attempts to protect against caller providing timestamps in different units. For example, Unix timestamps are the number of SECONDS since January 1, 1970, while Java Dates are number of MILLISECONDS since January 1, 1970.
        Parameters:
        epoch - timestamp value in seconds, milliseconds or microseconds
        Returns:
        a ZonedDateTime or null if the date is not valid
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(long epochValue,
                                                                 DateUtils.EpochUnits units)
        Returns a ZonedDateTime from the given epoch value.
        Parameters:
        epochValue - value in milliseconds
        units - epoch units
        Returns:
        a ZonedDateTime or null if the date is not valid
      • toZonedDateTimeUtc

        public static java.time.ZonedDateTime toZonedDateTimeUtc​(java.lang.String text)
      • toZonedDateTimeUtcChecked

        public static java.time.ZonedDateTime toZonedDateTimeUtcChecked​(java.lang.String text)
                                                                 throws java.time.format.DateTimeParseException
        Throws:
        java.time.format.DateTimeParseException
      • withDatePrecision

        public static java.time.ZonedDateTime withDatePrecision​(java.time.ZonedDateTime date)
      • yearMonthDay

        public static java.time.format.DateTimeFormatter yearMonthDay​(java.time.format.TextStyle monthStyle,
                                                                      java.lang.String yearSeparator,
                                                                      java.lang.String monthSeparator,
                                                                      int yearDigits,
                                                                      boolean time)