net.sf.japaki.text
Class DurationFormat

java.lang.Object
  extended by java.text.Format
      extended by net.sf.japaki.text.DurationFormat
All Implemented Interfaces:
Serializable, Cloneable

public class DurationFormat
extends Format

DurationFormat is for formatting and parsing durations. It is inspired by SimpleDateFormat.

Duration Patterns

Duration formats are specified by time pattern strings. Within time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. Characters ',', '.' and '#' can be used to specify a decimal format for a component. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

Letter Time Component Presentation Examples
d Days Number 589
h Hour in day (0-23) Number 0
m Minute in hour Number 30
s Second in minute Number 55
t Decisecond Number 9
c centisecond Number 97
f Millisecond Number 978
To specify the format of an item, the specification of DecimalFormat is used. The first character must be the character of the item, p.e. "m". It is interpreted as "0", that is the digit is always printed. The item character can then be repeated to use more digits. It can also be interspersed by "#", "," and ".".

Examples

The following examples show how time patterns are interpreted in the U.S. locale. The given time is 589 days 12:08:56.235.
Time Pattern Result
"d hh:mm:ss.sss" 589 12:08:56.235
"m:ss.s" 848888:56.2
"d.dd' days'" 589,50 days
"h,hhh:mm" 14.148:08

Synchronization

Duration formats (as other formats) are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Constructor Summary
DurationFormat()
          Constructs a duration format with a default pattern.
DurationFormat(String pattern)
          Constructs a new duration format.
DurationFormat(String pattern, Locale locale)
          Constructs a new duration format.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
           
 Object parseObject(String source, ParsePosition pos)
           
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DurationFormat

public DurationFormat(String pattern,
                      Locale locale)
Constructs a new duration format.

Parameters:
pattern - pattern as described above
locale - the locale for this duration format
Throws:
NullPointerException - if the given pattern is null
IllegalArgumentException - if the given pattern is invalid
See Also:
SimpleDateFormat

DurationFormat

public DurationFormat(String pattern)
Constructs a new duration format.

Parameters:
pattern - pattern as described above
Throws:
NullPointerException - if the given pattern is null
IllegalArgumentException - if the given pattern is invalid
See Also:
SimpleDateFormat

DurationFormat

public DurationFormat()
Constructs a duration format with a default pattern.

Method Detail

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Specified by:
format in class Format

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Specified by:
parseObject in class Format