Package nl.colorize.util
Class DateParser
java.lang.Object
nl.colorize.util.DateParser
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 Summary
Modifier and TypeMethodDescriptionstatic 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.static long
delta
(Date a, Date b, ChronoUnit unit) Returns the difference between two dates, expressed in the specified time unit.static String
Convenience method that wraps aroundSimpleDateFormat.format
and formats a date using the default time zone.static String
Convenience method that wraps aroundSimpleDateFormat.format
and formats a date in the time zone with the specified name.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.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.static TranslationBundle
Returns the translations that are used by this class for display names.static Date
Parses a date while attempting to automatically detect the date format for the specified input string.static Date
Wrapper aroundSimpleDateFormat.parse
to parse a date using the specified date format in a thread-safe way.static LocalDate
parseLocalDate
(String value) Uses the same logic asparse(String)
, but returns the result as aLocalDate
in the default time zone.static LocalDateTime
parseLocalDateTime
(String value) Uses the same logic asparse(String)
, but returns the result as aLocalDateTime
in the default time zone.Wrapper aroundSimpleDateFormat.parse
to parse a date using the specified date format in a thread-safe way.
-
Method Details
-
tryParse
Wrapper aroundSimpleDateFormat.parse
to parse a date using the specified date format in a thread-safe way. -
parse
Wrapper aroundSimpleDateFormat.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
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
Uses the same logic asparse(String)
, but returns the result as aLocalDate
in the default time zone. -
parseLocalDateTime
Uses the same logic asparse(String)
, but returns the result as aLocalDateTime
in the default time zone. -
format
Convenience method that wraps aroundSimpleDateFormat.format
and formats a date using the default time zone. -
format
Convenience method that wraps aroundSimpleDateFormat.format
and formats a date in the time zone with the specified name. -
add
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
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
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
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
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.
-