Class KiwiDateTimeFormatters


  • public class KiwiDateTimeFormatters
    extends Object
    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.

    • Constructor Detail

      • KiwiDateTimeFormatters

        public KiwiDateTimeFormatters()
    • Method Detail

      • formatAsIsoLocalDateUTC

        public static String formatAsIsoLocalDateUTC​(Date date)
        Converts the given Date in the UTC time zone into a string using the DateTimeFormatter.ISO_LOCAL_DATE formatter.
        Parameters:
        date - the date to convert
        Returns:
        the formatted date string
      • formatAsIsoLocalDate

        public static String formatAsIsoLocalDate​(Date date,
                                                  ZoneId targetZoneId)
        Converts the given Date in the given time zone into a string using the DateTimeFormatter.ISO_LOCAL_DATE formatter.
        Parameters:
        date - the date to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        Returns:
        the formatted date string
      • formatAsIsoLocalDateTimeUTC

        public static String formatAsIsoLocalDateTimeUTC​(Date date)
        Converts the given Date in the UTC time zone into a string using the DateTimeFormatter.ISO_LOCAL_DATE_TIME formatter.
        Parameters:
        date - the date to convert
        Returns:
        the formatted date/time string
      • formatAsIsoLocalDateTime

        public static String formatAsIsoLocalDateTime​(Date date,
                                                      ZoneId targetZoneId)
        Converts the given Date in the given time zone into a string using the DateTimeFormatter.ISO_LOCAL_DATE_TIME formatter.
        Parameters:
        date - the date to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        Returns:
        the formatted date/time string
      • formatAsIsoZonedDateTimeUTC

        public static String formatAsIsoZonedDateTimeUTC​(Date date)
        Converts the given Date in the UTC time zone into a string using the DateTimeFormatter.ISO_ZONED_DATE_TIME formatter.
        Parameters:
        date - the date to convert
        Returns:
        the formatted date/time string
      • formatAsIsoZonedDateTime

        public static String formatAsIsoZonedDateTime​(Date date,
                                                      ZoneId targetZoneId)
        Converts the given Date in the given time zone into a string using the DateTimeFormatter.ISO_ZONED_DATE_TIME formatter.
        Parameters:
        date - the date to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        Returns:
        the formatted date/time string
      • formatAsIsoZonedDateTimeUTC

        public static String formatAsIsoZonedDateTimeUTC​(LocalDate localDate)
        Converts the given LocalDate (at the start of the day) in the UTC time zone into a string using the DateTimeFormatter.ISO_ZONED_DATE_TIME formatter.
        Parameters:
        localDate - the date to convert
        Returns:
        the formatted date/time string
      • formatAsIsoZonedDateTime

        public static String formatAsIsoZonedDateTime​(LocalDateTime localDateTime,
                                                      ZoneId targetZoneId)
        Converts the given LocalDateTime in the given time zone into a string using the DateTimeFormatter.ISO_ZONED_DATE_TIME formatter.
        Parameters:
        localDateTime - the date/time to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        Returns:
        the formatted date/time string
      • formatAsIsoZonedDateTime

        public static String formatAsIsoZonedDateTime​(ZonedDateTime zonedDateTime,
                                                      ZoneId targetZoneId)
        Converts the given ZonedDateTime in the given time zone into a string using the DateTimeFormatter.ISO_ZONED_DATE_TIME formatter.
        Parameters:
        zonedDateTime - the date/time to convert
        targetZoneId - the ZoneId where 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 given Date in the UTC time zone into a string using the given DateTimeFormatter.
        Parameters:
        date - the date to convert
        formatter - the specific formatter to use
        Returns:
        the formatted date string
      • formatWith

        public static String formatWith​(Date date,
                                        ZoneId targetZoneId,
                                        DateTimeFormatter formatter)
        Converts the given Date in the given time zone into a string using the given DateTimeFormatter.
        Parameters:
        date - the date to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        formatter - the specific formatter to use
        Returns:
        the formatted date string
      • formatWith

        public static String formatWith​(LocalDateTime localDateTime,
                                        ZoneId targetZoneId,
                                        DateTimeFormatter formatter)
        Converts the given LocalDateTime (assuming it is in the UTC time zone) into a string using the given DateTimeFormatter.
        Parameters:
        localDateTime - the date/time to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        formatter - 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 given LocalDateTime (assuming it is in the given localZoneId time zone) into a string using the given DateTimeFormatter.
        Parameters:
        localDateTime - the date/time to convert
        localZoneId - the ZoneId to set the LocalDateTime in before converting to the target zone
        targetZoneId - the ZoneId where the output string should say that the date resides
        formatter - the specific formatter to use
        Returns:
        the formatted date string
      • formatWith

        public static String formatWith​(ZonedDateTime zonedDateTime,
                                        ZoneId targetZoneId,
                                        DateTimeFormatter formatter)
        Converts the given ZonedDateTime in the given time zone into a string using the given DateTimeFormatter.
        Parameters:
        zonedDateTime - the date/time to convert
        targetZoneId - the ZoneId where the output string should say that the date resides
        formatter - the specific formatter to use
        Returns:
        the formatted date string