Class DateTimeFormatterUtils
- java.lang.Object
-
- com.addicticks.texttime.formatters.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 Summary
Fields Modifier and Type Field Description protected static Map<Long,String>DATETIME_DELIMETER_LENIENT_MAPMap with the acceptable values for the delimiter between date and the time value.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringfixTooManySecondsDecs(String str, DateTimeParseException ex)Removes excessive seconds decimals from valuestr, given a previous parse error,ex.static OffsetDatefromTemporalAccessorToOffsetDate(TemporalAccessor temporal, Function<LocalDate,ZoneOffset> zoneOffsetProvider, int defaultMonth, int defaultDayOfMonth)Creates aOffsetDatefrom aTemporalAccessor.static OffsetDateTimefromTemporalAccessorToOffsetDateTime(TemporalAccessor temporal, Function<LocalDateTime,ZoneOffset> zoneOffsetProvider, int defaultMonth, int defaultDayOfMonth)Creates aOffsetDateTimefrom aTemporalAccessor.static OffsetDateTimefromTemporalAccessorToOffsetDateTimeNoTime(TemporalAccessor temporal, Function<LocalDate,ZoneOffset> zoneOffsetProvider, int defaultMonth, int defaultDayOfMonth)Creates aOffsetDateTimefrom aTemporalAccessor.static OffsetTimefromTemporalAccessorToOffsetTime(TemporalAccessor temporal, Function<LocalTime,ZoneOffset> zoneOffsetProvider)Creates aOffsetTimefrom aTemporalAccessor.static <T> TparseDualAttempt(DateTimeFormatter f, String str, TemporalQuery<T> q)Attempts to parse string value into a Java date/time value.
-
-
-
Method Detail
-
fromTemporalAccessorToOffsetDateTime
public static OffsetDateTime fromTemporalAccessorToOffsetDateTime(TemporalAccessor temporal, Function<LocalDateTime,ZoneOffset> zoneOffsetProvider, int defaultMonth, int defaultDayOfMonth)
Creates aOffsetDateTimefrom aTemporalAccessor. 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 inputzoneOffsetProvider- a function which will be called if no zone offset is present in thetemporal.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 aOffsetDatefrom aTemporalAccessor. 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 inputzoneOffsetProvider- a function which will be called if no zone offset is present in thetemporal.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 aOffsetDateTimefrom aTemporalAccessor. 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 proprietaryOffsetDateis unacceptable or not needed.- Parameters:
temporal- the inputzoneOffsetProvider- a function which will be called if no zone offset is present in thetemporal.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 aOffsetTimefrom aTemporalAccessor. If no timezone offset if found in the input then the supplied default timezone offset is used.- Parameters:
temporal- the inputzoneOffsetProvider- a function which will be called if no zone offset is present in thetemporal.- 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 befixedand 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 parsingstr- the string value to be parsedq-- Returns:
- parsed value, for example an OffsetDateTime.
-
fixTooManySecondsDecs
public static String fixTooManySecondsDecs(String str, DateTimeParseException ex)
Removes excessive seconds decimals from valuestr, given a previous parse error,ex. TheOffsetDateTimeandOffsetTimeclasses only allow a maximum of 9 decimals on the seconds value. In contrast, thexs:dateTimeandxs:timedata 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
nullmeans 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 ofstr, 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
-
-