在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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短