org.threeten.bp.format

DateTimeFormatter

final class DateTimeFormatter extends AnyRef

Formatter for printing and parsing date-time objects.

This class provides the main application entry point for printing and parsing. Common instances of DateTimeFormatter are provided:

For more complex formatters, a DateTimeFormatterBuilder 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 formatting, format(DateTimeFormatter formatter), and one for parsing, For example:

String text = date.format(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.

Specification for implementors

This class is immutable and thread-safe.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. DateTimeFormatter
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def format(temporal: TemporalAccessor): String

    Formats a date-time object using this formatter.

    Formats a date-time object using this formatter.

    This formats the date-time to a String using the rules of the formatter.

    temporal

    the temporal object to print, not null

    returns

    the printed string, not null

    Exceptions thrown
    DateTimeException

    if an error occurs during formatting

  12. def formatTo(temporal: TemporalAccessor, appendable: Appendable): Unit

    Formats a date-time object to an Appendable using this formatter.

    Formats a date-time object to an Appendable using this formatter.

    This formats 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.

    temporal

    the temporal object to print, not null

    appendable

    the appendable to print to, not null

    Exceptions thrown
    DateTimeException

    if an error occurs during formatting

  13. def getChronology: Chronology

    Gets the overriding chronology to be used during formatting.

    Gets the overriding chronology to be used during formatting.

    This returns the override chronology, used to convert dates. By default, a formatter has no override chronology, returning null. See #withChronology(Chronology) for more details on overriding.

    returns

    the chronology of this formatter, null if no override

  14. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  15. def getDecimalStyle: DecimalStyle

    Gets the decimal style to be used during formatting.

    Gets the decimal style to be used during formatting.

    returns

    the decimal style of this formatter, not null

  16. def getLocale: Locale

    Gets the locale to be used during formatting.

    Gets the locale to be used during formatting.

    This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern.

    returns

    the locale of this formatter, not null

  17. def getResolverFields: Set[TemporalField]

    Gets the resolver fields to use during parsing.

    Gets the resolver fields to use during parsing.

    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 #withResolverFields(Set) for more details.

    returns

    the immutable set of resolver fields of this formatter, null if no fields

  18. def getResolverStyle: ResolverStyle

    Gets the resolver style to use during parsing.

    Gets the resolver style to use during parsing.

    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 ResolverStyle#SMART SMART resolver style. See #withResolverStyle(ResolverStyle) for more details.

    returns

    the resolver style of this formatter, not null

  19. def getZone: ZoneId

    Gets the overriding zone to be used during formatting.

    Gets the overriding zone to be used during formatting.

    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.

    returns

    the chronology of this formatter, null if no override

  20. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  21. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  22. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  23. final def notify(): Unit

    Definition Classes
    AnyRef
  24. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  25. def parse[T](text: CharSequence, type: TemporalQuery[T]): T

    Fully parses the text producing an object of the specified type.

    Fully parses the text producing an object of the specified 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.FROM);
    
    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 extract

    text

    the text to parse, not null

    type

    the type to extract, not null

    returns

    the parsed date-time, not null

    Exceptions thrown
    DateTimeParseException

    if unable to parse the requested result

  26. def parse(text: CharSequence, position: ParsePosition): TemporalAccessor

    Parses the text using this formatter, providing control over the text position.

    Parses the text using this formatter, providing control over the text position.

    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 TemporalAccessor which has been resolved, applying basic validation checks to help ensure a valid date-time.

    The text will be parsed from the specified start ParsePosition. The entire length of the text does not have to be parsed, the ParsePosition will be updated with the index at the end of parsing.

    The operation of this method is slightly different to similar methods using ParsePosition on java.text.Format. That class will return errors using the error index on the ParsePosition. By contrast, this method will throw a 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.

    If the formatter parses the same field more than once with different values, the result will be an error.

    text

    the text to parse, not null

    position

    the position to parse from, updated with length parsed and the index of any error, not null

    returns

    the parsed temporal object, not null

    Exceptions thrown
    DateTimeParseException

    if unable to parse the requested result

    IndexOutOfBoundsException

    if the position is invalid

  27. def parse(text: CharSequence): TemporalAccessor

    Fully parses the text producing a temporal object.

    Fully parses the text producing a temporal object.

    This parses the entire text producing a temporal object. It is typically more useful to use #parse(CharSequence, TemporalQuery). The result of this method is TemporalAccessor which has been resolved, applying basic validation checks to help ensure a valid date-time.

    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 null

    returns

    the parsed temporal object, not null

    Exceptions thrown
    DateTimeParseException

    if unable to parse the requested result

  28. def parseBest(text: CharSequence, types: TemporalQuery[_]*): TemporalAccessor

    Fully parses the text producing an object of one of the specified types.

    Fully parses the text producing an object of one of the specified types.

    This parse method is convenient for use when the parser can handle optional elements. For example, a pattern of 'yyyy[-MM[-dd]]' can be fully parsed to a LocalDate, or partially parsed to a YearMonth or a Year. 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, LocalDate.FROM, YearMonth.FROM);
    if (dt instanceof LocalDate) {
    ...
    } 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 null

    types

    the types to attempt to parse to, which must implement { @code TemporalAccessor}, not null

    returns

    the parsed date-time, not null

    Annotations
    @varargs()
    Exceptions thrown
    DateTimeParseException

    if unable to parse the requested result

    IllegalArgumentException

    if less than 2 types are specified

  29. def parseUnresolved(text: CharSequence, position: ParsePosition): TemporalAccessor

    Parses the text using this formatter, without resolving the result, intended for advanced use cases.

    Parses the text using this formatter, without resolving the result, intended for advanced use cases.

    Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a Map of field to value, a ZoneId and a Chronology. Second, the parsed data is resolved, by validating, combining and simplifying the various fields into more useful ones. This method performs the parsing stage but not the resolving stage.

    The result of this method is 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'.

    The text will be parsed from the specified start ParsePosition. The entire length of the text does not have to be parsed, the ParsePosition will be updated with the index at the end of parsing.

    Errors are returned using the error index field of the ParsePosition instead of DateTimeParseException. The returned error index will be set to an index indicative of the error. Callers must check for errors before using the context.

    If the formatter parses the same field more than once with different values, the result will be an error.

    This method is intended for advanced use cases that need access to the internal state during parsing. Typical application code should use #parse(CharSequence, TemporalQuery) or the parse method on the target type.

    text

    the text to parse, not null

    position

    the position to parse from, updated with length parsed and the index of any error, not null

    returns

    the parsed text, null if the parse results in an error

    Exceptions thrown
    DateTimeException

    if some problem occurs during parsing

    IndexOutOfBoundsException

    if the position is invalid

  30. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  31. def toFormat(query: TemporalQuery[_ <: AnyRef]): Format

    Returns this formatter as a java.text.Format instance that will parse to the specified type.

    Returns this formatter as a 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.

    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.

    query

    the query to parse to, not null

    returns

    this formatter as a classic format instance, not null

  32. def toFormat: Format

    Returns this formatter as a java.text.Format instance.

    Returns this formatter as a java.text.Format instance.

    The returned Format instance will print any TemporalAccessor and parses to a resolved TemporalAccessor.

    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.

    returns

    this formatter as a classic format instance, not null

  33. def toString(): String

    Returns a description of the underlying formatters.

    Returns a description of the underlying formatters.

    returns

    a description of this formatter, not null

    Definition Classes
    DateTimeFormatter → AnyRef → Any
  34. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  35. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  36. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  37. def withChronology(chrono: Chronology): DateTimeFormatter

    Returns a copy of this formatter with a new override chronology.

    Returns a copy of this formatter with a new override chronology.

    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 plain ChronoField 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 null

    returns

    a formatter based on this formatter with the requested override chronology, not null

  38. def withDecimalStyle(decimalStyle: DecimalStyle): DateTimeFormatter

    Returns a copy of this formatter with a new decimal style.

    Returns a copy of this formatter with a new decimal style.

    This instance is immutable and unaffected by this method call.

    decimalStyle

    the new decimal style, not null

    returns

    a formatter based on this formatter with the requested symbols, not null

  39. def withLocale(locale: Locale): DateTimeFormatter

    Returns a copy of this formatter with a new locale.

    Returns a copy of this formatter with a new 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 null

    returns

    a formatter based on this formatter with the requested locale, not null

  40. def withResolverFields(resolverFields: Set[TemporalField]): DateTimeFormatter

    Returns a copy of this formatter with a new set of resolver fields.

    Returns a copy of this formatter with a new set of resolver fields.

    This returns a formatter with similar state to this formatter but with the resolver fields set. By default, a formatter has no resolver fields.

    Changing the resolver fields 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 fields are used to filter the field-value pairs between phase 1 and 2.

    This can be used to select between two or more ways that a date or time might be resolved. For example, if the formatter consists of year, month, day-of-month and day-of-year, then there are two ways to resolve a date. Calling this method with the arguments ChronoField#YEAR YEAR and ChronoField#DAY_OF_YEAR DAY_OF_YEAR will ensure that the date is resolved using the year and day-of-year, effectively meaning that the month and day-of-month are ignored during the resolving phase.

    In a similar manner, this method can be used to ignore secondary fields that would otherwise be cross-checked. For example, if the formatter consists of year, month, day-of-month and day-of-week, then there is only one way to resolve a date, but the parsed value for day-of-week will be cross-checked against the resolved date. Calling this method with the arguments ChronoField#YEAR YEAR, ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR and ChronoField#DAY_OF_MONTH DAY_OF_MONTH will ensure that the date is resolved correctly, but without any cross-check for the day-of-week.

    In implementation terms, this method behaves as follows. The result of the parsing phase can be considered to be a map of field to value. The behavior of this method is to cause that map to be filtered between phase 1 and 2, removing all fields other than those specified as arguments to this method.

    This instance is immutable and unaffected by this method call.

    resolverFields

    the new set of resolver fields, null if no fields

    returns

    a formatter based on this formatter with the requested resolver style, not null

  41. def withResolverFields(resolverFields: TemporalField*): DateTimeFormatter

    Returns a copy of this formatter with a new set of resolver fields.

    Returns a copy of this formatter with a new set of resolver fields.

    This returns a formatter with similar state to this formatter but with the resolver fields set. By default, a formatter has no resolver fields.

    Changing the resolver fields 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 fields are used to filter the field-value pairs between phase 1 and 2.

    This can be used to select between two or more ways that a date or time might be resolved. For example, if the formatter consists of year, month, day-of-month and day-of-year, then there are two ways to resolve a date. Calling this method with the arguments ChronoField#YEAR YEAR and ChronoField#DAY_OF_YEAR DAY_OF_YEAR will ensure that the date is resolved using the year and day-of-year, effectively meaning that the month and day-of-month are ignored during the resolving phase.

    In a similar manner, this method can be used to ignore secondary fields that would otherwise be cross-checked. For example, if the formatter consists of year, month, day-of-month and day-of-week, then there is only one way to resolve a date, but the parsed value for day-of-week will be cross-checked against the resolved date. Calling this method with the arguments ChronoField#YEAR YEAR, ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR and ChronoField#DAY_OF_MONTH DAY_OF_MONTH will ensure that the date is resolved correctly, but without any cross-check for the day-of-week.

    In implementation terms, this method behaves as follows. The result of the parsing phase can be considered to be a map of field to value. The behavior of this method is to cause that map to be filtered between phase 1 and 2, removing all fields other than those specified as arguments to this method.

    This instance is immutable and unaffected by this method call.

    resolverFields

    the new set of resolver fields, null if no fields

    returns

    a formatter based on this formatter with the requested resolver style, not null

  42. def withResolverStyle(resolverStyle: ResolverStyle): DateTimeFormatter

    Returns a copy of this formatter with a new resolver style.

    Returns a copy of this formatter with a new resolver style.

    This returns a formatter with similar state to this formatter but with the resolver style set. By default, a formatter has the ResolverStyle#SMART SMART resolver style.

    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 ResolverStyle for more information on the options available.

    This instance is immutable and unaffected by this method call.

    resolverStyle

    the new resolver style, not null

    returns

    a formatter based on this formatter with the requested resolver style, not null

  43. def withZone(zone: ZoneId): DateTimeFormatter

    Returns a copy of this formatter with a new override zone.

    Returns a copy of this formatter with a new override 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 plain ChronoField 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 null

    returns

    a formatter based on this formatter with the requested override zone, not null

Inherited from AnyRef

Inherited from Any

Ungrouped