Package org.kiwiproject.time
Class KiwiDateTimeFormatters
- java.lang.Object
-
- org.kiwiproject.time.KiwiDateTimeFormatters
-
public class KiwiDateTimeFormatters extends Object
A collection of small utilities to format various types of Java date/time classes, both the legacyDateand the Java 8 date/time API classes injava.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, seeKiwiDateTimeParsers.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
IllegalArgumentExceptionifnullarguments are passed to them.
-
-
Constructor Summary
Constructors Constructor Description KiwiDateTimeFormatters()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 StringformatAsIsoLocalDateTimeUTC(Date date)Converts 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 StringformatAsIsoZonedDateTimeUTC(Date date)Converts 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 Detail
-
formatAsIsoLocalDateUTC
public static String formatAsIsoLocalDateUTC(Date date)
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
public static String formatAsIsoLocalDate(Date date, ZoneId targetZoneId)
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
public static String formatAsIsoLocalDate(TemporalAccessor dateTime)
Converts the givenTemporalAccessorinto a string using theDateTimeFormatter.ISO_LOCAL_DATEformatter.- Parameters:
dateTime- the date/time to convert- Returns:
- the formatted date string
-
formatAsIsoLocalDateTimeUTC
public static String formatAsIsoLocalDateTimeUTC(Date date)
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
public static String formatAsIsoLocalDateTime(Date date, ZoneId targetZoneId)
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
public static String formatAsIsoLocalDateTime(LocalDate localDate)
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:
LocalDate.atStartOfDay()
-
formatAsIsoLocalDateTime
public static String formatAsIsoLocalDateTime(TemporalAccessor dateTime)
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
public static String formatAsIsoZonedDateTimeUTC(Date date)
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
public static String formatAsIsoZonedDateTime(Date date, ZoneId targetZoneId)
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
public static String formatAsIsoZonedDateTimeUTC(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.- Parameters:
localDate- the date to convert- Returns:
- the formatted date/time string
-
formatAsIsoZonedDateTime
public static String formatAsIsoZonedDateTime(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.- Parameters:
localDate- the date to converttargetZoneId- theZoneIdwhere the output string should say that the date resides- Returns:
- the formatted date/time string
- See Also:
LocalDate.atStartOfDay()
-
formatAsIsoZonedDateTimeUTC
public static String formatAsIsoZonedDateTimeUTC(LocalDateTime localDateTime)
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
public static String formatAsIsoZonedDateTime(LocalDateTime localDateTime, ZoneId targetZoneId)
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
public static String formatAsIsoZonedDateTimeUTC(ZonedDateTime zonedDateTime)
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
public static String formatAsIsoZonedDateTime(ZonedDateTime zonedDateTime, ZoneId targetZoneId)
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
public static String formatWithUTC(Date date, DateTimeFormatter formatter)
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
public static String formatWith(Date date, ZoneId targetZoneId, DateTimeFormatter formatter)
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
public static String formatWith(TemporalAccessor temporalAccessor, DateTimeFormatter formatter)
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
-
-