提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在Java中使用C的Printf样式格式
import java.util.Date; public class Main { public static void main(String[] args) {//N o w J a v a . c o m - 时 代 Java 提 供 // 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); // Formatting date and time Date dt = new Date(); System.out.printf(