Class DateTimeFormatterUtils


  • public class DateTimeFormatterUtils
    extends Object
    Utility methods for parsing Date/Time string values.

    There should rarely be a need to use these methods directly.

    • Field Detail

      • DATETIME_DELIMETER_LENIENT_MAP

        protected static final Map<Long,​String> DATETIME_DELIMETER_LENIENT_MAP
        Map with the acceptable values for the delimiter between date and the time value.
    • Method Detail

      • fromTemporalAccessorToOffsetDateTime

        public static OffsetDateTime fromTemporalAccessorToOffsetDateTime​(TemporalAccessor temporal,
                                                                          Function<LocalDateTime,​ZoneOffset> zoneOffsetProvider,
                                                                          int defaultMonth,
                                                                          int defaultDayOfMonth)
        Creates a OffsetDateTime from a TemporalAccessor. If no timezone offset if found in the temporal then the supplied default timezone offset is used.

        If no time-part is found in the temporal then time is set to MIDNIGHT (start of day).

        Parameters:
        temporal - the input
        zoneOffsetProvider - a function which will be called if no zone offset is present in the temporal.
        defaultMonth - the month value to use (1 - 12) if no month can be obtained from the temporal, or -1 if the month value is considered mandatory in the temporal.
        defaultDayOfMonth - the day-of-month value to use (1 - 31) if no day-of-month can be obtained from the temporal, or -1 if the day-of-month value is considered mandatory in the temporal.
        Returns:
        Throws:
        DateTimeTemporalQueryException - if the temporal does not contain the elements needed to construct an OffsetDateTime
      • fromTemporalAccessorToOffsetDate

        public static OffsetDate fromTemporalAccessorToOffsetDate​(TemporalAccessor temporal,
                                                                  Function<LocalDate,​ZoneOffset> zoneOffsetProvider,
                                                                  int defaultMonth,
                                                                  int defaultDayOfMonth)
        Creates a OffsetDate from a TemporalAccessor. Certain elements may be absent from the temporal in which case the supplied defaults are used.

        fromTemporalAccessorToOffsetDateTimeNoTime() is an alternative to this method.

        Parameters:
        temporal - the input
        zoneOffsetProvider - a function which will be called if no zone offset is present in the temporal.
        defaultMonth - the month value to use (1 - 12) if no month can be obtained from the temporal, or -1 if the month value is considered mandatory in the temporal.
        defaultDayOfMonth - the day-of-month value to use (1 - 31) if no day-of-month can be obtained from the temporal, or -1 if the day-of-month value is considered mandatory in the temporal.
        Returns:
        Throws:
        DateTimeTemporalQueryException - if the temporal does not contain the elements needed to construct an OffsetDate
      • fromTemporalAccessorToOffsetDateTimeNoTime

        public static OffsetDateTime fromTemporalAccessorToOffsetDateTimeNoTime​(TemporalAccessor temporal,
                                                                                Function<LocalDate,​ZoneOffset> zoneOffsetProvider,
                                                                                int defaultMonth,
                                                                                int defaultDayOfMonth)
        Creates a OffsetDateTime from a TemporalAccessor. If no timezone offset if found in the input then the supplied default timezone offset is used.

        Any time information on the input will be ignored. The returned value will always have its time information set to midnight.

        This method is an alternative to fromTemporalAccessorToOffsetDate() method when use of the proprietary OffsetDate is unacceptable or not needed.

        Parameters:
        temporal - the input
        zoneOffsetProvider - a function which will be called if no zone offset is present in the temporal.
        defaultMonth - the month value to use (1 - 12) if no month can be obtained from the temporal, or -1 if the month value is considered mandatory in the temporal.
        defaultDayOfMonth - the day-of-month value to use (1 - 31) if no day-of-month can be obtained from the temporal, or -1 if the day-of-month value is considered mandatory in the temporal.
        Returns:
        Throws:
        DateTimeTemporalQueryException - if the temporal does not contain the elements needed to construct an OffsetDateTime
      • fromTemporalAccessorToOffsetTime

        public static OffsetTime fromTemporalAccessorToOffsetTime​(TemporalAccessor temporal,
                                                                  Function<LocalTime,​ZoneOffset> zoneOffsetProvider)
        Creates a OffsetTime from a TemporalAccessor. If no timezone offset if found in the input then the supplied default timezone offset is used.
        Parameters:
        temporal - the input
        zoneOffsetProvider - a function which will be called if no zone offset is present in the temporal.
        Returns:
        Throws:
        DateTimeTemporalQueryException - if the temporal does not contain the elements needed to construct an OffsetTime
      • parseDualAttempt

        public static <T> T parseDualAttempt​(DateTimeFormatter f,
                                             String str,
                                             TemporalQuery<T> q)
        Attempts to parse string value into a Java date/time value. If first attempt fails the method will evaluate if it is something which can be fixed and if so it will "fix" the input value, str, and perform a new parsing attempt.
        Type Parameters:
        T - type is inferred from q, typically it will be an OffsetTime or OffsetDateTime type.
        Parameters:
        f - the formatter to be used for parsing
        str - the string value to be parsed
        q -
        Returns:
        parsed value, for example an OffsetDateTime.
      • fixTooManySecondsDecs

        public static String fixTooManySecondsDecs​(String str,
                                                   DateTimeParseException ex)
        Removes excessive seconds decimals from value str, given a previous parse error, ex. The OffsetDateTime and OffsetTime classes only allow a maximum of 9 decimals on the seconds value. In contrast, the xs:dateTime and xs:time data types allow an unlimited number of decimals on the seconds value. This method aims to fix this discrepancy by simply removing decimal digits after the 9th digit.

        The method is optimized for speed, meaning that it uses very few resources to check if it is likely that the parse error is caused by too many decimals on the seconds value. Only then will the method attempt to create a new (fixed) string value.

        A return value of null means that it is unlikely that the parsing error was due to too many seconds decimals. In contrast, a non-null return value will be a modified version of str, which is likely to be able to pass a new parsing operation. For example for input value "23:30:28.123456789012345678901234567890Z" (parse error at position 18) the method will return "23:30:28.123456789Z".

        The method errs on the side of caution, which means that it doesn't attempt to fix other semantical problems in the value and it does a number of checks before it concludes that the error is caused by too many decimals on the seconds value.

        Parameters:
        str - string value, either a date-time value or a time-only value.
        ex - exception from prior attempt to parse
        Returns:
        fixed string or null