format(String format, Object... args)
As you can see, this method takes the first argument which is a format String and the rest ones are arguments for the format String (the variable argument allows us to specify one or more arguments).A format String consists of static text embedded with format specifiers which must adhere to the following syntax:%[argument_index$][flags][width][.precision]conversion
The elements inside the brackets are optional. Let’s understand each element in briefs:String title = "Effective Java"; float price = 33.953f; System.out.format("%s is a great book. It is sold at %.2f USD today.%n", title, price);Note that the %n format specified at the end of the String produces a line terminator specific to the underlying operating system.Output:
Effective Java is a great book. It is sold at 33.95 USD today.
List<String> listBook = Arrays.asList( "Head First Java", "Effective Java", "The Passionate Programmer", "Head First Design Patterns" ); for (String book : listBook) { System.out.format("%-30s - In Stock%n", book); }Output:
System.out.format("Min value of a byte: %d (%1$#x)%n", Byte.MIN_VALUE); System.out.format("Max value of a byte: %d (%1$#x)%n", Byte.MAX_VALUE); System.out.format("Min value of an int: %d (%1$x)%n", Integer.MIN_VALUE); System.out.format("Max value of an int: %d (%1$x)%n", Integer.MAX_VALUE);Note that there are two format specifiers but only one argument so we use the argument index 1$ to specify the argument position for the specifier. Output:
Min value of a byte: -128 (0x80) Max value of a byte: 127 (0x7f) Min value of an int: -2147483648 (80000000) Max value of an int: 2147483647 (7fffffff)
PrintWriter writer = new PrintWriter(new File("datetime.txt")); writer.format("Now is %tT %n", new Date()); writer.format("Today is %tF %n", new Date()); writer.format("Today is %tD %n", new Date()); writer.close();The output file (datetime.txt) would have the following content:
Now is 23:25:11 Today is 2016-12-14 Today is 12/14/16
String longDate = String.format("Today is %tA %<tB %<td, %<tY", new Date()); System.out.println(longDate);Note that the String.format() method that returns a formatted String, and specify the < to indicate that this specifier uses the same argument as the previous specifier. Output:
Today is Wednesday December 14, 2016
long k = 1000; long m = 1000000; long t = 1000000000; System.out.println("French locale:"); System.out.format(Locale.FRENCH, "One thousand: %,d %n", k); System.out.format(Locale.FRENCH, "One million: %,d %n", m); System.out.format(Locale.FRENCH, "One billion: %,d %n", t);Note that we use the flag ‘,’ to include the locale-specific grouping separators. The output would be:
French locale: One thousand: 1 000 One million: 1 000 000 One billion: 1 000 000 000And the following code formats the numbers based on the default locale:
System.out.println("Default locale:"); System.out.format("One thousand: %,d %n", k); System.out.format("One million: %,d %n", m); System.out.format("One billion: %,d %n", t);On a US locale system, the output would be:
Default locale: One thousand: 1,000 One million: 1,000,000 One billion: 1,000,000,000
String specifiers = "%-30s %-20s %-5.2f%n"; System.out.format(specifiers, "Head First Java", "Kathy Sierra", 23.99f); System.out.format(specifiers, "Thinking in Java", "Bruce Eckel", 25.69f); System.out.format(specifiers, "Effective Java", "Joshua Bloch", 27.88f); System.out.format(specifiers, "The Passionate Programmer", "Chad Fowler", 41.99f); System.out.format(specifiers, "Code Complete", "Steve McConnell", 38.42f);Output:
float k = 1000f; float m = 1000000f; float t = 1000000000f; System.out.format("One thousand: %.1e %n", k); System.out.format("One million: %.1E %n", m); System.out.format("One billion: %.2g %n", t);Output:
One thousand: 1.0e+03 One million: 1.0E+06 One billion: 1.0e+09
System.out.format("%05d %n", 12); System.out.format("%05d %n", 2016); System.out.format("%05d %n", 365); System.out.format("%05d %n", 19001800); System.out.format("%(d %n", -1234); System.out.format("%+d %n", 567); System.out.format("%+d %n", -891);Output:
00012 02016 00365 19001800 (1234) +567 -891
System.out.format("%20s %s %n", "Full Name: ", "John"); System.out.format("%20s %s %n", "E-mail Address: ", "john@gmail.com"); System.out.format("%20s %s %n", "City: ", "New York"); System.out.format("%20s %s %n", "Country: ", "United States");Output: We hope you find these examples helpful and ready-to-use in your daily coding.