Package org.kiwiproject.time
Class KiwiDateTimeFormatters
java.lang.Object
org.kiwiproject.time.KiwiDateTimeFormatters
A collection of small utilities to format various types of Java date/time classes, both the legacy
Date and the Java 8 date/time API classes in java.time.
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 parse strings into
date/time objects, see KiwiDateTimeParsers.
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 arguments are passed to them.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatAsIsoLocalDate(TemporalAccessor dateTime) Converts the givenTemporalAccessorinto a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.static StringformatAsIsoLocalDate(Date date, ZoneId targetZoneId) Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.static StringformatAsIsoLocalDateTime(LocalDate localDate) Converts the givenLocalDate(at the start of the day) into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.static StringformatAsIsoLocalDateTime(TemporalAccessor dateTime) Converts the givenTemporalAccessor(at the start of the day) into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.static StringformatAsIsoLocalDateTime(Date date, ZoneId targetZoneId) Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.static StringConverts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.static StringformatAsIsoLocalDateUTC(Date date) Converts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.static StringformatAsIsoZonedDateTime(LocalDate localDate, ZoneId targetZoneId) Converts the givenLocalDate(at the start of the day) in the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTime(LocalDateTime localDateTime, ZoneId targetZoneId) Converts the givenLocalDateTimein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTime(ZonedDateTime zonedDateTime, ZoneId targetZoneId) Converts the givenZonedDateTimein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTime(Date date, ZoneId targetZoneId) Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTimeUTC(LocalDate localDate) Converts the givenLocalDate(at the start of the day) in the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTimeUTC(LocalDateTime localDateTime) Converts the givenLocalDateTimein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatAsIsoZonedDateTimeUTC(ZonedDateTime zonedDateTime) Converts the givenZonedDateTimein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringConverts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.static StringformatWith(LocalDateTime localDateTime, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenLocalDateTime(assuming it is in the UTC time zone) into a string using the givenDateTimeFormatter.static StringformatWith(LocalDateTime localDateTime, ZoneId localZoneId, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenLocalDateTime(assuming it is in the givenlocalZoneIdtime zone) into a string using the givenDateTimeFormatter.static StringformatWith(TemporalAccessor temporalAccessor, DateTimeFormatter formatter) Converts the givenTemporalAccessorinto a string using the givenDateTimeFormatter.static StringformatWith(ZonedDateTime zonedDateTime, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenZonedDateTimein the given time zone into a string using the givenDateTimeFormatter.static StringformatWith(Date date, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenDatein the given time zone into a string using the givenDateTimeFormatter.static StringformatWithUTC(Date date, DateTimeFormatter formatter) Converts the givenDatein the UTC time zone into a string using the givenDateTimeFormatter.
-
Method Details
-
formatAsIsoLocalDateUTC
Converts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.- Parameters:
date- the date to convert- Returns:
- the formatted date string
-
formatAsIsoLocalDate
Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.- Parameters:
date- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date string
-
formatAsIsoLocalDate
Converts the givenTemporalAccessorinto a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.- Parameters:
dateTime- the date/time to convert- Returns:
- the formatted date string
-
formatAsIsoLocalDateTimeUTC
Converts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.- Parameters:
date- the date to convert- Returns:
- the formatted date/time string
-
formatAsIsoLocalDateTime
Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.- Parameters:
date- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
-
formatAsIsoLocalDateTime
Converts the givenLocalDate(at the start of the day) into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.- Parameters:
localDate- the date to convert- Returns:
- the formatted date/time string
- See Also:
-
formatAsIsoLocalDateTime
Converts the givenTemporalAccessor(at the start of the day) into a string using theDateTimeFormatter.ISO_LOCAL_DATE_TIMEformatter.- Parameters:
dateTime- the date/time to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTimeUTC
Converts the givenDatein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
date- the date to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTime
Converts the givenDatein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
date- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTimeUTC
Converts the givenLocalDate(at the start of the day) in the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
localDate- the date to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTime
Converts the givenLocalDate(at the start of the day) in the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
localDate- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
- See Also:
-
formatAsIsoZonedDateTimeUTC
Converts the givenLocalDateTimein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
localDateTime- the date/time to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTime
Converts the givenLocalDateTimein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
localDateTime- the date/time to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTimeUTC
Converts the givenZonedDateTimein the UTC time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
zonedDateTime- the date/time to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTime
Converts the givenZonedDateTimein the given time zone into a string using theDateTimeFormatter.ISO_ZONED_DATE_TIMEformatter.- Parameters:
zonedDateTime- the date/time to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
-
formatWithUTC
Converts the givenDatein the UTC time zone into a string using the givenDateTimeFormatter.- Parameters:
date- the date to convertformatter- the specific formatter to use- Returns:
- the formatted date string
-
formatWith
Converts the givenDatein the given time zone into a string using the givenDateTimeFormatter.- Parameters:
date- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date residesformatter- the specific formatter to use- Returns:
- the formatted date string
-
formatWith
public static String formatWith(LocalDateTime localDateTime, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenLocalDateTime(assuming it is in the UTC time zone) into a string using the givenDateTimeFormatter.- Parameters:
localDateTime- the date/time to converttargetZoneId- theZoneIdwhere the output string should say that the date residesformatter- the specific formatter to use- Returns:
- the formatted date string
-
formatWith
public static String formatWith(LocalDateTime localDateTime, ZoneId localZoneId, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenLocalDateTime(assuming it is in the givenlocalZoneIdtime zone) into a string using the givenDateTimeFormatter.- Parameters:
localDateTime- the date/time to convertlocalZoneId- theZoneIdto set theLocalDateTimein before converting to the target zonetargetZoneId- theZoneIdwhere the output string should say that the date residesformatter- the specific formatter to use- Returns:
- the formatted date string
-
formatWith
public static String formatWith(ZonedDateTime zonedDateTime, ZoneId targetZoneId, DateTimeFormatter formatter) Converts the givenZonedDateTimein the given time zone into a string using the givenDateTimeFormatter.- Parameters:
zonedDateTime- the date/time to converttargetZoneId- theZoneIdwhere the output string should say that the date residesformatter- the specific formatter to use- Returns:
- the formatted date string
-
formatWith
Converts the givenTemporalAccessorinto a string using the givenDateTimeFormatter. Assumes that the caller has already set the appropriate time zone on theTemporalAccessor.This is just a static wrapper around
DateTimeFormatter.format(TemporalAccessor).- Parameters:
temporalAccessor- the date/time to convertformatter- the specific formatter to use- Returns:
- the formatted date/time string
-