Java examples for Language Basics:Number Format
Using C's Printf-style Formatting in Java
import java.util.Date; public class Main { public static void main(String[] args) { // Formatting strings System.out.printf("%1$s, %2$s, and %3$s %n", "Fu", "Hu", "Lo"); System.out.printf("%3$s, %2$s, and %1$s %n", "Fu", "Hu", "Lo"); // Formatting numbers System.out.printf("%1$4d, %2$4d, %3$4d %n", 1, 10, 100); System.out.printf("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000); System.out.printf("%1$-4d, %2$-4d, %3$-4d %n", 1, 10, 100); System.out.printf("%1$-4d, %2$-4d, %3$-4d %n", 10, 100, 1000); // ww w . j a v a2s .c o m // Formatting date and time Date dt = new Date(); System.out.printf("Today is %tD %n", dt); System.out.printf("Today is %tF %n", dt); System.out.printf("Today is %tc %n", dt); } }