Class DateTimeFormatter

java.lang.Object
javax.time.calendar.format.DateTimeFormatter

public final class DateTimeFormatter extends Object
Formatter for printing and parsing calendricals.

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.

Not all formatters can print and parse. Some can only print, while others can only parse. The isPrintSupported() and isParseSupported() methods determine which operations are available.

Some applications may need to use the older Format class for formatting. The toFormat() method returns an implementation of the old API.

DateTimeFormatter is immutable and thread-safe.

  • Field Details

  • Constructor Details

    • DateTimeFormatter

      DateTimeFormatter(Locale locale, CompositePrinterParser printerParser)
      Constructor.
      Parameters:
      locale - the locale to use for text formatting, not null
      printerParser - the printer/parser to use, not null
    • DateTimeFormatter

      private DateTimeFormatter(DateTimeFormatSymbols symbols, CompositePrinterParser printerParser)
      Constructor used by immutable copying.
      Parameters:
      symbols - the symbols to use for text formatting, not null
      asciiNumerics - whether to use ASCII numerics (true) or locale numerics (false)
      printerParser - the printer/parser to use, not null
  • Method Details

    • checkNotNull

      static void checkNotNull(Object object, String errorMessage)
      Validates that the input value is not null.
      Parameters:
      object - the object to check
      errorMessage - the error to throw
      Throws:
      NullPointerException - if the object is null
    • getLocale

      public Locale getLocale()
      Gets the locale to be used during formatting.
      Returns:
      the locale of this DateTimeFormatter, never null
    • withLocale

      public DateTimeFormatter withLocale(Locale locale)
      Returns a copy of this DateTimeFormatter with a new locale.

      This instance is immutable and unaffected by this method call.

      Parameters:
      locale - the new locale, not null
      Returns:
      a new DateTimeFormatter with the same format and the new locale, never null
    • isPrintSupported

      public boolean isPrintSupported()
      Checks whether this formatter can print.

      Depending on how this formatter is initialized, it may not be possible for it to print at all. This method allows the caller to check whether the print methods will throw UnsupportedOperationException or not.

      Returns:
      true if the formatter supports printing
    • print

      public String print(Calendrical calendrical)
      Prints the calendrical using this formatter.

      This method prints the calendrical to a String.

      Parameters:
      calendrical - the calendrical to print, not null
      Returns:
      the printed string, never null
      Throws:
      UnsupportedOperationException - if this formatter cannot print
      NullPointerException - if the calendrical is null
      CalendricalPrintException - if an error occurs during printing
    • print

      public void print(Calendrical calendrical, Appendable appendable)
      Prints the calendrical to an Appendable using this formatter.

      This method prints the calendrical to the specified Appendable. 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 CalendricalPrintException.rethrowIOException() for a means to extract the IOException.

      Parameters:
      calendrical - the calendrical to print, not null
      appendable - the appendable to print to, not null
      Throws:
      UnsupportedOperationException - if this formatter cannot print
      NullPointerException - if the calendrical or appendable is null
      CalendricalPrintException - if an error occurs during printing
    • isParseSupported

      public boolean isParseSupported()
      Checks whether this formatter can parse.

      Depending on how this formatter is initialized, it may not be possible for it to parse at all. This method allows the caller to check whether the parse methods will throw UnsupportedOperationException or not.

      Returns:
      true if the formatter supports parsing
    • parse

      public <T> T parse(String text, CalendricalRule<T> rule)
      Fully parses the text producing an object of the type defined by the rule.

      This parses the entire text to produce the required calendrical value. For example:

       LocalDateTime dt = parser.parse(str, LocalDateTime.rule());
       
      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.
      Parameters:
      text - the text to parse, not null
      Returns:
      the parsed date, never null
      Throws:
      UnsupportedOperationException - if this formatter cannot parse
      NullPointerException - if the text is null
      CalendricalParseException - if the parse fails
    • parse

      public CalendricalMerger parse(String text)
      Fully parses the text returning a merger that can be used to manage the merging of separate parsed fields to a meaningful calendrical.

      If the parse completes without reading the entire length of the text, or a problem occurs during parsing, then an exception is thrown.

      The result may be invalid including out of range values such as a month of 65. The methods on the calendrical allow you to handle the invalid input. For example:

       LocalDateTime dt = parser.parse(str).merge().get(LocalDateTime.rule());
       
      Parameters:
      text - the text to parse, not null
      Returns:
      the parsed text, never null
      Throws:
      UnsupportedOperationException - if this formatter cannot parse
      NullPointerException - if the text is null
      CalendricalParseException - if the parse fails
    • parse

      public DateTimeParseContext parse(String text, ParsePosition position)
      Parses the text into a Calendrical.

      The result may be invalid including out of range values such as a month of 65. The methods on the calendrical allow you to handle the invalid input. For example:

       LocalDateTime dt = parser.parse(str).mergeStrict().toLocalDateTime();
       
      Parameters:
      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 only if the parse results in an error
      Throws:
      UnsupportedOperationException - if this formatter cannot parse
      NullPointerException - if the text or position is null
      IndexOutOfBoundsException - if the position is invalid
    • toPrinterParser

      CompositePrinterParser toPrinterParser(boolean optional)
      Returns the formatter as a composite printer parser.
      Parameters:
      optional - whether the printer/parser should be optional
      Returns:
      the printer/parser, never null
    • toFormat

      public Format toFormat()
      Returns this formatter as a java.text.Format instance.

      The Format instance will print any Calendrical and parses to a merged CalendricalMerger.

      The format will throw UnsupportedOperationException and IndexOutOfBoundsException in line with those thrown by the print and parse methods.

      The format does not support attributing of the returned format string.

      Returns:
      this formatter as a classic format instance, never null
    • toString

      public String toString()
      Returns a description of the underlying formatters.
      Overrides:
      toString in class Object
      Returns:
      the pattern that will be used, never null