FieldPosition objects are optionally passed to the format( ) methods of the Format class and its subclasses to return information about the start and end positions of a specific part or “field” of the formatted string. This kind of information is often useful for aligning formatted strings in columns—for example, aligning the decimal points in a column of numbers.

The field of interest is specified when the FieldPosition( ) constructor is called. The NumberFormat and DateFormat classes define integer various constants (which end with the string _FIELD) that can be used here. In Java 1.4 and later you can also construct a FieldPosition by specifying the Format.Field object that identifies the field. (For constant Field instances, see DateFormat.Field, MessageFormat.Field and NumberFormat.Field.)

After a FieldPosition has been created and passed to a format( ) method, use getBeginIndex( ) and getEndIndex( ) methods of this class to obtain the starting and ending character positions of the desired field of the formatted string.

public class FieldPosition {
// Public Constructors
                  1.4  public FieldPosition(Format.Field attribute);  
     public FieldPosition(int field);  
1.4  public FieldPosition(Format.Field attribute, int fieldID);  
// Public Instance Methods
     public int getBeginIndex( );  
     public int getEndIndex( );  
     public int getField( );  
1.4  public Format.Field getFieldAttribute( );  
1.2  public void setBeginIndex(int bi);  
1.2  public void setEndIndex(int ei);  
// Public ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.