Package org.kiwiproject.time
Class KiwiDateTimeParsers
- java.lang.Object
-
- org.kiwiproject.time.KiwiDateTimeParsers
-
public class KiwiDateTimeParsers extends Object
A collection of small utilities to parse strings into various types of Java date/time classes, both the legacyDateand the Java 8 date/time API classes injava.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, seeKiwiDateTimeFormatters.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
IllegalArgumentExceptionifnullor blank arguments are passed to them.
-
-
Constructor Summary
Constructors Constructor Description KiwiDateTimeParsers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 Detail
-
parseAsDate
public static Date parseAsDate(String dateTimeString)
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
public static LocalDate parseAsLocalDate(String dateString)
Converts the given date string into aLocalDateusingDateTimeFormatter.ISO_LOCAL_DATEas the formatter.- Parameters:
dateString- the date string to parse- Returns:
- the parsed
LocalDate
-
parseAsLocalDate
public static LocalDate parseAsLocalDate(String dateString, DateTimeFormatter formatter)
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:
DateTimeFormatter.parse(CharSequence, TemporalQuery)
-
parseAsLocalDateTime
public static LocalDateTime parseAsLocalDateTime(String dateTimeString)
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:
DateTimeFormatter.parse(CharSequence, TemporalQuery)
-
parseAsZonedDateTime
public static ZonedDateTime parseAsZonedDateTime(String dateTimeString)
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:
DateTimeFormatter.parse(CharSequence, TemporalQuery)
-
-