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 Dateadd(Date original, ChronoUnit unit, int amount) Returns a new date that is created by adding the specified time unit to the original date.static longdelta(Date a, Date b, ChronoUnit unit) Returns the difference between two dates, expressed in the specified time unit.static StringConvenience method that wraps aroundSimpleDateFormat.formatand formats a date using the default time zone.static StringConvenience method that wraps aroundSimpleDateFormat.formatand formats a date in the time zone with the specified name.static StringformatRelative(Date date) Formats a date relative to the current date, with the precision being decided by the distance between the two dates.static StringformatRelative(Date date, Date reference) Formats a date relative to another date, with the precision being decided by the distance between the two dates.static Datestatic Datestatic DateParses a date while attempting to automatically detect the date format for the specified input string.static DateWrapper aroundSimpleDateFormat.parseto parse a date using the specified date format in a thread-safe way.static LocalDateparseLocalDate(String value) Uses the same logic asparse(String), but returns the result as aLocalDatein the default time zone.static LocalDateTimeparseLocalDateTime(String value) Uses the same logic asparse(String), but returns the result as aLocalDateTimein the default time zone.static LocalDatetoLocalDate(Date date) Returns aLocalDatebased on the specified value, in the default time zone as specified byPlatform.getDefaultTimeZoneId().static LocalDateTimetoLocalDateTime(Date date) Returns aLocalDateTimebased on the specified value, in the default time zone as specified byPlatform.getDefaultTimeZoneId().Wrapper aroundSimpleDateFormat.parseto parse a date using the specified date format in a thread-safe way.
-
Method Details
-
tryParse
-
parse
Wrapper aroundSimpleDateFormat.parseto 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 aLocalDatein the default time zone. -
parseLocalDateTime
Uses the same logic asparse(String), but returns the result as aLocalDateTimein the default time zone. -
format
-
format
-
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.
-
min
-
max
-
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
-
formatRelative
-
toLocalDate
Returns aLocalDatebased on the specified value, in the default time zone as specified byPlatform.getDefaultTimeZoneId(). -
toLocalDateTime
Returns aLocalDateTimebased on the specified value, in the default time zone as specified byPlatform.getDefaultTimeZoneId().
-