public final class DateTimeFormatter extends Object
This class provides the main application entry point for printing and parsing.
Common instances of DateTimeFormatter are provided by DateTimeFormatters.
For more complex formatters, a builder is provided.
In most cases, it is not necessary to use this class directly when formatting.
The main date-time classes provide two methods - one for printing,
toString(DateTimeFormatter formatter), and one for parsing,
parse(CharSequence text, DateTimeFormatter formatter).
For example:
String text = date.toString(formatter); LocalDate date = LocalDate.parse(text, formatter);Some aspects of printing and parsing are dependent on the locale. The locale can be changed using the
withLocale(Locale) method
which returns a new formatter in the requested locale.
Some applications may need to use the older Format class for formatting.
The toFormat() method returns an implementation of the old API.
| Modifier and Type | Method and Description |
|---|---|
Chrono<?> |
getChrono()
Gets the overriding chronology to be used during formatting.
|
Locale |
getLocale()
Gets the locale to be used during formatting.
|
DateTimeFormatSymbols |
getSymbols()
Gets the set of symbols to be used during formatting.
|
ZoneId |
getZone()
Gets the overriding zone to be used during formatting.
|
<T> T |
parse(CharSequence text,
Class<T> type)
Fully parses the text producing an object of the specified type.
|
TemporalAccessor |
parseBest(CharSequence text,
Class<?>... types)
Fully parses the text producing an object of one of the specified types.
|
DateTimeBuilder |
parseToBuilder(CharSequence text)
Parses the text to a builder.
|
DateTimeBuilder |
parseToBuilder(CharSequence text,
ParsePosition position)
Parses the text to a builder.
|
String |
print(TemporalAccessor temporal)
Prints a date-time object using this formatter.
|
void |
printTo(TemporalAccessor temporal,
Appendable appendable)
Prints a date-time object to an
Appendable using this formatter. |
Format |
toFormat()
Returns this formatter as a
java.text.Format instance. |
Format |
toFormat(Class<?> parseType)
Returns this formatter as a
java.text.Format instance that will
parse to the specified type. |
String |
toString()
Returns a description of the underlying formatters.
|
DateTimeFormatter |
withChrono(Chrono<?> chrono)
Returns a copy of this formatter with a new override chronology.
|
DateTimeFormatter |
withLocale(Locale locale)
Returns a copy of this formatter with a new locale.
|
DateTimeFormatter |
withSymbols(DateTimeFormatSymbols symbols)
Returns a copy of this formatter with a new set of symbols.
|
DateTimeFormatter |
withZone(ZoneId zone)
Returns a copy of this formatter with a new override zone.
|
public Locale getLocale()
This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern.
public DateTimeFormatter withLocale(Locale locale)
This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern.
This instance is immutable and unaffected by this method call.
locale - the new locale, not nullpublic DateTimeFormatSymbols getSymbols()
public DateTimeFormatter withSymbols(DateTimeFormatSymbols symbols)
This instance is immutable and unaffected by this method call.
symbols - the new symbols, not nullpublic Chrono<?> getChrono()
This returns the override chronology, used to convert dates.
By default, a formatter has no override chronology, returning null.
See withChrono(Chrono) for more details on overriding.
public DateTimeFormatter withChrono(Chrono<?> chrono)
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.
If an override is added, then any date that is printed or parsed will be affected.
When printing, if the Temporal object contains a date then it will
be converted to a date in the override chronology.
Any time or zone will be retained unless overridden.
The converted result will behave in a manner equivalent to an implementation
of ChronoLocalDate,ChronoLocalDateTime or ChronoZonedDateTime.
When parsing, the override chronology will be used to interpret the fields into a date unless the formatter directly parses a valid chronology.
This instance is immutable and unaffected by this method call.
chrono - the new chronology, not nullpublic ZoneId getZone()
This returns the override zone, used to convert instants.
By default, a formatter has no override zone, returning null.
See withZone(ZoneId) for more details on overriding.
public DateTimeFormatter withZone(ZoneId zone)
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.
If an override is added, then any instant that is printed or parsed will be affected.
When printing, if the Temporal object contains an instant then it will
be converted to a zoned date-time using the override zone.
If the input has a chronology then it will be retained unless overridden.
If the input does not have a chronology, such as Instant, then
the ISO chronology will be used.
The converted result will behave in a manner equivalent to an implementation
of ChronoZonedDateTime.
When parsing, the override zone will be used to interpret the fields into an instant unless the formatter directly parses a valid zone.
This instance is immutable and unaffected by this method call.
zone - the new override zone, not nullpublic String print(TemporalAccessor temporal)
This prints the date-time to a String using the rules of the formatter.
temporal - the temporal object to print, not nullDateTimeException - if an error occurs during printingpublic void printTo(TemporalAccessor temporal, Appendable appendable)
Appendable using this formatter.
This prints the date-time to the specified destination.
Appendable is a general purpose interface that is implemented by all
key character output classes including StringBuffer, StringBuilder,
PrintStream and Writer.
Although Appendable methods throw an IOException, this method does not.
Instead, any IOException is wrapped in a runtime exception.
See DateTimePrintException.rethrowIOException() for a means
to extract the IOException.
temporal - the temporal object to print, not nullappendable - the appendable to print to, not nullDateTimeException - if an error occurs during printingpublic <T> T parse(CharSequence text, Class<T> type)
Most applications should use this method for parsing. It parses the entire text to produce the required date-time. For example:
LocalDateTime dt = parser.parse(str, LocalDateTime.class);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.
T - the type to extracttext - the text to parse, not nulltype - the type to extract, not nullDateTimeParseException - if the parse failspublic TemporalAccessor parseBest(CharSequence text, Class<?>... types)
This parse method is convenient for use when the parser can handle optional elements.
For example, a pattern of 'yyyy-MM[-dd[Z]]' can be fully parsed to an OffsetDate,
or partially parsed to a LocalDate or a YearMonth.
The types must be specified in order, starting from the best matching full-parse option
and ending with the worst matching minimal parse option.
The result is associated with the first type that successfully parses.
Normally, applications will use instanceof to check the result.
For example:
TemporalAccessor dt = parser.parseBest(str, OffsetDate.class, LocalDate.class);
if (dt instanceof OffsetDate) {
...
} else {
...
}
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.text - the text to parse, not nulltypes - the types to attempt to parse to, which must implement TemporalAccessor, not nullIllegalArgumentException - if less than 2 types are specifiedDateTimeParseException - if the parse failspublic DateTimeBuilder parseToBuilder(CharSequence text)
This parses to a DateTimeBuilder ensuring that the text is fully parsed.
This method throws DateTimeParseException if unable to parse, or
some other DateTimeException if another date/time problem occurs.
text - the text to parse, not nullDateTimeParseException - if the parse failspublic DateTimeBuilder parseToBuilder(CharSequence text, ParsePosition position)
This parses to a DateTimeBuilder but does not require the input to be fully parsed.
This method does not throw DateTimeParseException.
Instead, errors are returned within the state of the specified parse position.
Callers must check for errors before using the context.
This method may throw some other DateTimeException if a date/time problem occurs.
text - the text to parse, not nullposition - the position to parse from, updated with length parsed
and the index of any error, not nullIndexOutOfBoundsException - if the position is invalidpublic Format toFormat()
java.text.Format instance.
The returned Format instance will print any TemporalAccessor
and parses to a resolved DateTimeBuilder.
Exceptions will follow the definitions of Format, see those methods
for details about IllegalArgumentException during formatting and
ParseException or null during parsing.
The format does not support attributing of the returned format string.
public Format toFormat(Class<?> parseType)
java.text.Format instance that will
parse to the specified type.
The returned Format instance will print any TemporalAccessor
and parses to the type specified.
The type must be one that is supported by parse(java.lang.CharSequence, java.lang.Class<T>).
Exceptions will follow the definitions of Format, see those methods
for details about IllegalArgumentException during formatting and
ParseException or null during parsing.
The format does not support attributing of the returned format string.
parseType - the type to parse to, not nullCopyright © 2007-2013 ThreeTen.org. All Rights Reserved.