Most visited

Recently visited

Added in API level 24

MeasureFormat

public class MeasureFormat
extends UFormat

java.lang.Object
   ↳ java.text.Format
     ↳ android.icu.text.UFormat
       ↳ android.icu.text.MeasureFormat


A formatter for Measure objects.

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.

See also:

Summary

Nested classes

enum MeasureFormat.FormatWidth

Formatting width enum. 

Public methods

final boolean equals(Object other)

Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.

StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)

Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures.

StringBuilder formatMeasures(StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures)

Formats a sequence of measures.

final String formatMeasures(Measure... measures)

Format a sequence of measures.

static MeasureFormat getCurrencyFormat()

Return a formatter for CurrencyAmount objects in the default FORMAT locale.

static MeasureFormat getCurrencyFormat(Locale locale)

Return a formatter for CurrencyAmount objects in the given Locale.

static MeasureFormat getCurrencyFormat(ULocale locale)

Return a formatter for CurrencyAmount objects in the given locale.

static MeasureFormat getInstance(Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)

Create a format from the Locale, formatWidth, and format.

static MeasureFormat getInstance(Locale locale, MeasureFormat.FormatWidth formatWidth)

Create a format from the Locale and formatWidth.

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.

final ULocale getLocale()

Get the locale of this instance.

NumberFormat getNumberFormat()

Get a copy of the number format.

MeasureFormat.FormatWidth getWidth()

Get the format width this instance is using.

final int hashCode()

Returns a hash code value for the object.

Inherited methods

From class java.text.Format
From class java.lang.Object

Public methods

equals

Added in API level 24
boolean equals (Object other)

Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.

Parameters
other Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

format

Added in API level 24
StringBuffer format (Object obj, 
                StringBuffer toAppendTo, 
                FieldPosition pos)

Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures. If the pos argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields. Calling a formatMeasures method is preferred over calling this method as they give better performance.

Parameters
obj Object: must be a Collection<? extends Measure>, Measure[], or Measure object.
toAppendTo StringBuffer: Formatted string appended here.
pos FieldPosition: Identifies a field in the formatted text.
Returns
StringBuffer the string buffer passed in as toAppendTo, with formatted text appended

See also:

formatMeasures

Added in API level 24
StringBuilder formatMeasures (StringBuilder appendTo, 
                FieldPosition fieldPosition, 
                Measure... measures)

Formats a sequence of measures. If the fieldPosition argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields.

Parameters
appendTo StringBuilder: the formatted string appended here.
fieldPosition FieldPosition: Identifies a field in the formatted text.
measures Measure: the measures to format.
Returns
StringBuilder appendTo.

See also:

formatMeasures

Added in API level 24
String formatMeasures (Measure... measures)

Format a sequence of measures. Uses the ListFormatter unit lists. So, for example, one could format “3 feet, 2 inches”. Zero values are formatted (eg, “3 feet, 0 inches”). It is the caller’s responsibility to have the appropriate values in appropriate order, and using the appropriate Number values. Typically the units should be in descending order, with all but the last Measure having integer values (eg, not “3.2 feet, 2 inches”).

Parameters
measures Measure: a sequence of one or more measures.
Returns
String the formatted string.

getCurrencyFormat

Added in API level 24
MeasureFormat getCurrencyFormat ()

Return a formatter for CurrencyAmount objects in the default FORMAT locale.

Returns
MeasureFormat a formatter object

See also:

getCurrencyFormat

Added in API level 24
MeasureFormat getCurrencyFormat (Locale locale)

Return a formatter for CurrencyAmount objects in the given Locale.

Parameters
locale Locale: desired Locale
Returns
MeasureFormat a formatter object

getCurrencyFormat

Added in API level 24
MeasureFormat getCurrencyFormat (ULocale locale)

Return a formatter for CurrencyAmount objects in the given locale.

Parameters
locale ULocale: desired locale
Returns
MeasureFormat a formatter object

getInstance

Added in API level 24
MeasureFormat getInstance (Locale locale, 
                MeasureFormat.FormatWidth formatWidth, 
                NumberFormat format)

Create a format from the Locale, formatWidth, and format.

Parameters
locale Locale: the Locale.
formatWidth MeasureFormat.FormatWidth: hints how long formatted strings should be.
format NumberFormat: This is defensively copied.
Returns
MeasureFormat The new MeasureFormat object.

getInstance

Added in API level 24
MeasureFormat getInstance (Locale locale, 
                MeasureFormat.FormatWidth formatWidth)

Create a format from the Locale and formatWidth.

Parameters
locale Locale: the Locale.
formatWidth MeasureFormat.FormatWidth: hints how long formatted strings should be.
Returns
MeasureFormat The new MeasureFormat object.

getInstance

Added in API level 24
MeasureFormat getInstance (ULocale locale, 
                MeasureFormat.FormatWidth formatWidth)

Create a format from the locale, formatWidth, and format.

Parameters
locale ULocale: the locale.
formatWidth MeasureFormat.FormatWidth: hints how long formatted strings should be.
Returns
MeasureFormat The new MeasureFormat object.

getInstance

Added in API level 24
MeasureFormat getInstance (ULocale locale, 
                MeasureFormat.FormatWidth formatWidth, 
                NumberFormat format)

Create a format from the locale, formatWidth, and format.

Parameters
locale ULocale: the locale.
formatWidth MeasureFormat.FormatWidth: hints how long formatted strings should be.
format NumberFormat: This is defensively copied.
Returns
MeasureFormat The new MeasureFormat object.

getLocale

Added in API level 24
ULocale getLocale ()

Get the locale of this instance.

Returns
ULocale

getNumberFormat

Added in API level 24
NumberFormat getNumberFormat ()

Get a copy of the number format.

Returns
NumberFormat

getWidth

Added in API level 24
MeasureFormat.FormatWidth getWidth ()

Get the format width this instance is using.

Returns
MeasureFormat.FormatWidth

hashCode

Added in API level 24
int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Returns
int a hash code value for this object.

Hooray!