Class DateUtils


  • public final class DateUtils
    extends Object
    Date utility class
    Author:
    bas.rutten
    • Method Detail

      • createJava8Date

        public static <T> T createJava8Date​(Class<T> clazz,
                                            String dateStr,
                                            String format)
      • createLocalDate

        public static LocalDate createLocalDate​(String dateStr)
        Creates a java.time.LocalTime based on a String representation
        Parameters:
        dateStr - the String (in the format ddMMyyyy)
        Returns:
      • createLocalDate

        public static LocalDate createLocalDate​(String dateStr,
                                                String format)
        Creates a java.time.LocalTime based on a String representation
        Parameters:
        dateStr - the String
        format - the desired format
        Returns:
      • createLocalDateTime

        public static LocalDateTime createLocalDateTime​(String dateTimeStr)
        Creates a java.time.LocalDateTime based on a String representation
        Parameters:
        dateTimeStr - the String representation (ddMMyyyy HHmmss)
        Returns:
      • createLocalDateTime

        public static LocalDateTime createLocalDateTime​(String dateTimeStr,
                                                        String format)
        Creates a java.time.LocalDateTime based on a String representation
        Parameters:
        dateTimeStr - the String representation
        format - the desired format
        Returns:
      • createLocalTime

        public static LocalTime createLocalTime​(String timeStr)
        Creates a java.time.LocalTime based on a String representation
        Parameters:
        timeStr - the String representation
        Returns:
      • createLocalTime

        public static LocalTime createLocalTime​(String timeStr,
                                                String format)
        Creates a java.time.LocalTime based on a String representation
        Parameters:
        timeStr - the String representation
        format - the desired format
        Returns:
      • createZonedDateTime

        public static ZonedDateTime createZonedDateTime​(String dateTimeStr)
        Create a ZonedDateTime based from a String, using the default format
        Parameters:
        dateTimeStr - the String to parse
        Returns:
      • createZonedDateTime

        public static ZonedDateTime createZonedDateTime​(String dateTimeStr,
                                                        String format)
        Creates a ZonedDateTime from a String, using the provided format
        Parameters:
        dateTimeStr - the String
        format - the format
        Returns:
      • formatDate

        public static String formatDate​(LocalDate date,
                                        String format)
        Formats a LocalDate according to the specified format
        Parameters:
        date - the date
        format - the format
        Returns:
      • formatDateTime

        public static String formatDateTime​(LocalDateTime dateTime,
                                            String format)
        Formats a LocalDateTime according to the specified format
        Parameters:
        dateTime - the DateTime to format
        format - the desired format
        Returns:
      • formatJava8Date

        public static String formatJava8Date​(Class<?> clazz,
                                             Object value,
                                             String format)
        Formats a Java 8 date/time/datetime based on the specified format
        Parameters:
        clazz - the class of the object to format
        value - the value
        format - the desired format
        Returns:
      • formatTime

        public static String formatTime​(LocalTime time,
                                        String format)
        Formats a java.time.LocalTime according to the specified format
        Parameters:
        time - the objec to format
        format - the desired format
        Returns:
      • formatZonedDateTime

        public static String formatZonedDateTime​(ZonedDateTime dateTime,
                                                 String format)
        Formats a ZonedDateTime according to the specified format
        Parameters:
        dateTime - the DateTime to format
        format - the desired format
        Returns:
      • getLastWeekOfYear

        public static int getLastWeekOfYear​(int year)
        Return the week number (1 - 53) of the last week of the specified year
        Parameters:
        year - the year
        Returns:
      • getNextWeekCode

        public static String getNextWeekCode​(String weekCode)
        Returns the next week code given an existing week code
        Parameters:
        weekCode - the week code
        Returns:
      • getQuarter

        public static int getQuarter​(LocalDate date)
        Returns the quarter of the year of a date, as an integer (1 to 4). Returns -1 in case the argument passed to this function is null
        Parameters:
        date - the date
        Returns:
      • getStartDateOfWeek

        public static LocalDate getStartDateOfWeek​(String weekCode)
        Translates a week code (yyyy-ww) to the starting day (this is taken to be a Monday) of that week
        Parameters:
        weekCode - the week code
        Returns:
        the date
      • isJava8DateType

        public static boolean isJava8DateType​(Class<?> clazz)
        Çhecks whether a class represents a Java 8 date or time type
        Parameters:
        clazz - the class
        Returns:
      • isSupportedDateType

        public static boolean isSupportedDateType​(Class<?> clazz)
        Checks if a class represents a supported Date time. This includes the Java 8 date types and the legacy java.time.LocalTime
        Parameters:
        clazz - the class to check
        Returns:
      • isValidWeekCode

        public static boolean isValidWeekCode​(String weekCode)
        Checks if a string represents a valid week code (yyyy-ww). An empty String is considered valid
        Parameters:
        weekCode - the week code
        Returns:
      • toLegacyDate

        public static Date toLegacyDate​(LocalDate d)
        Converts a LocalDate to a legacy java.time.LocalTime
        Parameters:
        d - the LocalDate to convert
        Returns:
      • toLegacyDate

        public static Date toLegacyDate​(LocalDateTime ldt)
        Converts a java.time.LocalDateTime to a java.time.LocalTime
        Parameters:
        d - the LocalDatetime to convert
        Returns:
      • toWeekCode

        public static String toWeekCode​(LocalDate date)
        Converts a date to its corresponding week code
        Parameters:
        date - the date
        Returns: