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 Form| 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(java.util.Locale locale)
Create TimeUnitFormat given a Locale, and using full name style.
|
TimeUnitFormat(java.util.Locale locale,
int style)
Create TimeUnitFormat given a Locale and a formatting style.
|
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.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.StringBuffer |
format(java.lang.Object obj,
java.lang.StringBuffer toAppendTo,
java.text.FieldPosition pos)
Format a TimeUnitAmount.
|
java.lang.Object |
parseObject(java.lang.String source,
java.text.ParsePosition pos)
Parse a TimeUnitAmount.
|
TimeUnitFormat |
setLocale(java.util.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, getCurrencyFormatpublic 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(java.util.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.java.lang.IllegalArgumentException - if the style is not FULL_NAME or
ABBREVIATED_NAME style.public TimeUnitFormat(java.util.Locale locale,
int style)
public TimeUnitFormat setLocale(ULocale locale)
locale - locale of this time unit formatter.public TimeUnitFormat setLocale(java.util.Locale locale)
locale - locale of this time unit formatter.public TimeUnitFormat setNumberFormat(NumberFormat format)
NumberFormat.getNumberInstance(ULocale).format - the number formatter.public java.lang.StringBuffer format(java.lang.Object obj,
java.lang.StringBuffer toAppendTo,
java.text.FieldPosition pos)
format in class java.text.FormatFormat.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)public java.lang.Object parseObject(java.lang.String source,
java.text.ParsePosition pos)
parseObject in class java.text.FormatFormat.parseObject(java.lang.String, java.text.ParsePosition)Copyright (c) 2013 IBM Corporation and others.