Class DateUtil


  • public class DateUtil
    extends Object
    Utility methods that will make it easier to handle date values. Especially, dates must be cloned whenver they are returned to or passed from client applications.
    Version:
    2.0
    Author:
    Nicklas
    • Constructor Detail

      • DateUtil

        public DateUtil()
    • Method Detail

      • setNullableDate

        public static Date setNullableDate​(Date value,
                                           String name)
        Make a copy of a Date object. Null values are allowed.
        Parameters:
        value - The date to check
        name - The name of the attribute to use if an exception is thrown
        Returns:
        A copy of the original date
      • setNotNullDate

        public static Date setNotNullDate​(Date value,
                                          String name)
                                   throws InvalidUseOfNullException
        Make a copy of a Date object. Null values are not allowed.
        Parameters:
        value - The date to check. Can not be null.
        name - The name of the attribute to use if an exception is thrown
        Returns:
        A copy of the original date
        Throws:
        InvalidUseOfNullException - If the value parameter is null.
      • setDateOrToday

        public static Date setDateOrToday​(Date value)
        Make a copy of the date. If null, today's date is used instead.
        Since:
        3.11
      • copy

        public static Date copy​(Date value)
        Get a copy of the date. Null values are allowed.
        Parameters:
        value - Date value to copy
        Returns:
        A cloned Date object or null if argument is null.
      • truncate

        public static Date truncate​(Date value)
        Truncate the given date so that only the date part remains. The time part is set to 00:00:00.0
        Parameters:
        value - The date to truncate, can be null
        Returns:
        A new date with time part set to 0 or null
        Since:
        2.16
      • parseString

        public static Date parseString​(String value)
                                throws InvalidDataException
        Parses a string to create a Date without time. This method supports date in yyyy-MM-dd format or as long timevalues.
        Parameters:
        value - the String to be parsed
        Returns:
        a Date object
        Throws:
        DateFormatException - if value isn't a valid date.
        InvalidDataException
      • formatDate

        public static String formatDate​(Date d)
        Formats a date in yyyy-MM-dd format.
        Parameters:
        d - The date to format.
        Returns:
        Formated date as a String.
        Since:
        2.2
      • parseTimestamp

        public static Date parseTimestamp​(String value)
                                   throws InvalidDataException
        Parses a string to create a Date including time. This method supports date in yyyy-MM-dd HH:mm:ss format or as long timevalues.
        Parameters:
        value - the String to be parsed
        Returns:
        a Date object
        Throws:
        DateFormatException - if value isn't a valid timestamp
        InvalidDataException
        Since:
        2.16
      • formatTimestamp

        public static String formatTimestamp​(Date d)
        Formats a timestamp in yyyy-MM-dd HH:mm:ss format.
        Parameters:
        d - The date to format.
        Returns:
        Formated date as a String
        Since:
        2.16
      • safeEquals

        public static boolean safeEquals​(Date d1,
                                         Date d2,
                                         boolean time)
        Compare two date instances for equality while protecting from strange implementations in the java.util.Date and java.sql.Timestamp classes.
        Parameters:
        time - TRUE to compare the date+time part, FALSE to only compare the date part
        Since:
        3.9.1
      • safeInvariantEquals

        public static boolean safeInvariantEquals​(List<Date> l1,
                                                  List<Date> l2,
                                                  boolean time)
        Checks if two lists contains equal elements disregarding their positions in the array. Eg. [1, 2] is equal to [2, 1]
        Parameters:
        time - TRUE to compare the date+time part, FALSE to only compare the date part
        Returns:
        TRUE if both list contains the same elements
        Since:
        3.9.1