«^»
7.2 Some examples of the use of printf

Here are some examples of the use of printf:

0242:       System.out.printf("%3d %1.2f%n%d%n", 
0243:                         tAge, tHeight, tNumberOfChildren);
0244:       String tFormat = 
0245:             "Age is %d, height is %f%nNo. of children is %d%n";
0246:       System.out.printf(tFormat, tAge, tHeight, tNumberOfChildren);
0247:       Calendar tNow = Calendar.getInstance();
0248:       System.out.printf("%tT%n", tNow);  
0249:       System.out.printf("%tY-%tm-%td%n", tNow, tNow, tNow);
0250:       System.out.printf("%tH:%<tM:%<tS%n", tNow);

The above code produces output like the following:

0253:  42 1.85
0254: 2
0255: Age is 42, height is 1.850000
0256: No. of children is 2
0257: 08:03:39
0258: 2004-08-12
0259: 08:03:39

The printf method used above is from the java.io.PrintStream package.