Class DateTimeUtils


  • public final class DateTimeUtils
    extends Object
    A set of utilities to assist in bridging the gap to Java 8.

    This class is not found in Java SE 8 but provides methods that are.

    • Method Detail

      • toInstant

        public static Instant toInstant​(Date utilDate)
        Converts a java.util.Date to an Instant.
        Parameters:
        utilDate - the util date, not null
        Returns:
        the instant, not null
      • toDate

        public static Date toDate​(Instant instant)
        Converts an Instant to a java.util.Date.

        Fractions of the instant smaller than milliseconds will be dropped.

        Parameters:
        instant - the instant, not null
        Returns:
        the util date, not null
        Throws:
        IllegalArgumentException - if the conversion fails
      • toInstant

        public static Instant toInstant​(Calendar calendar)
        Converts a Calendar to an Instant.
        Parameters:
        calendar - the calendar, not null
        Returns:
        the instant, not null
      • toZonedDateTime

        public static ZonedDateTime toZonedDateTime​(Calendar calendar)
        Converts a Calendar to a ZonedDateTime.

        Note that GregorianCalendar supports a Julian-Gregorian cutover date and ZonedDateTime does not so some differences will occur.

        Parameters:
        calendar - the calendar, not null
        Returns:
        the instant, not null
      • toGregorianCalendar

        public static GregorianCalendar toGregorianCalendar​(ZonedDateTime zdt)
        Converts a ZonedDateTime to a Calendar.

        The resulting GregorianCalendar is pure Gregorian and uses ISO week definitions, starting on Monday and with 4 days in a minimal week.

        Fractions of the instant smaller than milliseconds will be dropped.

        Parameters:
        zdt - the zoned date-time, not null
        Returns:
        the calendar, not null
        Throws:
        IllegalArgumentException - if the conversion fails
      • toZoneId

        public static ZoneId toZoneId​(TimeZone timeZone)
        Converts a TimeZone to a ZoneId.
        Parameters:
        timeZone - the time-zone, not null
        Returns:
        the zone, not null
      • toTimeZone

        public static TimeZone toTimeZone​(ZoneId zoneId)
        Converts a ZoneId to a TimeZone.
        Parameters:
        zoneId - the zone, not null
        Returns:
        the time-zone, not null
      • toLocalDate

        public static LocalDate toLocalDate​(Date sqlDate)
        Converts a java.sql.Date to a LocalDate.
        Parameters:
        sqlDate - the SQL date, not null
        Returns:
        the local date, not null
      • toSqlDate

        public static Date toSqlDate​(LocalDate date)
        Converts a LocalDate to a java.sql.Date.
        Parameters:
        date - the local date, not null
        Returns:
        the SQL date, not null
      • toLocalTime

        public static LocalTime toLocalTime​(Time sqlTime)
        Converts a java.sql.Time to a LocalTime.
        Parameters:
        sqlTime - the SQL time, not null
        Returns:
        the local time, not null
      • toSqlTime

        public static Time toSqlTime​(LocalTime time)
        Converts a LocalTime to a java.sql.Time.
        Parameters:
        time - the local time, not null
        Returns:
        the SQL time, not null
      • toSqlTimestamp

        public static Timestamp toSqlTimestamp​(LocalDateTime dateTime)
        Converts a LocalDateTime to a java.sql.Timestamp.
        Parameters:
        dateTime - the local date-time, not null
        Returns:
        the SQL timestamp, not null
      • toLocalDateTime

        public static LocalDateTime toLocalDateTime​(Timestamp sqlTimestamp)
        Converts a java.sql.Timestamp to a LocalDateTime.
        Parameters:
        sqlTimestamp - the SQL timestamp, not null
        Returns:
        the local date-time, not null
      • toSqlTimestamp

        public static Timestamp toSqlTimestamp​(Instant instant)
        Converts an Instant to a java.sql.Timestamp.
        Parameters:
        instant - the instant, not null
        Returns:
        the SQL timestamp, not null
      • toInstant

        public static Instant toInstant​(Timestamp sqlTimestamp)
        Converts a java.sql.Timestamp to an Instant.
        Parameters:
        sqlTimestamp - the SQL timestamp, not null
        Returns:
        the instant, not null