使用“ y”日期转换字符来显示两位数的年份。
System.out.printf("Two-digit Year = %TY\n",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\n",d); System.out.printf("Two-digit Year = %ty\n",d); } }
输出结果
Current date and time = 26/11/2018 12:00:51 PM Four-digit Year = 2018 Two-digit Year = 18