public final class DateTimeFormatter
extends java.lang.Object
This class provides the main application entry point for printing and parsing. Instances of DateTimeFormatter are constructed using DateTimeFormatterBuilder or by using one of the predefined constants on DateTimeFormatters.
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 |
---|---|
java.util.Locale |
getLocale()
Gets the locale to be used during formatting.
|
DateTimeFormatSymbols |
getSymbols()
Gets the set of symbols to be used during formatting.
|
<T> T |
parse(java.lang.CharSequence text,
java.lang.Class<T> type)
Fully parses the text producing an object of the specified type.
|
TemporalAccessor |
parseBest(java.lang.CharSequence text,
java.lang.Class<?>... types)
Fully parses the text producing an object of one of the specified types.
|
DateTimeBuilder |
parseToBuilder(java.lang.CharSequence text)
Parses the text to a builder.
|
DateTimeBuilder |
parseToBuilder(java.lang.CharSequence text,
java.text.ParsePosition position)
Parses the text to a builder.
|
java.lang.String |
print(TemporalAccessor temporal)
Prints a date-time object using this formatter.
|
void |
printTo(TemporalAccessor temporal,
java.lang.Appendable appendable)
Prints a date-time object to an
Appendable using this formatter. |
java.text.Format |
toFormat()
Returns this formatter as a
java.text.Format instance. |
java.text.Format |
toFormat(java.lang.Class<?> parseType)
Returns this formatter as a
java.text.Format instance that will
parse to the specified type. |
java.lang.String |
toString()
Returns a description of the underlying formatters.
|
DateTimeFormatter |
withLocale(java.util.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.
|
public java.util.Locale getLocale()
public DateTimeFormatter withLocale(java.util.Locale locale)
This instance is immutable and unaffected by this method call.
locale
- the new locale, not nullDateTimeFormatter
based on this one with the requested 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 nullDateTimeFormatter
based on this one with the requested symbols, not nullpublic java.lang.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, java.lang.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(java.lang.CharSequence text, java.lang.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(java.lang.CharSequence text, java.lang.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 nulljava.lang.IllegalArgumentException
- if less than 2 types are specifiedDateTimeParseException
- if the parse failspublic DateTimeBuilder parseToBuilder(java.lang.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(java.lang.CharSequence text, java.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 nulljava.lang.IndexOutOfBoundsException
- if the position is invalidpublic java.text.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 java.text.Format toFormat(java.lang.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(CharSequence, Class)
.
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 java.lang.String toString()
toString
in class java.lang.Object