package ysy07;
import java.util.Calendar;
public class CalenderDemo {
public static void main(String[] args) {
//获取日历类对象
Calendar c = Calendar.getInstance();//多态的形式
//public int get (int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
System.out.println("------------");
//public abstract void add (int field,int amount):将制定的时间量添加或减去给定的日历字段
c.add(Calendar.YEAR,-3);
c.add(Calendar.MONTH,+6);
c.add(Calendar.DATE,+1);
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH) + 1;
int date1 = c.get(Calendar.DATE);
System.out.println(year1 + "年" + month1 + "月" + date1 + "日");
System.out.println("------------");
//public final void set (int year ,int month,int date):设置当前日历的年月日
c.set(1998,3,18);
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH) + 1;
int date2 = c.get(Calendar.DATE);
System.out.println(year2 + "年" + month2 + "月" + date2 + "日");
}
}
【java基础】Calender类中的get,add,set方法
最新推荐文章于 2023-03-31 15:38:58 发布