public class TimeUnitFormat extends MeasureFormat
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) {
}
TimeUnitAmount,
TimeUnitFormat,
Serialized FormFormat.Field| Modifier and Type | Field and Description |
|---|---|
static int |
ABBREVIATED_NAME
Constant for abbreviated name style format.
|
static int |
FULL_NAME
Constant for full name style format.
|
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
getCurrencyFormat, getCurrencyFormatclone, format, formatToCharacterIterator, parseObjectpublic static final int FULL_NAME
public static final int ABBREVIATED_NAME
public TimeUnitFormat()
public TimeUnitFormat(ULocale locale)
locale - locale of this time unit formatter.public TimeUnitFormat(Locale locale)
locale - locale of this time unit formatter.public TimeUnitFormat(ULocale locale, int style)
locale - locale of this time unit formatter.style - format style, either FULL_NAME or ABBREVIATED_NAME style.IllegalArgumentException - if the style is not FULL_NAME or
ABBREVIATED_NAME style.public TimeUnitFormat(Locale locale, int style)
public TimeUnitFormat setLocale(ULocale locale)
locale - locale of this time unit formatter.public TimeUnitFormat setLocale(Locale locale)
locale - locale of this time unit formatter.public TimeUnitFormat setNumberFormat(NumberFormat format)
format - the number formatter.public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
format in class FormatFormat.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)public Object parseObject(String source, ParsePosition pos)
parseObject in class FormatFormat.parseObject(java.lang.String, java.text.ParsePosition)Copyright (c) 2012 IBM Corporation and others.