Formatting Data with Formatter

Methods in the Formatter Class

Method Summary
 void close()
          Closes this formatter.
 void flush()
          Flushes this formatter.
 Formatter format(Locale l, String format, Object... args)
          Writes a formatted string to this object's destination using the specified locale, format string, and arguments.
 Formatter format(String format, Object... args)
          Writes a formatted string to this object's destination using the specified format string and arguments.
 IOException ioException()
          Returns the IOException last thrown by this formatter's Appendable.
 Locale locale()
          Returns the locale set by the construction of this formatter.
 Appendable out()
          Returns the destination for the output.
 String toString()
          Returns the result of invoking toString() on the destination for the output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Using the Formatter Class

Common Format Specifiers
Format SpecifierConversion Applied
%a or %AFloating-point hexadecimal
%b or %BBoolean
%cCharacter
%dDecimal integer
%e or %EScientific notation
%fDecimal floating point
%nNewline
%s or %SString
%tTime and date
%x or %XInteger hexadecimal
%% Inserts a % sign

Specifying a Minimum Field Width

Specifying Precision

Format Specifiers for General, Character and Numeric types

Examples

// The '(' numeric flag may be used to format negative numbers with
   // parentheses rather than a minus sign.  Group separators are
   // automatically inserted.
   //                   %[argument_index$][flags][width][.precision]conversion 
   formatter.format("Amount gained or lost since last statement: $ %(,.2f",
                    balanceDelta);
   // -> "Amount gained or lost since last statement: $ (6,217.58)"
   
   
   StringBuilder sb = new StringBuilder();
   // Send all output to the Appendable object sb
   Formatter formatter = new Formatter(sb, Locale.US);
   //                   %[argument_index$][flags][width][.precision]conversion 
   // Explicit argument indices may be used to re-order output.
   formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
   // -> " d  c  b  a"

Flags

The following table summarizes the supported flags. y means the flag is supported for the indicated argument types.

Flag General Character Integral Floating Point Date/Time Description
'-' y y y y y The result will be left-justified.
'#' y1 - y3 y - The result should use a conversion-dependent alternate form
'+' - - y4 y - The result will always include a sign
'  ' - - y4 y - The result will include a leading space for positive values
'0' - - y y - The result will be zero-padded
',' - - y2 y5 - The result will include locale-specific grouping separators
'(' - - y4 y5 - The result will enclose negative numbers in parentheses

Herb Schildt's Demo Code for Formatter

listing 1
// Use Formatter to: 
// 
//   . Specify the number of decimal digits. 
//   . Use a group separator. 
//   . Precede a positive value with a + sign. 
//   . Show negative values within parentheses.  
 
import java.util.*; 
 
class NumericFormats { 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    // Limit the number of decimal digits 
    // by specifying the precision. 
    fmt.format("Default precision: %f\n", 10.0/3.0); 
    fmt.format("Two decimal digits: %.2f\n\n", 10.0/3.0); 
 
    // Using group separators. 
    fmt.format("No group separators: %d\n", 123456789); 
    fmt.format("With group separators: %,d\n\n", 123456789); 
 
    // Show positive values with a leading + 
    // and negative values within parentheses. 
    fmt.format("Default positive and negative format: %.2f %.2f\n", 
               423.78, -505.09); 
    
    fmt.format("With + and parentheses: %+.2f %(.2f\n", 
               423.78, -505.09); 
 
    // Displaya the formatted output. 
    System.out.println(fmt); 
  } 
}

listing 2
// Use Formatter to vertically align numeric values. 
 
import java.util.*; 
 
class AlignVertical { 
  public static void main(String args[]) { 
    double data[] = { 12.3, 45.5764, -0.09, -18.0, 1232.01 }; 
   
    Formatter fmt = new Formatter(); 
 
    // Create a table that contains values and the 
    // the cube roots of those values. 
    fmt.format("%12s %12s\n", "Value", "Cube Root"); 
 
    for(double v : data) { 
      fmt.format("%12.4f %12.4f\n", v, Math.cbrt(v)); 
    }  
 
    // Display the formatted data. 
    System.out.println(fmt); 
  } 
}

listing 3
// Center data within a field. 
 
import java.util.*; 
 
class CenterDemo { 
 
  // Center data within a specified field width. 
  // The format of the data is passed to fmtStr, 
  // the Formatter is passed to fmt, the data to format 
  // is passed to obj, and the field width is passed to width. 
  static void center(String fmtStr, Formatter fmt, 
                     Object obj, int width) { 
 
    String str; 
    try { 
      // First, format the data so that its length can 
      // be determined. Use a temporary Formatter for 
      // this purpose. 
      Formatter tmp = new Formatter(); 
      tmp.format(fmtStr, obj); 
      str = tmp.toString(); 
    } catch(IllegalFormatException exc) { 
      System.out.println("Invalid Format Request"); 
      fmt.format(""); 
      return; 
    } 
 
    // Obtain the difference between the length of the 
    // data and the length of the field. 
    int dif = width - str.length(); 
 
    // If data is longer than the field width, then just 
    // use it as-is. 
    if(dif < 0) { 
      fmt.format(str); 
      return; 
    } 
 
    // Add padding to the start of the field. 
    char[] pad = new char[dif/2]; 
    Arrays.fill(pad, ' ');  
    fmt.format(new String(pad)); 
 
    // Add the data. 
    fmt.format(str); 
 
    // Add padding to the end of the field.  
    pad = new char[width-dif/2-str.length()]; 
    Arrays.fill(pad, ' ');  
    fmt.format(new String(pad)); 
  } 
 
  // Demonstrate center(). 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    fmt.format("|"); 
    center("%s", fmt, "Source", 12); 
    fmt.format("|"); 
    center("%10s", fmt, "Profit/Loss", 14); 
    fmt.format("|\n\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Retail", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 1232675, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Wholesale", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 23232482, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Rents", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 3052238, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Royalties", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 329845, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Interest", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 8657, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Investments", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, 1675832, 14); 
    fmt.format("|\n"); 
 
    fmt.format("|"); 
    center("%s", fmt, "Patents", 12); 
    fmt.format("|"); 
    center("%,10d", fmt, -2011, 14); 
    fmt.format("|\n"); 
 
    // Display the formatted data. 
    System.out.println(fmt); 
  } 
}

listing 4
// Use Formatter to left-justify strings within a table. 
 
import java.util.*; 
 
class Table { 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    fmt.format("%-12s %12s\n\n", "Source", "Profit/Loss"); 
 
    fmt.format("%-12s %,12d\n", "Retail", 1232675); 
    fmt.format("%-12s %,12d\n", "Wholesale", 23232482); 
    fmt.format("%-12s %,12d\n", "Rents", 3052238); 
    fmt.format("%-12s %,12d\n", "Royalties", 329845); 
    fmt.format("%-12s %,12d\n", "Interest", 8657); 
    fmt.format("%-12s %,12d\n", "Investments", 1675832); 
    fmt.format("%-12s %,12d\n", "Patents", -2011); 
 
    // Display the formatted table. 
    System.out.println(fmt); 
  } 
}

listing 5
// Display several time and date formats 
// using the %t specifier with Formatter. 
 
import java.util.*; 
 
class TimeAndDate { 
 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    // Get the current time and date. 
    Calendar cal = Calendar.getInstance(); 
 
    // Display 12-hour time format. 
    fmt.format("Time using 12-hour clock: %tr\n", cal); 
 
    // Display 24-hour time format. 
    fmt.format("Time using 24-hour clock: %tT\n", cal); 
 
    // Display short date format. 
    fmt.format("Short date format: %tD\n", cal); 
 
    // Display date using full names. 
    fmt.format("Long date format: "); 
    fmt.format("%tA %1$tB %1$td, %1$tY\n", cal); 
 
    // Display complete time and date information. 
    // The first version uses lowercase. 
    // The second verison uses uppercase. 
    // As explained, uppercase is selected by 
    // using %T rather than %t. 
    fmt.format("Time and date in lowercase: %tc\n", cal); 
    fmt.format("Time and date in uppercase: %Tc\n", cal); 
 
    // Display hour and minute, and include 
    // AM or PM indicator. Note use of uppercase %T. 
    // This causes AM or PM to be in uppercase. 
    fmt.format("Hour and Minute: %tl:%1$tM %1$Tp\n", cal); 
 
    // Display the formatted times and dates. 
    System.out.println(fmt); 
  } 
}

listing 6
// Demonstrate locale-specific formatting. 
 
import java.util.*; 
 
class LocaleFormatDemo { 
 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    // Get the current time and date. 
    Calendar cal = Calendar.getInstance(); 
 
    // Display complete time and date information 
    // for various locales. 
    fmt = new Formatter(); 
    fmt.format("Default locale: %tc\n", cal); 
 
    fmt.format(Locale.GERMAN, "For Locale.GERMAN: %tc\n", cal); 
 
    fmt.format(Locale.ITALY, "For Locale.ITALY: %tc\n", cal); 
 
    fmt.format(Locale.FRANCE, "For Locale.FRANCE: %tc\n", cal); 
 
    System.out.println(fmt); 
  } 
}

listing 7
// Write formatted output directly to the console 
// and to a file. 
 
import java.io.*; 
 
import java.util.*; 
 
class FormatterStreams { 
  public static void main(String args[]) { 
    // Create a Formatter linked to the console. 
    Formatter fmtCon = new Formatter(System.out); 
 
    // Create a Formatted linked to a file. 
    Formatter fmtFile; 
    try { 
     fmtFile = new Formatter(new FileOutputStream("test.fmt")); 
    } catch(FileNotFoundException exc) { 
      System.out.println("Cannot Open File"); 
      return; 
    } 
 
    // First, write to the console. 
    fmtCon.format("This is a negative number: %(.2f\n\n", 
                  -123.34); 
 
    fmtCon.format("%8s %8s\n", "Value", "Square"); 
 
    for(int i=1; i < 20; i++) 
      fmtCon.format("%8d %8d\n", i, i*i); 
 
 
    // Now, write to the file. 
    fmtFile.format("This is a negative number: %(.2f\n\n", 
                   -123.34); 
 
    fmtFile.format("%8s %8s\n", "Value", "Square"); 
 
    for(int i=1; i < 20; i++) 
      fmtFile.format("%8d %8d\n", i, i*i); 
 
 
    fmtFile.close(); 
 
    // Use ioException() to check for file errors. 
    if(fmtFile.ioException() != null) { 
      System.out.println("File I/O Error Occurred"); 
    } 
  } 
}

listing 8
// Use printf() to display various types of formatted data. 
 
import java.util.*; 
 
class PrintfDemo { 
  public static void main(String args[]) { 
 
    // First use PrintStream's printf() method. 
    System.out.printf("Two decimal digits: %.2f\n", 10.0/3.0); 
 
    System.out.printf("Use group separators: %,.2f\n\n", 
                      1546456.87);  
    
    System.out.printf("%10s %10s %10s\n", 
                      "Value", "Root", "Square"); 
 
    for(double i=1.0; i < 20.0; i++)  
      System.out.printf("%10.2f %10.2f %10.2f\n", 
                        i, Math.sqrt(i), i*i); 
 
    System.out.println(); 
 
    Calendar cal = Calendar.getInstance(); 
    System.out.printf("Current time and date: %tc\n", cal); 
  } 
}

listing 9
// Use printf() to create a time stamp. 
 
import java.io.*; 
import java.util.*; 
 
class TimeStamp { 
   
  // Output a time stamp to the specified PrintWriter. 
  // You can precede the time stamp with a message 
  // by passing a string to msg. If no message is  
  // desired, pass an empty string. 
  static void timeStamp(String msg, PrintWriter pw) { 
    Calendar cal = Calendar.getInstance(); 
    pw.printf("%s %tc\n", msg, cal); 
  } 
 
  public static void main(String args[]) { 
 
    // Create a PrintWriter that is linked to a file. 
    PrintWriter pw; 
    try { 
      pw = new PrintWriter(new FileWriter("logfile.txt", true)); 
    } catch(IOException exc) { 
      System.out.println("Cannot Open logfile.txt"); 
      return; 
    } 
    
    timeStamp("File opened", pw); 
 
    try { 
      Thread.sleep(1000); // sleep for 1 second 
    } catch(InterruptedException exc) { 
      pw.printf("Sleep Interrupted"); 
    } 
 
    timeStamp("File closed", pw); 
 
    pw.close(); 
 
    // When using a PrintWriter, check for errors by 
    // calling checkError(). 
    if(pw.checkError()) 
      System.out.println("I/O error occurred."); 
  } 
}

listing 10
// Display short and long date and time formats. 
 
import java.text.*; 
import java.util.*; 
 
class DateFormatDemo { 
  public static void main(String args[]) { 
    Date date = new Date(); 
    DateFormat df; 
 
    df = DateFormat.getDateInstance(DateFormat.SHORT); 
    System.out.println("Short form: " + df.format(date)); 
 
    df = DateFormat.getDateInstance(DateFormat.LONG); 
    System.out.println("Long form: " + df.format(date)); 
 
    System.out.println(); 
 
    df = DateFormat.getTimeInstance(DateFormat.SHORT); 
    System.out.println("Short form: " + df.format(date)); 
 
    df = DateFormat.getTimeInstance(DateFormat.LONG); 
    System.out.println("Long form: " + df.format(date)); 
  } 
}

listing 11
// Demonstrate SimpleDateFormat. 
import java.text.*; 
import java.util.*; 
 
public class SDFDemo { 
 
  public static void main(String args[]) { 
    Date date = new Date(); 
    SimpleDateFormat simpDate; 
 
    // Time in 12-hour format. 
    simpDate = new SimpleDateFormat("hh:mm:ss a"); 
    System.out.println(simpDate.format(date)); 
 
    // Time in 24-hour format. 
    simpDate = new SimpleDateFormat("kk:mm:ss"); 
    System.out.println(simpDate.format(date)); 
 
    // Date and time with month as a number. 
    simpDate = new SimpleDateFormat("dd MMM yyyy hh:mm:ss a"); 
    System.out.println(simpDate.format(date)); 
 
    // Date and time with day and month fully spelled-out. 
    simpDate = new SimpleDateFormat("EEEE MMMMM dd yyyy kk:mm:ss"); 
    System.out.println(simpDate.format(date)); 
  } 
}

listing 12
// Use java.text.NumberFormat to format some numeric values. 
 
import java.text.NumberFormat; 
 
class NumberFormatDemo { 
  public static void main(String args[]) { 
    NumberFormat nf = NumberFormat.getInstance(); 
 
    System.out.println("Default format: " + 
                       nf.format(1234567.678)); 
 
    // Set format to 2 decimal places. 
    nf.setMinimumFractionDigits(2); 
    nf.setMaximumFractionDigits(2); 
 
 
    System.out.println("Format with two decimal places: " + 
                       nf.format(1234567.678)); 
 
    nf.setGroupingUsed(false);  
 
    System.out.println("Format without groupings: "  + 
                       nf.format(1234567.678)); 
 
    // Notice that two decimal places are 
    // provided, even though not all digits are 
    // present in these cases. 
    System.out.println("Notice two decimal places: " + 
                       nf.format(10.0) + ", " + 
                       nf.format(-1.8)); 
  } 
}

listing 13
// Use java.text.NumberFormat to format a currency value. 
 
import java.text.NumberFormat; 
import java.util.*; 
 
class CurrencyFormatDemo { 
 
  public static void main(String args[]) { 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
 
    System.out.println("1989.99 and -210.5 in currency format: " + 
                       nf.format(1989.99) + " " + 
                       nf.format(-210.5)); 
  } 
}

listing 14
// Demonstrate DecimalFormat. 
import java.text.*; 
 
public class DFDemo { 
 
  public static void main(String args[]) { 
    DecimalFormat df; 
 
    // Use group separators and show trailing zeros. 
    // Negative values are shown inside parentheses. 
    df = new DecimalFormat("#,###.00;(#,###.00)"); 
    System.out.println(df.format(7123.00)); 
    System.out.println(df.format(-7123.00)); 
 
    // Don't show trailing zeros. 
    df = new DecimalFormat("#,###.##;(#,###.##)"); 
    System.out.println(df.format(7123.00)); 
    System.out.println(df.format(-7123.00)); 
 
    // Display a percentage. 
    df = new DecimalFormat("#%"); 
    System.out.println(df.format(0.19)); 
    System.out.println(df.format(-0.19)); 
 
    // Display a currency value. 
    df = new DecimalFormat("\u00a4#,##0.00"); 
    System.out.println(df.format(4232.19)); 
    System.out.println(df.format(-4232.19)); 
  } 
}


Valid XHTML 1.0 Strict