Class DateUtil


  • public final class DateUtil
    extends Object
    Manages timestamp strings in ISO8601 format.

    Example:
    2011-09-09T14:58:17.6+02:00
    2011-09-09T14:58:17.65+02:00
    2011-09-09T14:58:17.654+02:00
    2011-09-09T14:58:17.654Z

    • Method Detail

      • getDateUtil

        public static DateUtil getDateUtil()
        Fetches a DateUtil that is set in the default timezone.
        Returns:
        DateUtil set in the default timezone
      • getDateUtil

        public static DateUtil getDateUtil​(String zone)
        Returns a DateUtil using a specified timezone.
        Parameters:
        zone - string representation of a timezone i.e. "UTC" or "Asia/Tokyo"
        Returns:
        DateUtil set with the supplied timezone
      • getDateUtil

        public static DateUtil getDateUtil​(org.joda.time.DateTimeZone zone)
        Creates a DateUtil using a specified timezone.
        Parameters:
        zone - DateTimeZone object
        Returns:
        DateUtil set with the supplied timezone
      • now

        public String now()
        Generate a formatted timestamp for the current time.
        Returns:
        String containing a timestamp
      • currentDateTime

        public org.joda.time.DateTime currentDateTime()
        Get current time.
        Returns:
        get current time as a DateTime object
      • formatDateTime

        public String formatDateTime​(org.joda.time.DateTime date)
        Formats a given DateTime into a timestamp.
        Parameters:
        date - DateTime object to convert
        Returns:
        String containing the formatted timestamp
      • formatDateTime

        public String formatDateTime​(Date date)
        Formats a given date into a timestamp.
        Parameters:
        date - date object to convert
        Returns:
        String containing the formatted timestamp
      • formatDateTime

        public String formatDateTime​(long timestamp)
        Formats a given timestamp into a ISO8601 timestamp.
        Parameters:
        timestamp - the timestamp to convert
        Returns:
        String containing the formatted timestamp
      • parseTimestamp

        public org.joda.time.DateTime parseTimestamp​(String timestamp)
        Parses an ISO8601 compliant timestamp into a DateTime object.
        Parameters:
        timestamp - timestamp to parse
        Returns:
        DateTime using the zone and chronology indicated by the timestamp
      • parseIfDate

        public org.joda.time.DateTime parseIfDate​(String timestamp)
        Parses an ISO8601 compliant timestamp into a DateTime object. Checks the length of the timestamp and returns null if the string is not ISO8601 compliant Accepted formats:
         yyyy-MM-ddTHH:mm:ss.SSSZ,
         yyyy-MM-ddTHH:mm:ss.SSS+00,
         yyyy-MM-ddTHH:mm:ss.SSS+00:00
         
        Parameters:
        timestamp - timestamp to parse
        Returns:
        the timestamp as a DateTime object
      • getDateDifferenceInDays

        public static int getDateDifferenceInDays​(Date start,
                                                  Date end,
                                                  Boolean includeDay)
        return the number of days between the two dates.
        Parameters:
        start - Start date
        end - End date
        includeDay - include both Days (increase the result with one)
        Returns:
        number of days