Class DateUtils


  • public class DateUtils
    extends java.lang.Object
    Utility class to work with timestamps or dates. This class allows parsing and formatting of timestamps in ISO 8601 format, and some utility methods such as to determine whether two timestamps correspond to the same date, etc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String DATE_FORMAT
      Date format without separators.
      private static java.lang.String TIMESTAMP_NO_SEPARATORS_FORMAT
      Timestamp format without date and time separators.
      private static java.lang.String TIMESTAMP_SEPARATORS_FORMAT
      Timestamp format with date and time separators.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected DateUtils()
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String formatDate​(java.util.Date date)
      Formats a date into a string using medium format.
      static java.lang.String formatDateAndTime​(java.util.Date date)
      Formats date and time as a string using short format.
      static boolean isSameDay​(java.util.Calendar cal1, java.util.Calendar cal2)
      Indicates if both provided calendar instances have the same calendar date (even if they have different times set).
      static boolean isSameDay​(java.util.Date date1, java.util.Date date2)
      Indicates whether two date/time instances correspond to the same day (same calendar date).
      static boolean isToday​(java.util.Date date)
      Indicates whether provided date and time instances correspond to today taking into account local timezone.
      static java.util.Date parse​(java.lang.String date)
      Parses a string representing a date or timestamp in ISO 8601 format.
      static java.util.Date today()
      Obtains a date/instance set to current date at midnight.
      • Methods inherited from class java.lang.Object

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

      • TIMESTAMP_NO_SEPARATORS_FORMAT

        private static final java.lang.String TIMESTAMP_NO_SEPARATORS_FORMAT
        Timestamp format without date and time separators.
        See Also:
        Constant Field Values
      • TIMESTAMP_SEPARATORS_FORMAT

        private static final java.lang.String TIMESTAMP_SEPARATORS_FORMAT
        Timestamp format with date and time separators.
        See Also:
        Constant Field Values
      • DATE_FORMAT

        private static final java.lang.String DATE_FORMAT
        Date format without separators.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DateUtils

        protected DateUtils()
        Constructor.
    • Method Detail

      • parse

        public static java.util.Date parse​(java.lang.String date)
        Parses a string representing a date or timestamp in ISO 8601 format.
        Parameters:
        date - date or timestamp expressed as a String in ISO 8601 format.
        Returns:
        a date object or null if string could not be parsed.
      • formatDate

        public static java.lang.String formatDate​(java.util.Date date)
        Formats a date into a string using medium format.
        Parameters:
        date - a date.
        Returns:
        a date formatted in medium format.
      • formatDateAndTime

        public static java.lang.String formatDateAndTime​(java.util.Date date)
        Formats date and time as a string using short format.
        Parameters:
        date - date and time.
        Returns:
        a date and time formatted in short format.
      • today

        public static java.util.Date today()
        Obtains a date/instance set to current date at midnight.
        Returns:
        current date at midnight.
      • isSameDay

        public static boolean isSameDay​(java.util.Date date1,
                                        java.util.Date date2)
        Indicates whether two date/time instances correspond to the same day (same calendar date).
        Parameters:
        date1 - 1st date to check.
        date2 - 2nd date to check.
        Returns:
        true if both dates are the same, false otherwise.
      • isSameDay

        public static boolean isSameDay​(java.util.Calendar cal1,
                                        java.util.Calendar cal2)
        Indicates if both provided calendar instances have the same calendar date (even if they have different times set).
        Parameters:
        cal1 - 1st calendar to check.
        cal2 - 2nd calendar to check.
        Returns:
        true if both calendars have the same date, false otherwise.
      • isToday

        public static boolean isToday​(java.util.Date date)
        Indicates whether provided date and time instances correspond to today taking into account local timezone.
        Parameters:
        date - date and time to check.
        Returns:
        true if date/time corresponds to today, false otherwise.