在 Java 中以两位数显示月份
使用“m”日期转换字符可以显示两位数的月份。
System.out.printf("Two-digit month: %tm
",d);上面,d 是一个日期对象,-
Date d = new Date();
下面是一个示例,-
示例
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Demo {
public static void main(String[] args) throws Exception {
Date d = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String format = dateFormat.format(d);
System.out.println("Current date and time = " + format);
System.out.printf("Four-digit Year = %TY
",d);
System.out.printf("Two-digit Year = %ty
",d);
System.out.printf("Three-digit Day of the Year: %tj/%Tj
", d, d);
System.out.printf("Two-digit month: %tm
",d);
}
}输出
Current date and time = 26/11/2018 12:18:51 PM Four-digit Year = 2018 Two-digit Year = 18 Three-digit Day of the Year: 330/330 Two-digit month: 11
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP