在 Java 中使用 Formatter 显示完整日期和时间信息
首先,创建一个 Formatter 和 Calendar 对象。
Formatter f = new Formatter(); Calendar cal = Calendar.getInstance();
现在显示当前日期和时间。我们在此以小写和大写显示日期 −
f = new Formatter(); System.out.println(f.format("
Date and Time (lowercase): %tc
", cal)); f = new Formatter(); System.out.println(f.format("Date and Time (uppercase): %Tc
", cal));
以下是一个示例 −
示例
import java.util.Calendar; import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar cal = Calendar.getInstance(); System.out.println("Current date and time: "+cal.getTime()); f = new Formatter(); System.out.println(f.format("
Date and Time (lowercase): %tc
", cal)); f = new Formatter(); System.out.println(f.format("Date and Time (uppercase): %Tc
", cal)); } }
输出
Current date and time: Mon Nov 26 07:24:21 UTC 2018 Date and Time (lowercase): Mon Nov 26 07:24:21 UTC 2018 Date and Time (uppercase): MON NOV 26 07:24:21 UTC 2018
广告