Class DateTimeFormatterISO
- java.lang.Object
-
- com.addicticks.texttime.formatters.DateTimeFormatterISO
-
public class DateTimeFormatterISO extends Object
Formatter-Parsers for ISO 8601 date and time values.These classes complement those in the JDK and provide additional features such as accepting a space as a the separator between the date and the time value and accepting the absence of the timezone offset.
Predefined Formatters-Parsers overview
Formatter Usage Description Examples ISO_OFFSET_DATE_TIME_PARSERParsing For ISO-8601 date/time value parsing with mandatory offset '2018-12-03T10:15:30+01:00''2018-12-03 10:15:30+01:00''2018-12-03T10:15:30+01''2018-12-03t10:15:30+01''2018-12-03T10:15:30Z''2018-12-03T10:15:30z'ISO_OPT_OFFSET_DATE_TIME_PARSERParsing For ISO-8601 date/time value parsing with optional offset '2018-12-03T10:15:30+01:00''2018-12-03 10:15:30+01:00''2018-12-03T10:15:30+01''2018-12-03t10:15:30+01''2018-12-03T10:15:30Z''2018-12-03T10:15:30z''2018-12-03T10:15:30'ISO_OPT_OFFSET_DATE_PARSERParsing For ISO-8601 date value parsing with optional offset '2018-12-03+01:00''2018-12-03+01''2018-12-03Z''2018-12-03z''2018-12-03'ISO_OPT_OFFSET_TIME_PARSERParsing For ISO-8601 time value parsing with optional offset '10:15:30+01:00''10:15:30+01''10:15:30Z''10:15:30z''10:15:30'Parsing
This class also provides convenience methods for parsing ISO-8601 date/time string values with optional offset.Formatting
For producing ISO-8601 compliant string values you can simply use the classes in the JDK.- See Also:
- Wikipedia ISO 8601
-
-
Field Summary
Fields Modifier and Type Field Description static DateTimeFormatterISO_OFFSET_DATE_TIME_PARSERFor parsing of ISO 8601 date-time values with mandatory offset.static DateTimeFormatterISO_OPT_OFFSET_DATE_PARSERFor parsing of ISO 8601 date values with optional offset.static DateTimeFormatterISO_OPT_OFFSET_DATE_TIME_PARSERFor parsing of ISO 8601 date-time values with optional offset.static DateTimeFormatterISO_OPT_OFFSET_TIME_PARSERFor parsing of ISO 8601 time values with optional offset.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static OffsetDateparseIntoOffsetDate(String str, ZoneOffset defaultZoneOffset)Parses a ISO-8601 date string into anOffsetDate.static OffsetDateparseIntoOffsetDate(String str, Function<LocalDate,ZoneOffset> zoneOffsetProvider)Parses a ISO-8601 date string into anOffsetDate.static OffsetDateTimeparseIntoOffsetDateTime(String str)Parses an ISO-8601 date-time string with offset into anOffsetDateTime.static OffsetDateTimeparseIntoOffsetDateTime(String str, ZoneOffset defaultZoneOffset)Parses an ISO-8601 date-time string into anOffsetDateTime.static OffsetDateTimeparseIntoOffsetDateTime(String str, Function<LocalDateTime,ZoneOffset> zoneOffsetProvider)Parses an ISO-8601 date-time string into anOffsetDateTime.static OffsetDateTimeparseIntoOffsetDateTimeNoTime(String str, ZoneOffset defaultZoneOffset)Parses a ISO-8601 date string into anOffsetDateTime.static OffsetDateTimeparseIntoOffsetDateTimeNoTime(String str, Function<LocalDate,ZoneOffset> zoneOffsetProvider)Parses a ISO-8601 date string into anOffsetDateTime.static OffsetTimeparseIntoOffsetTime(String str, ZoneOffset defaultZoneOffset)Parses a ISO-8601 time string into anOffsetTime.static OffsetTimeparseIntoOffsetTime(String str, Function<LocalTime,ZoneOffset> zoneOffsetProvider)Parses a ISO-8601 time string into anOffsetTime.
-
-
-
Field Detail
-
ISO_OFFSET_DATE_TIME_PARSER
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME_PARSER
For parsing of ISO 8601 date-time values with mandatory offset.This Formatter-Parser is similar to the JDK's
ISO_OFFSET_DATE_TIMEexcept that while parsing it also accepts a space as the separator between the date and the time value (see note below).This Formatter-Parser is primarily intended for parsing. For formatting the JDK's
ISO_OFFSET_DATE_TIMEis a better option.Parsing
This class is only intended for the use-case where the timezone offset is mandatory in the input and the absence of same will generate an exception. Very often the presence of the timezone offset cannot be guaranteed and in this case it is much better to useISO_OPT_OFFSET_DATE_TIME_PARSERinstead.There are two methods for parsing a date-time string with mandatory offset into an
OffsetDateTimedepending on your scenario:- Use the static
parse()method on this class. This method gracefully handles the situation where there are more then 9 digits on the seconds fractional value. - Use this DateTimeFormatter directly:
OffsetDateTime value = DateTimeFormatterISO.ISO_OPT_OFFSET_DATE_TIME_PARSER.parse("2018-03-19:22:35:58", OffsetDateTime::from);
Note: The date-time pattern where a space is used to separate the date and the time value is not a pattern which should be encouraged except perhaps for display purpose. The pattern is allowed in the ISO 8601 specification (allowed as an exception: "by mutual agreement of the partners in information interchange"), but it is strictly forbidden in the definition of
xs:dateTimein the XML Schema Specification and also strictly forbidden in the W3C Date and Time Format. In conclusion: The pattern may be justifiable for display purpose, but for data exchange purpose the separator should always be an upper-case'T'.- See Also:
DateTimeFormatter.ISO_OFFSET_DATE_TIME
- Use the static
-
ISO_OPT_OFFSET_DATE_TIME_PARSER
public static final DateTimeFormatter ISO_OPT_OFFSET_DATE_TIME_PARSER
For parsing of ISO 8601 date-time values with optional offset.This Formatter-Parser is similar to
ISO_OFFSET_DATE_TIME_PARSER, however with the following extra features:- The timezone offset is optional while parsing.
- The delimiter between the date and time value can optionally be a space character (see note below).
This Formatter-Parser is primarily intended for parsing. For formatting the JDK's
ISO_OFFSET_DATE_TIMEis a better option.Parsing
There are two methods for parsing a string into anOffsetDateTimedepending on your scenario:- Use the static
parse()method on this class. This method gracefully handles the situation where there are more then 9 digits on the seconds fractional value. - Creating a new
DateTimeFormatterbased on this one, but with a default ZoneId which is used during parsing if none is provided in the input.// This only need to be created once and can be re-used // (DateTimeFormatters are immutable and thread-safe) DateTimeFormatter dtf = DateTimeFormatterISO.ISO_OPT_OFFSET_DATE_TIME_PARSER.withZone(ZoneId.of("America/New_York")); // Parse the string value. Since no offset is given in the input, the New York TZ is assumed. OffsetDateTime value = dtf.parse("2018-03-19:22:35:58", OffsetDateTime::from);
Note: The date-time pattern where a space is used to separate the date and the time value is not a pattern which should be encouraged except perhaps for display purpose. The pattern is allowed in the ISO 8601 specification (allowed as an exception: "by mutual agreement of the partners in information interchange"), but it is strictly forbidden in the definition of
xs:dateTimein the XML Schema Specification and also strictly forbidden in the W3C Date and Time Format. In conclusion: The pattern may be justifiable for display purpose, but for data exchange purpose the separator should always be an upper-case'T'.
-
ISO_OPT_OFFSET_DATE_PARSER
public static final DateTimeFormatter ISO_OPT_OFFSET_DATE_PARSER
For parsing of ISO 8601 date values with optional offset.This Formatter-Parser is similar to the JDK's
DateTimeFormatter.ISO_OFFSET_DATEexcept that the timezone offset is optional while parsing.This Formatter-Parser is primarily intended for parsing. For formatting the JDK's
DateTimeFormatter.ISO_OFFSET_DATEis a better option.Parsing
There are two methods for parsing a date string into anOffsetDate/OffsetDateTimedepending on your scenario:- Use either of the static methods: on this class.
- Creating a new
DateTimeFormatterbased on this one, but with a default ZoneId which is used during parsing if none is provided in the input.// This only need to be created once and can be re-used // (DateTimeFormatters are immutable and thread-safe) DateTimeFormatter dtf = DateTimeFormatterISO.ISO_OPT_OFFSET_DATE_PARSER.withZone(ZoneId.of("America/New_York")); // Parse the string value. Since no offset is given in the input, the New York TZ is assumed. OffsetDate value = dtf.parse("2018-03-19", OffsetDate::from);
-
ISO_OPT_OFFSET_TIME_PARSER
public static final DateTimeFormatter ISO_OPT_OFFSET_TIME_PARSER
For parsing of ISO 8601 time values with optional offset.This Formatter-Parser is similar to the JDK's
DateTimeFormatter.ISO_OFFSET_TIMEexcept that the timezone offset is optional while parsing.This Formatter-Parser is primarily intended for parsing. For formatting the JDK's
DateTimeFormatter.ISO_OFFSET_TIMEis a better option.Parsing
There are two methods for parsing a string into anOffsetTimedepending on your scenario:- Use the static
parse()on this class. This method gracefully handles the situation where there are more then 9 digits on the seconds fractional value. - Creating a new
DateTimeFormatterbased on this one, but with a default ZoneId which is used during parsing if none is provided in the input.// This only need to be created once and can be re-used // (DateTimeFormatters are immutable and thread-safe) DateTimeFormatter dtf = DateTimeFormatterISO.ISO_OPT_OFFSET_TIME_PARSER.withZone(ZoneId.of("America/New_York")); // Parse the string value. Since no offset is given in the input, the New York TZ is assumed. OffsetTime value = dtf.parse("22:33:58", OffsetTime::from);
- Use the static
-
-
Method Detail
-
parseIntoOffsetDateTime
public static OffsetDateTime parseIntoOffsetDateTime(String str)
Parses an ISO-8601 date-time string with offset into anOffsetDateTime. For example a string on the form"2018-12-03T10:15:30+01:00". The timezone offset part is mandatory in the input.The string is parsed using
ISO_OFFSET_DATE_TIME_PARSER.Furthermore, parsing is attempted in a
two-step procedure: If first parsing attempt fails the method will evaluate if the problem is something which can likely befixedand if so it will "fix" the input value,str, and perform a new parsing attempt.- Parameters:
str- the string to parse- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string, for example if the string has no timezone offset.
-
parseIntoOffsetDateTime
public static OffsetDateTime parseIntoOffsetDateTime(String str, Function<LocalDateTime,ZoneOffset> zoneOffsetProvider)
Parses an ISO-8601 date-time string into anOffsetDateTime. For example a string on the form"2018-12-03T10:15:30+01:00"(value with offset) or"2018-12-03T10:15:30"(value without offset).The string is parsed using
ISO_OPT_OFFSET_DATE_TIME_PARSER.Furthermore, parsing is attempted in a
two-step procedure: If first parsing attempt fails the method will evaluate if the problem is something which can likely befixedand if so it will "fix" the input value,str, and perform a new parsing attempt.- Parameters:
str- the string to parsezoneOffsetProvider- a function which will be called if no zone offset is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetDateTime
public static OffsetDateTime parseIntoOffsetDateTime(String str, ZoneOffset defaultZoneOffset)
Parses an ISO-8601 date-time string into anOffsetDateTime.This is a convenience method for the more generalized
parseIntoOffsetDateTime(java.lang.String, java.util.function.Function)method.- Parameters:
str- the string to parsedefaultZoneOffset- the zone offset to use if none is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetDate
public static OffsetDate parseIntoOffsetDate(String str, Function<LocalDate,ZoneOffset> zoneOffsetProvider)
Parses a ISO-8601 date string into anOffsetDate. For example a string on the form"2018-12-03+01:00"(value with offset) or"2018-12-03"(value without offset).The string is parsed using
ISO_OPT_OFFSET_DATE_PARSER.- Parameters:
str- the string to parsezoneOffsetProvider- a function which will be called if no zone offset is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetDate
public static OffsetDate parseIntoOffsetDate(String str, ZoneOffset defaultZoneOffset)
Parses a ISO-8601 date string into anOffsetDate.This is a convenience method for the more generalized
parseIntoOffsetDate(java.lang.String, java.util.function.Function)method.- Parameters:
str- the string to parsedefaultZoneOffset- the zone offset to use if none is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetDateTimeNoTime
public static OffsetDateTime parseIntoOffsetDateTimeNoTime(String str, Function<LocalDate,ZoneOffset> zoneOffsetProvider)
Parses a ISO-8601 date string into anOffsetDateTime. For example a string on the form"2018-12-03+01:00"(value with offset) or"2018-12-03"(value without offset).The string is parsed using
ISO_OPT_OFFSET_DATE_PARSER.- Parameters:
str- the string to parsezoneOffsetProvider- a function which will be called if no zone offset is present in the input string.- Returns:
- parsed value (with no time value, meaning time value is set to midnight)
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetDateTimeNoTime
public static OffsetDateTime parseIntoOffsetDateTimeNoTime(String str, ZoneOffset defaultZoneOffset)
Parses a ISO-8601 date string into anOffsetDateTime.This is a convenience method for the more generalized
parseIntoOffsetDateTimeNoTime(java.lang.String, java.util.function.Function)method.- Parameters:
str- the string to parsedefaultZoneOffset- the zone offset to use if none is present in the input string.- Returns:
- parsed value (with no time value, meaning time value is set to midnight)
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetTime
public static OffsetTime parseIntoOffsetTime(String str, Function<LocalTime,ZoneOffset> zoneOffsetProvider)
Parses a ISO-8601 time string into anOffsetTime. For example a string on the form"10:15:30+01:00"(value with offset) or"10:15:30"(value without offset).The string is parsed using
ISO_OPT_OFFSET_TIME_PARSER.Furthermore, parsing is attempted in a
two-step procedure: If first parsing attempt fails the method will evaluate if the problem is something which can likely befixedand if so it will "fix" the input value,str, and perform a new parsing attempt.- Parameters:
str- the string to parsezoneOffsetProvider- a function which will be called if no zone offset is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
parseIntoOffsetTime
public static OffsetTime parseIntoOffsetTime(String str, ZoneOffset defaultZoneOffset)
Parses a ISO-8601 time string into anOffsetTime.This is convenience method for the more generalized
parseIntoOffsetTime(java.lang.String, java.util.function.Function)method.- Parameters:
str- the string to parsedefaultZoneOffset- the zone offset to use if none is present in the input string.- Returns:
- parsed value
- Throws:
DateTimeParseException- if unable to parse the string
-
-