import java.util.Date;
public class FormatString {
public static void main(String[] args) {
String name = "Tom";
int age = 100;
Date date = new Date();
float f = 1.2f;
String s1 = String.format("Hello %s", name);
System.out.println(s1);
String s2 = String.format("The age %d is old", age);
System.out.println(s2);
String s3 = String.format("%s is %d years old", name, age);
System.out.println(s3);
System.out.printf("%.3f %n", f);
String day = String.format("Today is %tD", date);
System.out.println(day);
}
}
Output:
$ java FormatString Hello Tom The age 100 is old Tom is 100 years old 1.200 Today is 01/26/14