在Java中以yyyy格式格式化Year
以yyyy格式格式化年份就像显示整个年份。例如,2018年。
使用这样的yyyy格式-
SimpleDateFormat("yyyy");让我们看一个例子-
//yyyy格式的年份
SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy");
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);
//yyyy格式的年份
simpleformat = new SimpleDateFormat("yyyy");
String strYear = simpleformat.format(new Date());
System.out.println("Current Year = "+strYear);
}
}输出结果
Date and time = Mon, 26 Nov 2018 11:12:39 Current Date = 26/November/2018 Current Year = 2018
热门推荐
10 祝福语简短七夕
11 生日祝福语贺卡老师简短
12 生日祝福语简短搞笑女儿
13 周二祝福语大全简短
14 道教生日祝福语简短大全
15 简短祝福语乔迁新家贺词
16 生日寿宴祝福语简短精辟
17 致姐姐生日祝福语简短
18 宝宝生病简短祝福语大全