代码如下:
import java.util.Calendar; import java.util.Date; public class StartApplication { public static void main(String[] args) { //1.实例化 //方式一:创建其子类(Gregoriancalendar)的对象/方式二:调用其静态方法getInstance( ) Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getClass());//class java.util.GregorianCalendar //常用方法 //get() int month = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(month); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //set() calendar.set(Calendar.DAY_OF_YEAR, 100); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //add() calendar.add(Calendar.DAY_OF_YEAR, 10); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //getTime():日历类-->Date类 Date time = calendar.getTime(); System.out.println(time); //setTime():Date类-->日历类 calendar.setTime(new Date()); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); } }
结果:
class java.util.GregorianCalendar
10
130
100
110
Wed Apr 20 14:42:06 CST 2022
130