com.ibm.icu.text
Class TimeUnitFormat

java.lang.Object
  extended by java.text.Format
      extended by com.ibm.icu.text.UFormat
          extended by com.ibm.icu.text.MeasureFormat
              extended by com.ibm.icu.text.TimeUnitFormat
All Implemented Interfaces:
Serializable, Cloneable

public class TimeUnitFormat
extends MeasureFormat

Format or parse a TimeUnitAmount, using plural rules for the units where available.

Code Sample:

   // create a time unit instance.
   // only SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, and YEAR are supported
   TimeUnit timeUnit = TimeUnit.SECOND;
   // create time unit amount instance - a combination of Number and time unit
   TimeUnitAmount source = new TimeUnitAmount(2, timeUnit);
   // create time unit format instance
   TimeUnitFormat format = new TimeUnitFormat();
   // set the locale of time unit format
   format.setLocale(new ULocale("en"));
   // format a time unit amount
   String formatted = format.format(source);
   System.out.println(formatted);
   try {
       // parse a string into time unit amount
       TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
       // result should equal to source 
   } catch (ParseException e) {
   }
 

Author:
markdavis
See Also:
TimeUnitAmount, TimeUnitFormat, Serialized Form
Status:
Stable ICU 4.0.

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Field Summary
static int ABBREVIATED_NAME
          Constant for abbreviated name style format.
static int FULL_NAME
          Constant for full name style format.
 
Constructor Summary
TimeUnitFormat()
          Create empty format using full name style, for example, "hours".
TimeUnitFormat(Locale locale)
          Create TimeUnitFormat given a Locale, and using full name style.
TimeUnitFormat(Locale locale, int style)
          Create TimeUnitFormat given a Locale and a formatting style: full or abbreviated.
TimeUnitFormat(ULocale locale)
          Create TimeUnitFormat given a ULocale, and using full name style.
TimeUnitFormat(ULocale locale, int style)
          Create TimeUnitFormat given a ULocale and a formatting style: full or abbreviated.
 
Method Summary
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
          Format a TimeUnitAmount.
 Object parseObject(String source, ParsePosition pos)
          Parse a TimeUnitAmount.
 TimeUnitFormat setLocale(Locale locale)
          Set the locale used for formatting or parsing.
 TimeUnitFormat setLocale(ULocale locale)
          Set the locale used for formatting or parsing.
 TimeUnitFormat setNumberFormat(NumberFormat format)
          Set the format used for formatting or parsing.
 
Methods inherited from class com.ibm.icu.text.MeasureFormat
getCurrencyFormat, getCurrencyFormat
 
Methods inherited from class com.ibm.icu.text.UFormat
getLocale
 
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
 

Field Detail

FULL_NAME

public static final int FULL_NAME
Constant for full name style format. For example, the full name for "hour" in English is "hour" or "hours".

See Also:
Constant Field Values
Status:
Stable ICU 4.2.

ABBREVIATED_NAME

public static final int ABBREVIATED_NAME
Constant for abbreviated name style format. For example, the abbreviated name for "hour" in English is "hr" or "hrs".

See Also:
Constant Field Values
Status:
Stable ICU 4.2.
Constructor Detail

TimeUnitFormat

public TimeUnitFormat()
Create empty format using full name style, for example, "hours". Use setLocale and/or setFormat to modify.

Status:
Stable ICU 4.0.

TimeUnitFormat

public TimeUnitFormat(ULocale locale)
Create TimeUnitFormat given a ULocale, and using full name style.

Parameters:
locale - locale of this time unit formatter.
Status:
Stable ICU 4.0.

TimeUnitFormat

public TimeUnitFormat(Locale locale)
Create TimeUnitFormat given a Locale, and using full name style.

Parameters:
locale - locale of this time unit formatter.
Status:
Stable ICU 4.0.

TimeUnitFormat

public TimeUnitFormat(ULocale locale,
                      int style)
Create TimeUnitFormat given a ULocale and a formatting style: full or abbreviated.

Parameters:
locale - locale of this time unit formatter.
style - format style, either FULL_NAME or ABBREVIATED_NAME style.
Throws:
IllegalArgumentException - if the style is not FULL_NAME or ABBREVIATED_NAME style.
Status:
Stable ICU 4.2.

TimeUnitFormat

public TimeUnitFormat(Locale locale,
                      int style)
Create TimeUnitFormat given a Locale and a formatting style: full or abbreviated.

Status:
Stable ICU 4.2.
Method Detail

setLocale

public TimeUnitFormat setLocale(ULocale locale)
Set the locale used for formatting or parsing.

Parameters:
locale - locale of this time unit formatter.
Returns:
this, for chaining.
Status:
Stable ICU 4.0.

setLocale

public TimeUnitFormat setLocale(Locale locale)
Set the locale used for formatting or parsing.

Parameters:
locale - locale of this time unit formatter.
Returns:
this, for chaining.
Status:
Stable ICU 4.0.

setNumberFormat

public TimeUnitFormat setNumberFormat(NumberFormat format)
Set the format used for formatting or parsing. If null or not available, use the getNumberInstance(locale).

Parameters:
format - the number formatter.
Returns:
this, for chaining.
Status:
Stable ICU 4.0.

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Format a TimeUnitAmount.

Specified by:
format in class Format
See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
Status:
Stable ICU 4.0.

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Parse a TimeUnitAmount.

Specified by:
parseObject in class Format
See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)
Status:
Stable ICU 4.0.


Copyright (c) 2011 IBM Corporation and others.