在Java中以yy格式格式化Year
将年份格式化为yy格式就像将年份显示为01、02、03、04等。例如,2018年为18。
像这样使用yy格式。
SimpleDateFormat("yy");让我们看一个例子-
//yy格式的年份
SimpleDateFormat simpleformat = new SimpleDateFormat("yy");
String strYear = simpleformat.format(new Date());
System.out.println("Current Year = "+strYear);上面,我们使用了SimpleDateFormat类,因此导入了以下包-
import java.text.SimpleDateFormat;
以下是一个例子-
示例
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class Demo {
   public static void main(String[] args) throws Exception {
      //显示当前日期和时间
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat simpleformat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss");
      System.out.println("Date and time = "+simpleformat.format(cal.getTime()));
      //显示日期
      simpleformat = new SimpleDateFormat("dd/MMMM/yyyy");
      String str = simpleformat.format(new Date());
      System.out.println("Current Date = "+str);
      //yy格式的年份
      simpleformat = new SimpleDateFormat("yy");
      String strYear = simpleformat.format(new Date());
      System.out.println("Current Year = "+strYear);
   }
}输出结果
Date and time = Mon, 26 Nov 2018 11:05:15 Current Date = 26/November/2018 Current Year = 18
