Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class DateTimeFormatter

  1. Object
  2. DateTimeFormatter

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.

Fields

public static final DateTimeFormatter ISO_LOCAL_DATE
public static final DateTimeFormatter ISO_LOCAL_TIME
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME
public static final DateTimeFormatter ISO_ZONED_DATE_TIME
public static final DateTimeFormatter ISO_INSTANT

Methods

public static DateTimeFormatter ofPattern(String pattern)
public static DateTimeFormatter ofPattern(String pattern, Locale locale)
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.
public ZoneId getZone()The zone given to withZone(ZoneId), or null.
public DateTimeFormatter withLocale(Locale locale)
public Locale getLocale()
public String format(TemporalAccessor temporal)
public TemporalAccessor parse(CharSequence text)
public <T> T parse(CharSequence text, TemporalQuery<T> query)Parses text and hands the result to query, as in formatter.parse(text, LocalDate::from).
public String toString()Returns a string representation of the object.

Inherited methods

Field details

ISO_LOCAL_DATE

public static final DateTimeFormatter ISO_LOCAL_DATE

ISO_LOCAL_TIME

public static final DateTimeFormatter ISO_LOCAL_TIME

ISO_LOCAL_DATE_TIME

public static final DateTimeFormatter ISO_LOCAL_DATE_TIME

ISO_OFFSET_DATE_TIME

public static final DateTimeFormatter ISO_OFFSET_DATE_TIME

ISO_ZONED_DATE_TIME

public static final DateTimeFormatter ISO_ZONED_DATE_TIME

ISO_INSTANT

public static final DateTimeFormatter ISO_INSTANT

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()
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())