Class DateTimeFormatter

java.lang.Object
java.time.format.DateTimeFormatter

public final class DateTimeFormatter extends Object

Formats and parses dates and times: the ISO formats as constants, and any other through ofPattern(String).

Patterns follow the JDK's letters and counts. Supported: G era, u year, y year of era, M/L month, d day of month, D day of year, Q/q quarter, E day of week, a AM/PM, h, K, k, H hours, m minutes, s seconds, S fraction of a second, n nano of second, N nano of day, A milli of day, VV zone id, z (the zone id), O, X, x and Z offsets, quoted text, and [...] optional sections. Text is English whatever the locale. The week-based letters (Y, w, W, e, c, F, g), v, B and p are refused.

A pattern parses as the JDK's smart resolver does: a day of month past the end of its month becomes the last day, an hour alone is that hour on the dot, and a two-digit year is one of 2000 to 2099. The ISO formats resolve strictly.

  • Field Details

  • Method Details

    • ofPattern

      public static DateTimeFormatter ofPattern(String pattern)
    • ofPattern

      public static DateTimeFormatter ofPattern(String pattern, Locale locale)
    • withZone

      public DateTimeFormatter withZone(ZoneId zone)
      This formatter with zone overriding the zone of what it formats and supplying one to what it parses; null removes the override.
    • getZone

      public ZoneId getZone()
      The zone given to withZone(ZoneId), or null.
    • withLocale

      public DateTimeFormatter withLocale(Locale locale)
    • getLocale

      public Locale getLocale()
    • format

      public String format(TemporalAccessor temporal)
    • parse

      public TemporalAccessor parse(CharSequence text)
    • parse

      public <T> T parse(CharSequence text, TemporalQuery<T> query)
      Parses text and hands the result to query, as in formatter.parse(text, LocalDate::from).
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object