Calendar類(抽象類)日曆的使用
阿新 • • 發佈:2021-12-02
1 import java.util.Calendar; 2 import java.util.Date; 3 4 /** 5 *Calendar類(抽象類)日曆的使用 6 * 7 * @author fu jingchao 8 * @creat 2021/11/7-17:10 9 */ 10 public class CalendarTest { 11 public static void main(String[] args) { 12 //1.例項化 13 //方式一:建立其子類(GregorianCalendar)的物件 14 //此為本人學習筆記,若有錯誤,請不吝賜教方式二:呼叫其靜態方法getInstance() 15 Calendar calendar = Calendar.getInstance(); 16 System.out.println(calendar.getClass());//class java.util.GregorianCalendar 17 18 //2.常用方法 19 //1.get() 20 int days = calendar.get(Calendar.DAY_OF_MONTH); 21 System.out.println(days);22 System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); 23 24 //2.set() 25 calendar.set(Calendar.DAY_OF_MONTH,22); 26 days = calendar.get(Calendar.DAY_OF_MONTH); 27 System.out.println(days); 28 29 //3.add() 30 calendar.add(Calendar.DAY_OF_MONTH,3);//如果是負數,就是減幾天 31 days = calendar.get(Calendar.DAY_OF_MONTH); 32 System.out.println(days); 33 34 //4.getTime():日曆類---->Date 35 Date date = calendar.getTime(); 36 System.out.println(date);//Sat Dec 25 16:58:11 CST 2021 37 38 //5.setTime():Date---->日曆類 39 Date date1 = new Date(); 40 calendar.setTime(date1); 41 days = calendar.get(Calendar.DAY_OF_MONTH); 42 System.out.println(days); 43 44 System.out.println(calendar.get(Calendar.MONTH));//注意:獲取月份時,一月是0.二月是1,...,12月是11 45 46 } 47 48 }