在Java中显示月份的两位数日期
使用“d”日期转换字符显示月份的两位数日期,例如 27、28、20 等。
System.out.printf("Two-digit day of the month: %td
",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
", d); System.out.printf("Two-digit month: %tm
",d); System.out.printf("Two-digit day of the month: %td
",d); } }
输出
Current date and time = 26/11/2018 12:20:33 PM Four-digit Year = 2018 Two-digit Year = 18 Three-digit Day of the Year: 330 Two-digit month: 11 Two-digit day of the month: 26
广告