使用Java中的Formatter仅显示小时和分钟
首先,创建一个Formatter和一个Calendar对象。
Formatter f = new Formatter(); Calendar c = Calendar.getInstance();
要使用Formatter类显示小时,请使用'l'转换字符-
f = new Formatter();
System.out.println(f.format("Hour: %tl", c));要使用Formatter类显示分钟,请使用'M'转换字符-
f = new Formatter();
System.out.println(f.format("Minute: %1$tM", c));以下是一个例子-
示例
import java.util.Calendar;
import java.util.Formatter;
public class Demo {
public static void main(String args[]) {
Formatter f = new Formatter();
Calendar c = Calendar.getInstance();
System.out.println("Current date and time: "+c.getTime());
f = new Formatter();
System.out.println(f.format("Hour and Minute with AM/ PM: %tl:%1$tM %1$Tp", c));
f = new Formatter();
System.out.println(f.format("Hour: %tl", c));
f = new Formatter();
System.out.println(f.format("Minute: %1$tM", c));
}
}输出结果
Current date and time: Mon Nov 26 07:35:43 UTC 2018 Hour and Minute with AM/ PM: 7:35 AM Hour: 7 Minute: 35
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短