Package org.kiwiproject.time
Class KiwiDateTimeConverters
java.lang.Object
org.kiwiproject.time.KiwiDateTimeConverters
A collection of small utility methods to convert between legacy
Date and the Java 8 date/time API classes.
Mainly, these utilities save you some typing and having to remember the chains of method calls you need to make when doing these conversions.
All methods throw IllegalArgumentException if null values are passed into them.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Datestatic DatetoDate(LocalDateTime localDateTime) Convert the givenLocalDateTimeinto its equivalentDate(epoch milliseconds using UTC).static DatetoDate(ZonedDateTime zonedDateTime) Convert the givenZonedDateTimeinto its equivalentDate(epoch milliseconds using UTC).static @Nullable InstanttoInstantOrNull(@Nullable Calendar calendar) static @Nullable InstanttoInstantOrNull(@Nullable Date date) static LocalDatetoLocalDate(Date date, ZoneId zoneId) static LocalDateTimetoLocalDateTime(Date date, ZoneId zoneId) Convert the givenDateinto its equivalentLocalDateTimein the specified time zone.static LocalDateTimetoLocalDateTimeUTC(Date date) Convert the givenDateinto its equivalentLocalDateTimein the UTC time zone.static LocalDatetoLocalDateUTC(Date date) static ZonedDateTimetoZonedDateTime(Date date, ZoneId zoneId) Convert the givenDateinto its equivalentZonedDateTimein the specified time zone.static @Nullable ZonedDateTimetoZonedDateTimeOrNull(@Nullable GregorianCalendar calendar) static ZonedDateTimetoZonedDateTimeUTC(Date date) Convert the givenDateinto its equivalentZonedDateTimein the UTC time zone.
-
Method Details
-
toDate
-
toDate
Convert the givenLocalDateTimeinto its equivalentDate(epoch milliseconds using UTC).- Parameters:
localDateTime- theLocalDateTimeto convert- Returns:
- the converted
Date
-
toDate
Convert the givenZonedDateTimeinto its equivalentDate(epoch milliseconds using UTC).- Parameters:
zonedDateTime- theZonedDateTimeto convert- Returns:
- the converted
Date
-
toInstantOrNull
Return theInstantconverted fromdate, ornullifdateisnull.This is a null-safe wrapper around
Date.toInstant(), and is useful in situations when you might have anulldate.- Parameters:
date- theDateto convert, may be null- Returns:
- the converted
Instantornull - Implementation Note:
- This method only exists because
Date.toInstant()is an instance method and obviously aNullPointerExceptionis thrown if you attempt to call it when the receiver isnull, i.e.maybeNullDate.toInstant().
-
toInstantOrNull
Return theInstantconverted fromcalendar, ornullifcalendarisnull.This is a null-safe wrapper around
Calendar.toInstant(), and is useful in situations when you might have anullcalendar.- Parameters:
calendar- theCalendarto convert, may be null- Returns:
- the converted
Instantornull - Implementation Note:
- This method only exists because
Calendar.toInstant()is an instance method and obviously aNullPointerExceptionis thrown if you attempt to call it when the receiver isnull, i.e.maybeNullCalendar.toInstant().
-
toLocalDateUTC
-
toLocalDate
-
toLocalDateTimeUTC
Convert the givenDateinto its equivalentLocalDateTimein the UTC time zone.- Parameters:
date- theDateto convert- Returns:
- the converted
LocalDateTime - See Also:
-
toLocalDateTime
Convert the givenDateinto its equivalentLocalDateTimein the specified time zone.- Parameters:
date- theDateto convertzoneId- the time zone as aZoneId- Returns:
- the converted
LocalDateTime
-
toZonedDateTimeUTC
Convert the givenDateinto its equivalentZonedDateTimein the UTC time zone.- Parameters:
date- theDateto convert- Returns:
- the converted
ZonedDateTime - See Also:
-
toZonedDateTime
Convert the givenDateinto its equivalentZonedDateTimein the specified time zone.- Parameters:
date- theDateto convertzoneId- the time zone as aZoneId- Returns:
- the converted
ZonedDateTime
-
toZonedDateTimeOrNull
Return theZonedDateTimeconverted fromcalendar, ornullifcalendarisnull.This is a null-safe wrapper around
GregorianCalendar.toZonedDateTime(), and is useful in situations when you might have anullcalendar.- Parameters:
calendar- theGregorianCalendarto convert, may be null- Returns:
- the converted
ZonedDateTimeornull - Implementation Note:
- This method only exists because
GregorianCalendar.toZonedDateTime()is an instance method and obviously aNullPointerExceptionis thrown if you attempt to call it when the receiver isnull, i.e.maybeNullGregorianCalendar.toZonedDateTime(). Note also thattoZonedDateTimeis only inGregorianCalendar, not the baseCalendar, which is the reason this accepts aGregorianCalendarargument.
-