DateTimeFormatter

Undocumented in source.
final
class DateTimeFormatter {}

Constructors

this
this(CompositePrinterParser printerParser, Locale locale, DecimalStyle decimalStyle, ResolverStyle resolverStyle, Set!(TemporalField) resolverFields, Chronology chrono, ZoneId zone)

Constructor.

Members

Functions

format
string format(TemporalAccessor temporal)

Formats a date-time object using this formatter. !(p) This formats the date-time to a string using the rules of the formatter.

formatTo
void formatTo(TemporalAccessor temporal, Appendable appendable)

Formats a date-time object to an {@code Appendable} using this formatter. !(p) This outputs the formatted date-time to the specified destination. {@link Appendable} is a general purpose interface that is implemented by all key character output classes including {@code StringBuffer}, {@code StringBuilder}, {@code PrintStream} and {@code Writer}. !(p) Although {@code Appendable} methods throw an {@code IOException}, this method does not. Instead, any {@code IOException} is wrapped _in a runtime exception.

getChronology
Chronology getChronology()

Gets the overriding chronology to be used during formatting. !(p) This returns the override chronology, used to convert dates. By default, a formatter has no override chronology, returning null. See {@link #withChronology(Chronology)} for more details on overriding.

getDecimalStyle
DecimalStyle getDecimalStyle()

Gets the DecimalStyle to be used during formatting.

getLocale
Locale getLocale()

Gets the locale to be used during formatting. !(p) This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern.

getResolverFields
Set!(TemporalField) getResolverFields()

Gets the resolver fields to use during parsing. !(p) This returns the resolver fields, used during the second phase of parsing when fields are resolved into dates and times. By default, a formatter has no resolver fields, and thus returns null. See {@link #withResolverFields(Set)} for more details.

getResolverStyle
ResolverStyle getResolverStyle()

Gets the resolver style to use during parsing. !(p) This returns the resolver style, used during the second phase of parsing when fields are resolved into dates and times. By default, a formatter has the {@link ResolverStyle#SMART SMART} resolver style. See {@link #withResolverStyle(ResolverStyle)} for more details.

getZone
ZoneId getZone()

Gets the overriding zone to be used during formatting. !(p) This returns the override zone, used to convert instants. By default, a formatter has no override zone, returning null. See {@link #withZone(ZoneId)} for more details on overriding.

parse
TemporalAccessor parse(string text)

Fully parses the text producing a temporal object. !(p) This parses the entire text producing a temporal object. It is typically more useful to use {@link #parse(string, TemporalQuery)}. The result of this method is {@code TemporalAccessor} which has been resolved, applying basic validation checks to help ensure a valid date-time. !(p) If the parse completes without reading the entire length of the text, or a problem occurs during parsing or merging, then an exception is thrown.

parse
TemporalAccessor parse(string text, ParsePosition position)

Parses the text using this formatter, providing control over the text position. !(p) This parses the text without requiring the parse to start from the beginning of the string or finish at the end. The result of this method is {@code TemporalAccessor} which has been resolved, applying basic validation checks to help ensure a valid date-time. !(p) The text will be parsed from the specified start {@code ParsePosition}. The entire length of the text does not have to be parsed, the {@code ParsePosition} will be updated with the index at the end of parsing. !(p) The operation of this method is slightly different to similar methods using {@code ParsePosition} on {@code java.text.Format}. That class will return errors using the error index on the {@code ParsePosition}. By contrast, this method will throw a {@link DateTimeParseException} if an error occurs, with the exception containing the error index. This change _in behavior is necessary due to the increased complexity of parsing and resolving dates/times _in this API. !(p) If the formatter parses the same field more than once with different values, the result will be an error.

parse
T parse(string text, TemporalQuery!(T) query)

Fully parses the text producing an object of the specified type. !(p) Most applications should use this method for parsing. It parses the entire text to produce the required date-time. The query is typically a method reference to a {@code from(TemporalAccessor)} method. For example: !(pre) LocalDateTime dt = parser.parse(str, LocalDateTime.from); </pre> If the parse completes without reading the entire length of the text, or a problem occurs during parsing or merging, then an exception is thrown.

parseBest
TemporalAccessor parseBest(string text, TemporalQuery!(Object)[] queries)

Fully parses the text producing an object of one of the specified types. !(p) This parse method is convenient for use when the parser can handle optional elements. For example, a pattern of 'uuuu-MM-dd HH.mm[ VV]' can be fully parsed to a {@code ZonedDateTime}, or partially parsed to a {@code LocalDateTime}. The queries must be specified _in order, starting from the best matching full-parse option and ending with the worst matching minimal parse option. The query is typically a method reference to a {@code from(TemporalAccessor)} method. !(p) The result is associated with the first type that successfully parses. Normally, applications will use {@code instanceof} to check the result. For example: !(pre) TemporalAccessor dt = parser.parseBest(str, ZonedDateTime::from, LocalDateTime.from); if (cast(ZonedDateTime)(dt) !is null) { ... } else { ... } </pre> If the parse completes without reading the entire length of the text, or a problem occurs during parsing or merging, then an exception is thrown.

parseUnresolved
TemporalAccessor parseUnresolved(string text, ParsePosition position)

Parses the text using this formatter, without resolving the result, intended for advanced use cases. !(p) Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a {@code Map} of field to value, a {@code ZoneId} and a {@code Chronology}. Second, the parsed data is !(em)resolved</em>, by validating, combining and simplifying the various fields into more useful ones. This method performs the parsing stage but not the resolving stage. !(p) The result of this method is {@code TemporalAccessor} which represents the data as seen _in the input. Values are not validated, thus parsing a date string of '2012-00-65' would result _in a temporal with three fields - year of '2012', month of '0' and day-of-month of '65'. !(p) The text will be parsed from the specified start {@code ParsePosition}. The entire length of the text does not have to be parsed, the {@code ParsePosition} will be updated with the index at the end of parsing. !(p) Errors are returned using the error index field of the {@code ParsePosition} instead of {@code DateTimeParseException}. The returned error index will be set to an index indicative of the error. Callers must check for errors before using the result. !(p) If the formatter parses the same field more than once with different values, the result will be an error. !(p) This method is intended for advanced use cases that need access to the internal state during parsing. Typical application code should use {@link #parse(string, TemporalQuery)} or the parse method on the target type.

toPrinterParser
CompositePrinterParser toPrinterParser(bool optional)

Returns the formatter as a composite printer parser.

toString
string toString()

Returns a description of the underlying formatters.

withChronology
DateTimeFormatter withChronology(Chronology chrono)

Returns a copy of this formatter with a new override chronology. !(p) This returns a formatter with similar state to this formatter but with the override chronology set. By default, a formatter has no override chronology, returning null. !(p) If an override is added, then any date that is formatted or parsed will be affected. !(p) When formatting, if the temporal object contains a date, then it will be converted to a date _in the override chronology. Whether the temporal contains a date is determined by querying the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. Any time or zone will be retained unaltered unless overridden. !(p) If the temporal object does not contain a date, but does contain one or more {@code ChronoField} date fields, then a {@code DateTimeException} is thrown. In all other cases, the override chronology is added to the temporal, replacing any previous chronology, but without changing the date/time. !(p) When parsing, there are two distinct cases to consider. If a chronology has been parsed directly from the text, perhaps because {@link DateTimeFormatterBuilder#appendChronologyId()} was used, then this override chronology has no effect. If no zone has been parsed, then this override chronology will be used to interpret the {@code ChronoField} values into a date according to the date resolving rules of the chronology. !(p) This instance is immutable and unaffected by this method call.

withDecimalStyle
DateTimeFormatter withDecimalStyle(DecimalStyle decimalStyle)

Returns a copy of this formatter with a new DecimalStyle. !(p) This instance is immutable and unaffected by this method call.

withLocale
DateTimeFormatter withLocale(Locale locale)

Returns a copy of this formatter with a new locale. !(p) This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern. !(p) The locale is stored as passed _in, without further processing. If the locale has <a href="../../util/Locale.html#def_locale_extension"> Unicode extensions</a>, they may be used later _in text processing. To set the chronology, time-zone and decimal style from unicode extensions, see {@link #localizedBy localizedBy()}. !(p) This instance is immutable and unaffected by this method call.

withResolverStyle
DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle)

Returns a copy of this formatter with a new resolver style. !(p) This returns a formatter with similar state to this formatter but with the resolver style set. By default, a formatter has the {@link ResolverStyle#SMART SMART} resolver style. !(p) Changing the resolver style only has an effect during parsing. Parsing a text string occurs _in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects. The resolver style is used to control how phase 2, resolving, happens. See {@code ResolverStyle} for more information on the options available. !(p) This instance is immutable and unaffected by this method call.

withZone
DateTimeFormatter withZone(ZoneId zone)

Returns a copy of this formatter with a new override zone. !(p) This returns a formatter with similar state to this formatter but with the override zone set. By default, a formatter has no override zone, returning null. !(p) If an override is added, then any instant that is formatted or parsed will be affected. !(p) When formatting, if the temporal object contains an instant, then it will be converted to a zoned date-time using the override zone. Whether the temporal is an instant is determined by querying the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS} field. If the input has a chronology then it will be retained unless overridden. If the input does not have a chronology, such as {@code Instant}, then the ISO chronology will be used. !(p) If the temporal object does not contain an instant, but does contain an offset then an additional check is made. If the normalized override zone is an offset that differs from the offset of the temporal, then a {@code DateTimeException} is thrown. In all other cases, the override zone is added to the temporal, replacing any previous zone, but without changing the date/time. !(p) When parsing, there are two distinct cases to consider. If a zone has been parsed directly from the text, perhaps because {@link DateTimeFormatterBuilder#appendZoneId()} was used, then this override zone has no effect. If no zone has been parsed, then this override zone will be included _in the result of the parse where it can be used to build instants and date-times. !(p) This instance is immutable and unaffected by this method call.

Static functions

ofLocalizedDate
deprecated DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle)
Undocumented in source. Be warned that the author may not have intended to support it.
ofLocalizedDateTime
deprecated DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle)
Undocumented in source. Be warned that the author may not have intended to support it.
ofLocalizedDateTime
deprecated DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle)
Undocumented in source. Be warned that the author may not have intended to support it.
ofLocalizedTime
deprecated DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle)
Undocumented in source. Be warned that the author may not have intended to support it.
ofPattern
deprecated DateTimeFormatter ofPattern(string pattern)
Undocumented in source. Be warned that the author may not have intended to support it.
ofPattern
deprecated DateTimeFormatter ofPattern(string pattern, Locale locale)
Undocumented in source. Be warned that the author may not have intended to support it.
parsedExcessDays
TemporalQuery!(Period) parsedExcessDays()

A query that provides access to the excess days that were parsed. !(p) This returns a singleton {@linkplain TemporalQuery query} that provides access to additional information from the parse. The query always returns a non-null period, with a zero period returned instead of null. !(p) There are two situations where this query may return a non-zero period. !(ul) !(li)If the {@code ResolverStyle} is {@code LENIENT} and a time is parsed without a date, then the complete result of the parse consists of a {@code LocalTime} and an excess {@code Period} _in days.

parsedLeapSecond
TemporalQuery!(Boolean) parsedLeapSecond()

A query that provides access to whether a leap-second was parsed. !(p) This returns a singleton {@linkplain TemporalQuery query} that provides access to additional information from the parse. The query always returns a non-null bool, true if parsing saw a leap-second, false if not. !(p) Instant parsing handles the special "leap second" time of '23:59:60'. Leap seconds occur at '23:59:60' _in the UTC time-zone, but at other local times _in different time-zones. To avoid this potential ambiguity, the handling of leap-seconds is limited to {@link DateTimeFormatterBuilder#appendInstant()}, as that method always parses the instant with the UTC zone offset. !(p) If the time '23:59:60' is received, then a simple conversion is applied, replacing the second-of-minute of 60 with 59. This query can be used on the parse result to determine if the leap-second adjustment was made. The query will return {@code true} if it did adjust to remove the leap-second, and {@code false} if not. Note that applying a leap-second smoothing mechanism, such as UTC-SLS, is the responsibility of the application, as follows: !(pre) TemporalAccessor parsed = formatter.parse(str); Instant instant = parsed.query(Instant::from); if (parsed.query(DateTimeFormatter.parsedLeapSecond())) { // validate leap-second is correct and apply correct smoothing } </pre> @return a query that provides access to whether a leap-second was parsed

Meta