public class MeasureFormat extends UFormat
To format a Measure object, first create a formatter object using a MeasureFormat factory method. Then use that object's format or formatMeasures methods. Here is sample code:
MeasureFormat fmtFr = MeasureFormat.getInstance( ULocale.FRENCH, FormatWidth.SHORT); Measure measure = new Measure(23, MeasureUnit.CELSIUS); // Output: 23 °C System.out.println(fmtFr.format(measure)); Measure measureF = new Measure(70, MeasureUnit.FAHRENHEIT); // Output: 70 °F System.out.println(fmtFr.format(measureF)); MeasureFormat fmtFrFull = MeasureFormat.getInstance( ULocale.FRENCH, FormatWidth.WIDE); // Output: 70 pieds et 5,3 pouces System.out.println(fmtFrFull.formatMeasures( new Measure(70, MeasureUnit.FOOT), new Measure(5.3, MeasureUnit.INCH))); // Output: 1 pied et 1 pouce System.out.println(fmtFrFull.formatMeasures( new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH))); MeasureFormat fmtFrNarrow = MeasureFormat.getInstance( ULocale.FRENCH, FormatWidth.NARROW); // Output: 1′ 1″ System.out.println(fmtFrNarrow.formatMeasures( new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH))); MeasureFormat fmtEn = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE); // Output: 1 inch, 2 feet fmtEn.formatMeasures( new Measure(1, MeasureUnit.INCH), new Measure(2, MeasureUnit.FOOT));
This class does not do conversions from one unit to another. It simply formats whatever units it is given
This class is immutable and thread-safe so long as its deprecated subclass, TimeUnitFormat, is never used. TimeUnitFormat is not thread-safe, and is mutable. Although this class has existing subclasses, this class does not support new sub-classes.
UFormat
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
MeasureFormat.FormatWidth
Formatting width enum.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other)
Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth,
locale, and equal number formats.
|
java.lang.StringBuffer |
format(java.lang.Object obj,
java.lang.StringBuffer toAppendTo,
java.text.FieldPosition pos)
Able to format Collection<?
|
java.lang.String |
formatMeasureRange(Measure lowValue,
Measure highValue)
Deprecated.
This API is ICU internal only.
|
java.lang.String |
formatMeasures(Measure... measures)
Format a sequence of measures.
|
java.lang.StringBuilder |
formatMeasures(java.lang.StringBuilder appendTo,
java.text.FieldPosition fieldPosition,
Measure... measures)
Formats a sequence of measures.
|
java.lang.StringBuilder |
formatMeasuresPer(java.lang.StringBuilder appendTo,
java.text.FieldPosition fieldPosition,
MeasureUnit perUnit,
Measure... measures)
Deprecated.
This API is ICU internal only.
|
static MeasureFormat |
getCurrencyFormat()
Return a formatter for CurrencyAmount objects in the default
FORMAT locale. |
static MeasureFormat |
getCurrencyFormat(java.util.Locale locale)
Return a formatter for CurrencyAmount objects in the given
JDK locale.
|
static MeasureFormat |
getCurrencyFormat(ULocale locale)
Return a formatter for CurrencyAmount objects in the given
locale.
|
static MeasureFormat |
getInstance(java.util.Locale locale,
MeasureFormat.FormatWidth formatWidth)
Create a format from the JDK locale, formatWidth, and format.
|
static MeasureFormat |
getInstance(java.util.Locale locale,
MeasureFormat.FormatWidth formatWidth,
NumberFormat format)
Create a format from the JDK locale, formatWidth, and format.
|
static MeasureFormat |
getInstance(ULocale locale,
MeasureFormat.FormatWidth formatWidth)
Create a format from the locale, formatWidth, and format.
|
static MeasureFormat |
getInstance(ULocale locale,
MeasureFormat.FormatWidth formatWidth,
NumberFormat format)
Create a format from the locale, formatWidth, and format.
|
ULocale |
getLocale()
Get the locale of this instance.
|
NumberFormat |
getNumberFormat()
Get a copy of the number format.
|
static com.ibm.icu.impl.SimplePatternFormatter |
getRangeFormat(ULocale forLocale,
MeasureFormat.FormatWidth width)
Deprecated.
This API is ICU internal only.
|
static java.lang.String |
getRangePattern(ULocale forLocale,
MeasureFormat.FormatWidth width)
Deprecated.
This API is ICU internal only.
|
MeasureFormat.FormatWidth |
getWidth()
Get the format width this instance is using.
|
int |
hashCode() |
Measure |
parseObject(java.lang.String source,
java.text.ParsePosition pos)
Parses text from a string to produce a
Measure . |
public static MeasureFormat getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth)
locale
- the locale.formatWidth
- hints how long formatted strings should be.public static MeasureFormat getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth)
locale
- the JDK locale.formatWidth
- hints how long formatted strings should be.public static MeasureFormat getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
locale
- the locale.formatWidth
- hints how long formatted strings should be.format
- This is defensively copied.public static MeasureFormat getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
locale
- the JDK locale.formatWidth
- hints how long formatted strings should be.format
- This is defensively copied.public java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
formatMeasures
method is preferred over calling
this method as they give better performance.format
in class java.text.Format
obj
- must be a Collection extends Measure>, Measure[], or Measure object.toAppendTo
- Formatted string appended here.pos
- Identifies a field in the formatted text.Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
public Measure parseObject(java.lang.String source, java.text.ParsePosition pos)
Measure
.parseObject
in class java.text.Format
java.lang.UnsupportedOperationException
- Not supported.Format.parseObject(java.lang.String, java.text.ParsePosition)
public final java.lang.String formatMeasures(Measure... measures)
measures
- a sequence of one or more measures.@Deprecated public final java.lang.String formatMeasureRange(Measure lowValue, Measure highValue)
lowValue
- low value in rangehighValue
- high value in range@Deprecated public java.lang.StringBuilder formatMeasuresPer(java.lang.StringBuilder appendTo, java.text.FieldPosition fieldPosition, MeasureUnit perUnit, Measure... measures)
appendTo
- the formatted string appended here.fieldPosition
- Identifies a field in the formatted text.perUnit
- for the example above would be MeasureUnit.HOUR.measures
- the measures to format.public java.lang.StringBuilder formatMeasures(java.lang.StringBuilder appendTo, java.text.FieldPosition fieldPosition, Measure... measures)
appendTo
- the formatted string appended here.fieldPosition
- Identifies a field in the formatted text.measures
- the measures to format.formatMeasures(Measure...)
public final boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public final int hashCode()
hashCode
in class java.lang.Object
public MeasureFormat.FormatWidth getWidth()
public final ULocale getLocale()
public NumberFormat getNumberFormat()
public static MeasureFormat getCurrencyFormat(ULocale locale)
locale
- desired localepublic static MeasureFormat getCurrencyFormat(java.util.Locale locale)
locale
- desired JDK localepublic static MeasureFormat getCurrencyFormat()
FORMAT
locale.ULocale.Category.FORMAT
@Deprecated public static com.ibm.icu.impl.SimplePatternFormatter getRangeFormat(ULocale forLocale, MeasureFormat.FormatWidth width)
forLocale
- locale to get the format forwidth
- the format width@Deprecated public static java.lang.String getRangePattern(ULocale forLocale, MeasureFormat.FormatWidth width)
forLocale
- locale to get the range pattern forwidth
- the format width.Copyright (c) 2014 IBM Corporation and others.