import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo01 {
public static void main(String[] args) throws ParseException {
// Calendar 是抽象类,不能直接实例化
// 实例化 可以通过static方法Calendar.getInstance()
// 或者使用 Calendar的子类GregorianCalendar,其实结果是一样的都是GregorianCalendar对象
Calendar c1 = Calendar.getInstance();
System.out.println(c1.getClass());
// 常用方法
// get 返回日历相关的int值 MONTH一月返回的是0,DAY_OF_WEEK 周日返回的是1
System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
//add
System.out.println("-- add ------------------------------");
c1.add(Calendar.MONTH, 3);
System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
c1.add(Calendar.MONTH, -4);
System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
//set
System.out.println("-- set ------------------------------");
c1.set(Calendar.YEAR, 2022);
System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
//setTime
System.out.println("---setTime-----------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = sdf.parse("2020-12-10 08:59:44");
c1.setTime(d);
System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
//getTime
System.out.println("---getTime-----------------------------");
d = c1.getTime();
System.out.println(sdf.format(d));
}
}