使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP