Class DateTimeUtils


  • public class DateTimeUtils
    extends Object
    This class contains various utility methods related to date and time.
    • Constructor Detail

      • DateTimeUtils

        public DateTimeUtils()
    • Method Detail

      • parseDateTime

        public static <T> T parseDateTime​(String dateTimeString,
                                          Class<T> clazz)
                                   throws ParseException
        Parses a date/time string and returns a date/time object of the specified class. We distinguish three types of date/time classes: UTC time, time with time zone, local date/time. The supported date/time classes are:

        • long/Long (UNIX timestamp in milliseconds, UTC time)
        • Date (UTC time)
        • Instant (UTC time)
        • Calendar (with time zone)
        • DateTime (with time zone)
        • LocalDate
        • LocalTime
        • LocalDateTime

        It depends on the string format what class can be returned. This is detailed below. Supported string formats:

        • UNIX timestamp

          • long/Long, Date, Instant
          • Calendar, DateTime: the timestamp is translated to the default time zone.
          • LocalDate, LocalTime, LocalDateTime: the timestamp is translated to the default time zone before creating the local date/time.

        • SQL date: yyyy-MM-dd

          • LocalDate

        • SQL time: HH:mm:ss

          • LocalTime

        • SQL datetime: yyyy-MM-dd HH:mm:ss

          • LocalDateTime

        • any ISO date/time accepted by ISODateTimeFormat.dateTimeParser()

          • long/Long, Date, Instant. If no time zone is given in the string, it interprets the date/time with the default time zone. If the date/time does not exist in the time zone (because of a DST change), this method throws an exception. These classes store UTC times, so any specified time zone is eventually lost. Note that a string with only a date is a valid ISO date, but this method considers it an SQL date so the result must be a LocalDate.
          • Calendar, DateTime. The same as the UTC times except that any specified time zone is preserved in the result.
          • LocalDate, LocalTime, LocalDateTime. Any specified time zone is ignored.

        Type Parameters:
        T - the type of date/time to return
        Parameters:
        dateTimeString - the date/time string
        clazz - the result class
        Returns:
        the date/time with the specified class
        Throws:
        ParseException - if the date/time string is invalid, or a date/time without a time zone is parsed in a time zone where that date/time does not exist
      • dateTimeToType

        public static <T> T dateTimeToType​(org.joda.time.DateTime dateTime,
                                           Class<T> clazz)
                                    throws IllegalArgumentException
        Converts a DateTime object to an object of the specified class. It supports the following classes.

        • long/Long (UNIX timestamp in milliseconds): translated to UTC time, time zone is lost
        • Date: translated to UTC time, time zone is lost
        • Instant: translated to UTC time, time zone is lost
        • Calendar
        • DateTime
        • LocalDate: time and time zone is ignored
        • LocalTime: date and time zone is ignored
        • LocalDateTime: time zone is ignored

        Type Parameters:
        T - the type of date/time to return
        Parameters:
        dateTime - the date/time
        clazz - the result class
        Returns:
        the date/time with the specified class
        Throws:
        IllegalArgumentException - if the target class is not supported
      • localToUtcWithGapCorrection

        public static org.joda.time.DateTime localToUtcWithGapCorrection​(org.joda.time.LocalDateTime localDateTime,
                                                                         org.joda.time.DateTimeZone tz)
        Converts the specified local date/time to a date/time in the specified time zone. If the local time is in a DST gap, it will add one hour. It could therefore occur in the next day.
        Parameters:
        localDateTime - the local date/time
        tz - the time zone
        Returns:
        the date/time