Class DateTimeFormatterJalali
This class provides the main application entry point for printing and parsing
and provides common implementations of DateTimeFormatter:
- Using predefined constants, such as
ISO_LOCAL_DATE - Using pattern letters, such as
uuuu-MMM-dd - Using localized styles, such as
longormedium
More complex formatters are provided by
DateTimeFormatterJalaliBuilder.
The main date-time classes provide two methods - one for formatting,
format(DateTimeFormatter formatter), and one for parsing,
parse(CharSequence text, DateTimeFormatterJalali formatter).
For example:
LocalDate date = LocalDate.now(); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter);
In addition to the format, formatters can be created with desired Locale, Chronology, ZoneId, and DecimalStyle.
The withLocale method returns a new formatter that
overrides the locale. The locale affects some aspects of formatting and parsing
The withChronology method returns a new formatter
that overrides the chronology. If overridden, the date-time value is
converted to the chronology before formatting. During parsing the date-time
value is converted to the chronology before it is returned.
The withZone method returns a new formatter that overrides
the zone. If overridden, the date-time value is converted to a ZonedDateTime
with the requested ZoneId before formatting. During parsing the ZoneId is
applied before the value is returned.
The withDecimalStyle method returns a new formatter that
overrides the DecimalStyle. The DecimalStyle symbols are used for
formatting and parsing.
| Formatter | Description | Example |
|---|---|---|
BASIC_ISO_DATE |
Basic ISO date | '20111203' |
ISO_LOCAL_DATE |
ISO Local Date | '2011-12-03' |
ISO_OFFSET_DATE |
ISO Date with offset | '2011-12-03+01:00' |
ISO_DATE |
ISO Date with or without offset | '2011-12-03+01:00'; '2011-12-03' |
ISO_LOCAL_TIME |
Time without offset | '10:15:30' |
ISO_OFFSET_TIME |
Time with offset | '10:15:30+01:00' |
ISO_TIME |
Time with or without offset | '10:15:30+01:00'; '10:15:30' |
ISO_LOCAL_DATE_TIME |
ISO Local Date and Time | '2011-12-03T10:15:30' |
ISO_OFFSET_DATE_TIME |
Date Time with Offset | 2011-12-03T10:15:30+01:00' |
ISO_ZONED_DATE_TIME |
Zoned Date Time | '2011-12-03T10:15:30+01:00[Europe/Paris]' |
ISO_DATE_TIME |
Date and time with ZoneId | '2011-12-03T10:15:30+01:00[Europe/Paris]' |
ISO_ORDINAL_DATE |
Year and day of year | '2012-337' |
ISO_WEEK_DATE |
Year and Week | 2012-W48-6' |
ISO_INSTANT |
Date and Time of an Instant | '2011-12-03T10:15:30Z' |
RFC_1123_DATE_TIME |
RFC 1123 / RFC 822 | 'Tue, 3 Jun 2008 11:05:30 GMT' |
Patterns are based on a simple sequence of letters and symbols.
"d MMM uuuu" will format 2011-12-03 as '3 Dec 2011'.
A formatter created from a pattern can be used as many times as necessary,
it is immutable and is thread-safe.
For example:
LocalDate date = LocalDate.now();
DateTimeFormatterJalali formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The following pattern letters are defined:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
p pad next pad modifier 1
' escape for text delimiter
'' single quote literal '
[ optional section start
] optional section end
# reserved for future use
{ reserved for future use
} reserved for future use
The count of pattern letters determines the format.
Text: The text style is determined based on the number of pattern
letters used. Less than 4 pattern letters will use the
short form. Exactly 4 pattern letters will use the
full form. Exactly 5 pattern letters will use the
narrow form.
Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles.
Number: If the count of letters is one, then the value is output using the minimum number of digits and without padding. Otherwise, the count of digits is used as the width of the output field, with the value zero-padded as necessary. The following pattern letters have constraints on the count of letters. Only one letter of 'c' and 'F' can be specified. Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified. Up to three letters of 'D' can be specified.
Number/Text: If the count of pattern letters is 3 or greater, use the Text rules above. Otherwise use the Number rules above.
Fraction: Outputs the nano-of-second field as a fraction-of-second. The nano-of-second value has nine digits, thus the count of pattern letters is from 1 to 9. If it is less than 9, then the nano-of-second value is truncated, with only the most significant digits being output.
Year: The count of letters determines the minimum field width below
which padding is used. If the count of letters is two, then a
reduced two digit form is
used. For printing, this outputs the rightmost two digits. For parsing, this
will parse using the base value of 2000, resulting in a year within the range
2000 to 2099 inclusive. If the count of letters is less than four (but not
two), then the sign is only output for negative years as per
SignStyle.NORMAL. Otherwise, the sign is output if the pad width is
exceeded, as per SignStyle.EXCEEDS_PAD.
ZoneId: This outputs the time-zone ID, such as 'Europe/Paris'. If the
count of letters is two, then the time-zone ID is output. Any other count of
letters throws IllegalArgumentException.
Zone names: This outputs the display name of the time-zone ID. If the
count of letters is one, two or three, then the short name is output. If the
count of letters is four, then the full name is output. Five or more letters
throws IllegalArgumentException.
Offset X and x: This formats the offset based on the number of pattern
letters. One letter outputs just the hour, such as '+01', unless the minute
is non-zero in which case the minute is also output, such as '+0130'. Two
letters outputs the hour and minute, without a colon, such as '+0130'. Three
letters outputs the hour and minute, with a colon, such as '+01:30'. Four
letters outputs the hour and minute and optional second, without a colon,
such as '+013015'. Five letters outputs the hour and minute and optional
second, with a colon, such as '+01:30:15'. Six or more letters throws
IllegalArgumentException. Pattern letter 'X' (upper case) will output
'Z' when the offset to be output would be zero, whereas pattern letter 'x'
(lower case) will output '+00', '+0000', or '+00:00'.
Offset O: This formats the localized offset based on the number of
pattern letters. One letter outputs the short
form of the localized offset, which is localized offset text, such as 'GMT',
with hour without leading zero, optional 2-digit minute and second if
non-zero, and colon, for example 'GMT+8'. Four letters outputs the
full form, which is localized offset text,
such as 'GMT, with 2-digit hour and minute field, optional second field
if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters
throws IllegalArgumentException.
Offset Z: This formats the offset based on the number of pattern
letters. One, two or three letters outputs the hour and minute, without a
colon, such as '+0130'. The output will be '+0000' when the offset is zero.
Four letters outputs the full form of localized
offset, equivalent to four letters of Offset-O. The output will be the
corresponding localized offset text if the offset is zero. Five
letters outputs the hour, minute, with optional second if non-zero, with
colon. It outputs 'Z' if the offset is zero.
Six or more letters throws IllegalArgumentException.
Optional section: The optional section markers work exactly like
calling DateTimeFormatterBuilderJalali.optionalStart() and
DateTimeFormatterBuilderJalali.optionalEnd().
Pad modifier: Modifies the pattern that immediately follows to be
padded with spaces. The pad width is determined by the number of pattern
letters. This is the same as calling
DateTimeFormatterBuilderJalali.padNext(int).
For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.
Any unrecognized letter is an error. Any non-letter character, other than '[', ']', '{', '}', '#' and the single quote will be output directly. Despite this, it is recommended to use single quotes around all characters that you want to output directly to ensure that future changes do not break your application.
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.
Five parsing methods are supplied by this class. Four of these perform both the parse and resolve phases.
The resolve phase is controlled by two parameters, set on this class.
For example, if the formatter has parsed a year, month, day-of-month and day-of-year, then there are two approaches to resolve a date: (year + month + day-of-month) and (year + day-of-year). The resolver fields allows one of the two approaches to be selected. If no resolver fields are set then both approaches must result in the same date.
Resolving separate fields to form a complete date and time is a complex process with behaviour distributed across a number of classes. It follows these steps:
- The chronology is determined.
The chronology of the result is either the chronology that was parsed,
or if no chronology was parsed, it is the chronology set on this class,
or if that is null, it is
IsoChronology. - The
ChronoFielddate fields are resolved. This is achieved usingChronology.resolveDate(Map, ResolverStyle). Documentation about field resolution is located in the implementation ofChronology. - The
ChronoFieldtime fields are resolved. This is documented onChronoFieldand is the same for all chronologies. - Any fields that are not
ChronoFieldare processed. This is achieved usingTemporalField.resolve(Map, TemporalAccessor, ResolverStyle). Documentation about field resolution is located in the implementation ofTemporalField. - The
ChronoFielddate and time fields are re-resolved. This allows fields in step four to produceChronoFieldvalues and have them be processed into dates and times. - A
LocalTimeis formed if there is at least an hour-of-day available. This involves providing default values for minute, second and fraction of second. - Any remaining unresolved fields are cross-checked against any date and/or time that was resolved. Thus, an earlier stage would resolve (year + month + day-of-month) to a date, and this stage would check that day-of-week was valid for the date.
This class is immutable and thread-safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DateTimeFormatterJalaliThe ISO date formatter that formats or parses a date without an offset, such as '20111203'.static final DateTimeFormatterJalaliThe ISO date formatter that formats or parses a date with the offset if available, such as '2011-12-03' or '2011-12-03+01:00'.static final DateTimeFormatterJalaliThe ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'.static final DateTimeFormatterJalaliThe ISO instant formatter that formats or parses an instant in UTC, such as '2011-12-03T10:15:30Z'.static final DateTimeFormatterJalaliThe ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'.static final DateTimeFormatterJalaliThe ISO date-time formatter that formats or parses a date-time without an offset, such as '2011-12-03T10:15:30'.static final DateTimeFormatterJalaliThe ISO time formatter that formats or parses a time without an offset, such as '10:15' or '10:15:30'.static final DateTimeFormatterJalaliThe ISO date formatter that formats or parses a date with an offset, such as '2011-12-03+01:00'.static final DateTimeFormatterJalaliThe ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30+01:00'.static final DateTimeFormatterJalaliThe ISO time formatter that formats or parses a time with an offset, such as '10:15+01:00' or '10:15:30+01:00'.static final DateTimeFormatterJalaliThe ISO date formatter that formats or parses the ordinal date without an offset, such as '2012-337'.static final DateTimeFormatterJalaliThe ISO time formatter that formats or parses a time, with the offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'.static final DateTimeFormatterJalaliThe ISO date formatter that formats or parses the week-based date without an offset, such as '2012-W48-6'.static final DateTimeFormatterJalaliThe ISO-like date-time formatter that formats or parses a date-time with offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.static final DateTimeFormatterJalaliThe RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'. -
Method Summary
Modifier and TypeMethodDescriptionformat(TemporalAccessor temporal) Formats a date-time object using this formatter.voidformatTo(TemporalAccessor temporal, Appendable appendable) Formats a date-time object to anAppendableusing this formatter.Gets the overriding chronology to be used during formatting.getDayOfWeek(Locale locale) org.bardframework.time.format.DecimalStyleGets the DecimalStyle to be used during formatting.Gets the locale to be used during formatting.getMonthOfYear(Locale locale) Gets the resolver style to use during parsing.getZone()Gets the overriding zone to be used during formatting.static DateTimeFormatterJalaliCreates a formatter using the specified pattern.parse(CharSequence text) Fully parses the text producing a temporal object.parse(CharSequence text, ParsePosition position) Parses the text using this formatter, providing control over the text position.<T> Tparse(CharSequence text, TemporalQuery<T> query) Fully parses the text producing an object of the specified type.static TemporalQuery<Boolean> A query that provides access to whether a leap-second was parsed.toString()Returns a description of the underlying formatters.withChronology(Chronology chrono) Returns a copy of this formatter with a new override chronology.withDecimalStyle(org.bardframework.time.format.DecimalStyle decimalStyle) Returns a copy of this formatter with a new DecimalStyle.withLocale(Locale locale) Returns a copy of this formatter with a new locale.Returns a copy of this formatter with a new override zone.
-
Field Details
-
ISO_LOCAL_DATE
The ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local date format. The format consists of:
- Four digits or more for the
year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol. - A dash
- Two digits for the
month-of-year. This is pre-padded by zero to ensure two digits. - A dash
- Two digits for the
day-of-month. This is pre-padded by zero to ensure two digits.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - Four digits or more for the
-
RFC_1123_DATE_TIME
The RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'.This returns an immutable formatter capable of formatting and parsing most of the RFC-1123 format. RFC-1123 updates RFC-822 changing the year from two digits to four. This implementation requires a four digit year. This implementation also does not handle North American or military zone names, only 'GMT' and offset amounts.
The format consists of:
- If the day-of-week is not available to format or parse then jump to day-of-month.
- Three letter
day-of-weekin English. - A comma
- A space
- One or two digits for the
day-of-month. - A space
- Three letter
month-of-yearin English. - A space
- Four digits for the
year. Only years in the range 0000 to 9999 are supported. - A space
- Two digits for the
hour-of-day. This is pre-padded by zero to ensure two digits. - A colon
- Two digits for the
minute-of-hour. This is pre-padded by zero to ensure two digits. - If the second-of-minute is not available then jump to the next space.
- A colon
- Two digits for the
second-of-minute. This is pre-padded by zero to ensure two digits. - A space
- The
offset IDwithout colons or seconds. An offset of zero uses "GMT". North American zone names and military zone names are not handled.
Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
SMARTresolver style. -
ISO_OFFSET_DATE
The ISO date formatter that formats or parses a date with an offset, such as '2011-12-03+01:00'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset date format. The format consists of:
- The
ISO_LOCAL_DATE - The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_DATE
The ISO date formatter that formats or parses a date with the offset if available, such as '2011-12-03' or '2011-12-03+01:00'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended date format. The format consists of:
- The
ISO_LOCAL_DATE - If the offset is not available then the format is complete.
- The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_LOCAL_TIME
The ISO time formatter that formats or parses a time without an offset, such as '10:15' or '10:15:30'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local time format. The format consists of:
- Two digits for the
hour-of-day. This is pre-padded by zero to ensure two digits. - A colon
- Two digits for the
minute-of-hour. This is pre-padded by zero to ensure two digits. - If the second-of-minute is not available then the format is complete.
- A colon
- Two digits for the
second-of-minute. This is pre-padded by zero to ensure two digits. - If the nano-of-second is zero or not available then the format is complete.
- A decimal point
- One to nine digits for the
nano-of-second. As many digits will be output as required.
The returned formatter has no override chronology or zone. It uses the
STRICTresolver style. - Two digits for the
-
ISO_OFFSET_TIME
The ISO time formatter that formats or parses a time with an offset, such as '10:15+01:00' or '10:15:30+01:00'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset time format. The format consists of:
- The
ISO_LOCAL_TIME - The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has no override chronology or zone. It uses the
STRICTresolver style. - The
-
ISO_TIME
The ISO time formatter that formats or parses a time, with the offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset time format. The format consists of:
- The
ISO_LOCAL_TIME - If the offset is not available then the format is complete.
- The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
STRICTresolver style. - The
-
ISO_LOCAL_DATE_TIME
The ISO date-time formatter that formats or parses a date-time without an offset, such as '2011-12-03T10:15:30'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset date-time format. The format consists of:
- The
ISO_LOCAL_DATE - The letter 'T'. Parsing is case insensitive.
- The
ISO_LOCAL_TIME
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_OFFSET_DATE_TIME
The ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30+01:00'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset date-time format. The format consists of:
- The
ISO_LOCAL_DATE_TIME - The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_ZONED_DATE_TIME
The ISO-like date-time formatter that formats or parses a date-time with offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.This returns an immutable formatter capable of formatting and parsing a format that extends the ISO-8601 extended offset date-time format to add the time-zone. The section in square brackets is not part of the ISO-8601 standard. The format consists of:
- The
ISO_OFFSET_DATE_TIME - If the zone ID is not available or is a
ZoneOffsetthen the format is complete. - An open square bracket '['.
- The
zone ID. This is not part of the ISO-8601 standard. Parsing is case sensitive. - A close square bracket ']'.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_DATE_TIME
The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local or offset date-time format, as well as the extended non-ISO form specifying the time-zone. The format consists of:
- The
ISO_LOCAL_DATE_TIME - If the offset is not available to format or parse then the format is complete.
- The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. - If the zone ID is not available or is a
ZoneOffsetthen the format is complete. - An open square bracket '['.
- The
zone ID. This is not part of the ISO-8601 standard. Parsing is case sensitive. - A close square bracket ']'.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - The
-
ISO_ORDINAL_DATE
The ISO date formatter that formats or parses the ordinal date without an offset, such as '2012-337'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended ordinal date format. The format consists of:
- Four digits or more for the
year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol. - A dash
- Three digits for the
day-of-year. This is pre-padded by zero to ensure three digits. - If the offset is not available to format or parse then the format is complete.
- The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - Four digits or more for the
-
ISO_WEEK_DATE
The ISO date formatter that formats or parses the week-based date without an offset, such as '2012-W48-6'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended week-based date format. The format consists of:
- Four digits or more for the
week-based-year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol. - A dash
- The letter 'W'. Parsing is case insensitive.
- Two digits for the
week-of-week-based-year. This is pre-padded by zero to ensure three digits. - A dash
- One digit for the
day-of-week. The value run from Monday (1) to Sunday (7). - If the offset is not available to format or parse then the format is complete.
- The
offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - Four digits or more for the
-
ISO_INSTANT
The ISO instant formatter that formats or parses an instant in UTC, such as '2011-12-03T10:15:30Z'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 instant format. When formatting, the second-of-minute is always output. The nano-of-second outputs zero, three, six or nine digits digits as necessary. When parsing, time to at least the seconds field is required. Fractional seconds from zero to nine are parsed. The localized decimal style is not used.
This is a special case formatter intended to allow a human readable form of an
Instant. TheInstantclass is designed to only represent a point in time and internally stores a value in nanoseconds from a fixed epoch of 1970-01-01Z. As such, anInstantcannot be formatted as a date or time without providing some form of time-zone. This formatter allows theInstantto be formatted, by providing a suitable conversion usingZoneOffset.UTC.The format consists of:
- The
ISO_OFFSET_DATE_TIMEwhere the instant is converted fromChronoField.INSTANT_SECONDSandChronoField.NANO_OF_SECONDusing theUTCoffset. Parsing is case insensitive.
The returned formatter has no override chronology or zone. It uses the
STRICTresolver style. - The
-
BASIC_ISO_DATE
The ISO date formatter that formats or parses a date without an offset, such as '20111203'.This returns an immutable formatter capable of formatting and parsing the ISO-8601 basic local date format. The format consists of:
- Four digits for the
year. Only years in the range 0000 to 9999 are supported. - Two digits for the
month-of-year. This is pre-padded by zero to ensure two digits. - Two digits for the
day-of-month. This is pre-padded by zero to ensure two digits. - If the offset is not available to format or parse then the format is complete.
- The
offset IDwithout colons. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the
STRICTresolver style. - Four digits for the
-
-
Method Details
-
ofPattern
Creates a formatter using the specified pattern.This method will create a formatter based on a simple pattern of letters and symbols as described in the class documentation. For example,
d MMM uuuuwill format 2011-12-03 as '3 Dec 2011'.The formatter will use the
default FORMAT locale. This can be changed usingwithLocale(Locale)on the returned formatterThe returned formatter has no override chronology or zone. It uses
SMARTresolver style.- Parameters:
pattern- the pattern to use, not null- Returns:
- the formatter based on the pattern, not null
- Throws:
IllegalArgumentException- if the pattern is invalid- See Also:
-
parsedLeapSecond
A query that provides access to whether a leap-second was parsed.This returns a singleton query that provides access to additional information from the parse. The query always returns a non-null boolean, true if parsing saw a leap-second, false if not.
Instant parsing handles the special "leap second" time of '23:59:60'. Leap seconds occur at '23:59:60' in the UTC time-zone, but at other local times in different time-zones. To avoid this potential ambiguity, the handling of leap-seconds is limited to
DateTimeFormatterBuilderJalali.appendInstant(), as that method always parses the instant with the UTC zone offset.If the time '23:59:60' is received, then a simple conversion is applied, replacing the second-of-minute of 60 with 59. This query can be used on the parse result to determine if the leap-second adjustment was made. The query will return
trueif it did adjust to remove the leap-second, andfalseif not. Note that applying a leap-second smoothing mechanism, such as UTC-SLS, is the responsibility of the application, as follows:TemporalAccessor parsed = formatter.parse(str); Instant instant = parsed.query(Instant::from); if (parsed.query(DateTimeFormatter.parsedLeapSecond())) { // validate leap-second is correct and apply correct smoothing }- Returns:
- a query that provides access to whether a leap-second was parsed
-
getDayOfWeek
-
getAmPm
-
getMonthOfYear
-
getLocale
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
-
withLocale
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.
- Parameters:
locale- the new locale, not null- Returns:
- a formatter based on this formatter with the requested locale, not null
-
getDecimalStyle
public org.bardframework.time.format.DecimalStyle getDecimalStyle()Gets the DecimalStyle to be used during formatting.- Returns:
- the locale of this formatter, not null
-
withDecimalStyle
public DateTimeFormatterJalali withDecimalStyle(org.bardframework.time.format.DecimalStyle decimalStyle) Returns a copy of this formatter with a new DecimalStyle.This instance is immutable and unaffected by this method call.
- Parameters:
decimalStyle- the new DecimalStyle, not null- Returns:
- a formatter based on this formatter with the requested DecimalStyle, not null
-
getChronology
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 override chronology of this formatter, null if no override
-
withChronology
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 formatted or parsed will be affected.
When formatting, if the temporal object contains a date, then it will be converted to a date in the override chronology. Whether the temporal contains a date is determined by querying the
EPOCH_DAYfield. Any time or zone will be retained unaltered unless overridden.If the temporal object does not contain a date, but does contain one or more
ChronoFielddate fields, then aDateTimeExceptionis thrown. In all other cases, the override chronology is added to the temporal, replacing any previous chronology, but without changing the date/time.When parsing, there are two distinct cases to consider. If a chronology has been parsed directly from the text, perhaps because
DateTimeFormatterBuilderJalali.appendChronologyId()was used, then this override chronology has no effect. If no zone has been parsed, then this override chronology will be used to interpret theChronoFieldvalues into a date according to the date resolving rules of the chronology.This instance is immutable and unaffected by this method call.
- Parameters:
chrono- the new chronology, null if no override- Returns:
- a formatter based on this formatter with the requested override chronology, not null
-
getZone
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 override zone of this formatter, null if no override
-
withZone
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 formatted or parsed will be affected.
When formatting, if the temporal object contains an instant, then it will be converted to a zoned date-time using the override zone. Whether the temporal is an instant is determined by querying the
INSTANT_SECONDSfield. If the input has a chronology then it will be retained unless overridden. If the input does not have a chronology, such asInstant, then the ISO chronology will be used.If the temporal object does not contain an instant, but does contain an offset then an additional check is made. If the normalized override zone is an offset that differs from the offset of the temporal, then a
DateTimeExceptionis thrown. In all other cases, the override zone is added to the temporal, replacing any previous zone, but without changing the date/time.When parsing, there are two distinct cases to consider. If a zone has been parsed directly from the text, perhaps because
DateTimeFormatterBuilderJalali.appendZoneId()was used, then this override zone has no effect. If no zone has been parsed, then this override zone will be included in the result of the parse where it can be used to build instants and date-times.This instance is immutable and unaffected by this method call.
- Parameters:
zone- the new override zone, null if no override- Returns:
- a formatter based on this formatter with the requested override zone, not null
-
getResolverStyle
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
SMARTresolver style.- Returns:
- the resolver style of this formatter, not null
-
format
Formats a date-time object using this formatter.This formats the date-time to a String using the rules of the formatter.
- Parameters:
temporal- the temporal object to format, not null- Returns:
- the formatted string, not null
- Throws:
DateTimeException- if an error occurs during formatting
-
formatTo
Formats a date-time object to anAppendableusing this formatter.This outputs the formatted date-time to the specified destination.
Appendableis a general purpose interface that is implemented by all key character output classes includingStringBuffer,StringBuilder,PrintStreamandWriter.Although
Appendablemethods throw anIOException, this method does not. Instead, anyIOExceptionis wrapped in a runtime exception.- Parameters:
temporal- the temporal object to format, not nullappendable- the appendable to format to, not null- Throws:
DateTimeException- if an error occurs during formatting
-
parse
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 isTemporalAccessorwhich 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.
- Parameters:
text- the text to parse, not null- Returns:
- the parsed temporal object, not null
- Throws:
DateTimeParseException- if unable to parse the requested result
-
parse
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
TemporalAccessorwhich 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, theParsePositionwill be updated with the index at the end of parsing.The operation of this method is slightly different to similar methods using
ParsePositiononjava.text.Format. That class will return errors using the error index on theParsePosition. By contrast, this method will throw aDateTimeParseExceptionif 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.
- Parameters:
text- the text to parse, not nullposition- the position to parse from, updated with length parsed and the index of any error, not null- Returns:
- the parsed temporal object, not null
- Throws:
DateTimeParseException- if unable to parse the requested resultIndexOutOfBoundsException- if the position is invalid
-
parse
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. The query is typically a method reference to a
from(TemporalAccessor)method. 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.- Type Parameters:
T- the type of the parsed date-time- Parameters:
text- the text to parse, not nullquery- the query defining the type to parse to, not null- Returns:
- the parsed date-time, not null
- Throws:
DateTimeParseException- if unable to parse the requested result
-
toString
Returns a description of the underlying formatters.
-