Package org.kiwiproject.time
Class KiwiDateTimeParsers
java.lang.Object
org.kiwiproject.time.KiwiDateTimeParsers
A collection of small utilities to parse strings into various types of Java date/time classes, both the legacy
Date and the Java 8 date/time API classes in java.time.
Please make sure you actually read the method documentation, so you know the format(s) that are expected or allowed.
While the JDK has decided to keep the formatting and parsing methods together in DateTimeFormatter,
we decided to split them into separate utilities. So if you are looking for utilities to format date/time objects
into strings, see KiwiDateTimeFormatters.
None of these are difficult to implement, but if you are constantly doing them, the time and code adds up over time.
All methods throw IllegalArgumentException if null or blank arguments are passed to them.
-
Method Summary
Modifier and TypeMethodDescriptionstatic DateparseAsDate(String dateTimeString) Converts the given date/time string into aDateusingDateTimeFormatter.ISO_ZONED_DATE_TIMEas the formatter.static LocalDateparseAsLocalDate(String dateString) Converts the given date string into aLocalDateusingDateTimeFormatter.ISO_LOCAL_DATEas the formatter.static LocalDateparseAsLocalDate(String dateString, DateTimeFormatter formatter) Converts the given date string into aLocalDateusing the given formatter.static LocalDateTimeparseAsLocalDateTime(String dateTimeString) Converts the given date/time string into aLocalDateTimeusingDateTimeFormatter.ISO_LOCAL_DATE_TIMEas the formatter.static LocalDateTimeparseAsLocalDateTime(String dateTimeString, DateTimeFormatter formatter) Converts the given date/time string into aLocalDateTimeusing the given formatter.static ZonedDateTimeparseAsZonedDateTime(String dateTimeString) Converts the given date/time string into aZonedDateTimeusingDateTimeFormatter.ISO_ZONED_DATE_TIMEas the formatter.static ZonedDateTimeparseAsZonedDateTime(String dateTimeString, DateTimeFormatter formatter) Converts the given date/time string into aZonedDateTimeusing the specifiedDateTimeFormatter.
-
Method Details
-
parseAsDate
Converts the given date/time string into aDateusingDateTimeFormatter.ISO_ZONED_DATE_TIMEas the formatter.- Parameters:
dateTimeString- the date/time string to parse- Returns:
- the parsed
Date - Implementation Note:
- If you need to parse a string into a custom format, you'll need to use
SimpleDateFormat(which is NOT thread safe, so make sure to use in aThreadLocalor simply instantiate a new one each time you need to parse)
-
parseAsLocalDate
Converts the given date string into aLocalDateusingDateTimeFormatter.ISO_LOCAL_DATEas the formatter.- Parameters:
dateString- the date string to parse- Returns:
- the parsed
LocalDate
-
parseAsLocalDate
Converts the given date string into aLocalDateusing the given formatter.- Parameters:
dateString- the date string to parseformatter- the formatter to use- Returns:
- the parsed
LocalDate - See Also:
-
parseAsLocalDateTime
Converts the given date/time string into aLocalDateTimeusingDateTimeFormatter.ISO_LOCAL_DATE_TIMEas the formatter.- Parameters:
dateTimeString- the date/time string to parse- Returns:
- the parsed
LocalDate
-
parseAsLocalDateTime
public static LocalDateTime parseAsLocalDateTime(String dateTimeString, DateTimeFormatter formatter) Converts the given date/time string into aLocalDateTimeusing the given formatter.- Parameters:
dateTimeString- the date/time string to parseformatter- the formatter to use- Returns:
- the parsed
LocalDateTime - See Also:
-
parseAsZonedDateTime
Converts the given date/time string into aZonedDateTimeusingDateTimeFormatter.ISO_ZONED_DATE_TIMEas the formatter.- Parameters:
dateTimeString- the date/time string to parse- Returns:
- the parsed
ZonedDateTime
-
parseAsZonedDateTime
public static ZonedDateTime parseAsZonedDateTime(String dateTimeString, DateTimeFormatter formatter) Converts the given date/time string into aZonedDateTimeusing the specifiedDateTimeFormatter.- Parameters:
dateTimeString- the date/time string to parseformatter- the formatter to use- Returns:
- the parsed
ZonedDateTime - See Also:
-