在 Java 中显示年份的前两位数字(两位世纪)

java 8object oriented programmingprogramming

使用‘C’日期转换字符显示年份的两位数字 −

System.out.printf("Two-digit Year (Century Name) = %tC/%TC
", d, 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("Localized day name = %tA/%TA
", d, d);       System.out.printf("Two-digit Year (Century Name) = %tC/%TC
", d, d);    } }

输出

Current date and time = 26/11/2018 11:51:18 AM
Localized day name = Monday/MONDAY
Two-digit Year (Century Name) = 20/20

相关文章