Class DateParser

java.lang.Object
nl.colorize.util.DateParser

public final class DateParser extends Object
Convenience functions for parsing, formatting, and working with dates. The Date and SimpleDateFormat are often inconvenient to use. For example, SimpleDateFormat is not thread safe, and parsing dates throws a checked exception.

If a date string does not contain an explicit time zone, the default time zone will be used. See Platform.getDefaultTimeZone() for how to configure the default time zone.

Note: The java.time API introduced in Java 8 pretty much solves most of the usability problems with the old date and calendar APIs. However, this class is not deprecated because java.time is only partially available on some platforms and Java implementations.

  • Method Details

    • tryParse

      public static Optional<Date> tryParse(String input, String dateFormat)
      Wrapper around SimpleDateFormat.parse to parse a date using the specified date format in a thread-safe way.
    • parse

      public static Date parse(String input, String dateFormat)
      Wrapper around SimpleDateFormat.parse to parse a date using the specified date format in a thread-safe way.
      Throws:
      IllegalArgumentException - if the specified input string does not follow the date format.
    • parse

      public static Date parse(String input)
      Parses a date while attempting to automatically detect the date format for the specified input string.
      Throws:
      IllegalArgumentException - if the input string does not confirm to any of the date formats supported by this method.
    • parseLocalDate

      public static LocalDate parseLocalDate(String value)
      Uses the same logic as parse(String), but returns the result as a LocalDate in the default time zone.
    • parseLocalDateTime

      public static LocalDateTime parseLocalDateTime(String value)
      Uses the same logic as parse(String), but returns the result as a LocalDateTime in the default time zone.
    • format

      public static String format(Date date, String dateFormat)
      Convenience method that wraps around SimpleDateFormat.format and formats a date using the default time zone.
    • format

      public static String format(Date date, String dateFormat, String timeZone)
      Convenience method that wraps around SimpleDateFormat.format and formats a date in the time zone with the specified name.
    • add

      public static Date add(Date original, ChronoUnit unit, int amount)
      Returns a new date that is created by adding the specified time unit to the original date. The amount can be negative, which will return a date that is before the original.

      This method supports the following time units:

      Throws:
      IllegalArgumentException - if the provided time unit is not in the list of supported time units.
    • delta

      public static long delta(Date a, Date b, ChronoUnit unit)
      Returns the difference between two dates, expressed in the specified time unit. The difference is absolute, so it doesn't matter whether the first or second argument is more recent. The value is rounded, so a delta of 11 days with a unit of weeks will return 2.
    • formatRelative

      public static String formatRelative(Date date, Date reference)
      Formats a date relative to another date, with the precision being decided by the distance between the two dates. Examples of returned values are "2 hours ago", "yesterday", and "3 weeks ago".
    • formatRelative

      public static String formatRelative(Date date)
      Formats a date relative to the current date, with the precision being decided by the distance between the two dates. Examples of returned values are "2 hours ago", "yesterday", and "3 weeks ago".
    • getTranslationBundle

      public static TranslationBundle getTranslationBundle()
      Returns the translations that are used by this class for display names. By default, only English is supported, but this can be extended by adding additional translations.