U - generic type of time unitspublic static final class Duration.Formatter<U extends IsoUnit>
extends java.lang.Object
Non-localized and user-defined format for durations based on a pattern containing some standard symbols and literals.
Note: For storing purposes users should normally use the canonical
or ISO- or XML-representation of a duration, not this custom format.
Otherwise, if users want a localized output then the class
PrettyTime is usually the best choice. This class is mainly
designed for handling non-standardized formats.
First example (parsing a Joda-Time-Period using a max width of 2):
Duration.Formatter<IsoUnit> f = Duration.Formatter.ofJodaStyle();
Duration<?> dur = f.parse("P-2Y-15DT-30H-5M");
System.out.println(dur); // output: -P2Y15DT30H5M
Second example (printing a wall-time-like duration):
Duration.Formatter<ClockUnit> f =
Duration.Formatter.ofPattern(ClockUnit.class, "+hh:mm:ss");
String s = f.print(Duration.ofClockUnits(27, 30, 5));
System.out.println(s); // output: +27:30:05
Duration.toString(),
Duration.parsePeriod(String),
ofPattern(Class, String)| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
format(Duration<?> duration)
Creates a textual output of given duration.
|
java.lang.String |
getPattern()
Yields the underlying format pattern.
|
java.lang.Class<U> |
getType()
Yields the associated reified unit type.
|
static Duration.Formatter<IsoUnit> |
ofJodaStyle()
Handles Joda-Time-style-patterns which in general follow XML-schema
- with the exception of sign handling.
|
static <U extends IsoUnit> |
ofPattern(java.lang.Class<U> type,
java.lang.String pattern)
Constructs a new instance of duration formatter.
|
static Duration.Formatter<IsoUnit> |
ofPattern(java.lang.String pattern)
Equivalent to
ofPattern(IsoUnit.class, pattern). |
Duration<U> |
parse(java.lang.CharSequence text)
Equivalent to
parse(text, 0). |
Duration<U> |
parse(java.lang.CharSequence text,
int offset)
Analyzes given text according to format pattern and parses the
text to a duration.
|
void |
print(Duration<?> duration,
java.lang.Appendable buffer)
Creates a textual output of given duration and writes to
the buffer.
|
public static Duration.Formatter<IsoUnit> ofJodaStyle()
Handles Joda-Time-style-patterns which in general follow XML-schema - with the exception of sign handling.
The sign handling of Joda-Time allows and even enforces in contrast to XML-schema negative signs not before the P-symbol but for every single duration item repeatedly. Warning: Mixed signs are never supported by Time4J.
ofPattern(Class, String)public static Duration.Formatter<IsoUnit> ofPattern(java.lang.String pattern)
Equivalent to ofPattern(IsoUnit.class, pattern).
pattern - format patternofPattern(Class, String)public static <U extends IsoUnit> Duration.Formatter<U> ofPattern(java.lang.Class<U> type, java.lang.String pattern)
Constructs a new instance of duration formatter.
Uses a pattern with symbols as followed:
| Symbol | Description |
|---|---|
| + | sign of duration, printing + or - |
| - | sign of duration, printing only - |
| I | CalendarUnit.MILLENNIA |
| C | CalendarUnit.CENTURIES |
| E | CalendarUnit.DECADES |
| Y | CalendarUnit.YEARS |
| Q | CalendarUnit.QUARTERS |
| M | CalendarUnit.MONTHS |
| W | CalendarUnit.WEEKS |
| D | CalendarUnit.DAYS |
| h | ClockUnit.HOURS |
| m | ClockUnit.MINUTES |
| s | ClockUnit.SECONDS |
| , | decimal separator, comma is preferred |
| . | decimal separator, dot is preferred |
| f | ClockUnit.NANOS as fraction, (1-9) chars |
| ' | apostroph, for escaping literal chars |
| [] | optional section |
| {} | section with plural forms, since v2.0 |
| # | placeholder for an optional digit, since v3.0 |
All letters in range a-z and A-Z are always reserved chars and must be escaped by apostrophes for interpretation as literals. If such a letter is repeated then the count of symbols controls the minimum width for formatted output. Such a minimum width also reserves this area for parsing of any preceding item. If necessary a number (of units) will be padded from left with the zero digit. The unit symbol (with exception of "f") can be preceded by any count of char "#" (>= 0). The sum of min width and count of #-chars define the maximum width for formatted output and parsing.
Optional sections let the parser be error-tolerant and continue with the next section in case of errors. Since v2.3: During printing, an optional section will only be printed if there is any non-zero part.
Enhancement since version v2.0: plural forms
Every expression inside curly brackets represents a combination of amount, separator and pluralized unit name and has following syntax:
{[symbol]:[separator]:[locale]:[CATEGORY=LITERAL][:...]}
The symbol is one of following chars: I, C, E, Y, Q, M, W, D, h, m, s, f (legend see table above)
Afterwards the definition of separator chars follows. The
empty separator (represented by zero space between colons) is
permitted, too. The next section denotes the locale necessary
for determination of suitable plural rules. The form
[language]-[country]-[variant] can be used, for example
"en-US" or "en_US". At least the language
must be present. The underscore is an acceptable alternative
for the minus-sign. Finally there must be a sequence of
name-value-pairs in the form CATEGORY=LITERAL. Every category
label must be the name of a plural category.
The category OTHER must exist. Example:
Duration.Formatter<CalendarUnit> formatter =
Duration.Formatter.ofPattern(
CalendarUnit.class,
"{D: :en:ONE=day:OTHER=days}");
String s = formatter.format(Duration.of(3, DAYS));
System.out.println(s); // output: 3 days
Enhancement since version v3.0: numerical placeholders
Before version v3.0, the maximum numerical width was always 18. Now it is the sum of min width and the count of preceding #-chars. Example:
Duration.Formatter<CalendarUnit> formatter1 =
Duration.Formatter.ofPattern(CalendarUnit.class, "D");
formatter1.format(Duration.of(123, DAYS)); throws IllegalArgumentException
Duration.Formatter<CalendarUnit> formatter2 =
Duration.Formatter.ofPattern(CalendarUnit.class, "##D");
String s = formatter2.format(Duration.of(123, DAYS));
System.out.println(s); // output: 123
U - generic unit typetype - reified unit typepattern - format patternpublic java.lang.String getPattern()
Yields the underlying format pattern.
public java.lang.Class<U> getType()
Yields the associated reified unit type.
public java.lang.String format(Duration<?> duration)
Creates a textual output of given duration.
duration - duration objectjava.lang.IllegalArgumentException - if some aspects of duration
prevents printing (for example too many nanoseconds)public void print(Duration<?> duration, java.lang.Appendable buffer) throws java.io.IOException
Creates a textual output of given duration and writes to the buffer.
duration - duration objectbuffer - I/O-buffer where the result is written tojava.lang.IllegalArgumentException - if some aspects of duration
prevents printing (for example too many nanoseconds)java.io.IOException - if writing into buffer failspublic Duration<U> parse(java.lang.CharSequence text) throws java.text.ParseException
Equivalent to parse(text, 0).
text - custom textual representation to be parsedjava.text.ParseException - (for example in case of mixed signs)parse(CharSequence, int)public Duration<U> parse(java.lang.CharSequence text, int offset) throws java.text.ParseException
Analyzes given text according to format pattern and parses the text to a duration.
text - custom textual representation to be parsedoffset - start position for the parserjava.text.ParseException - (for example in case of mixed signs)