Java程序以AM或PM显示小时和分钟
首先,创建一个Formatter和一个Calendar对象。
Formatter f = new Formatter(); Calendar c = Calendar.getInstance();
对于AM/PM标记,请使用以下转换字符-
p
在这里,您可以看到AM/PM标记-
f = new Formatter();
System.out.println(f.format("Hour: %tl %1$Tp", 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 %1$Tp", c));
f = new Formatter();
System.out.println(f.format("Minute: %1$tM", c));
}
}输出结果
Current date and time: Mon Nov 26 07:41:35 UTC 2018 Hour and Minute with AM/ PM: 7:41 AM Hour: 7 AM Minute: 41
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特