Calendar日历类(抽象类)的使用

Calendar日历类(抽象类)的使用

 

 

4. java.util.Calendar( 日历)类 类
 Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
 获取Calendar实例的方法
 使用Calendar.getInstance()方法
 调用它的子类GregorianCalendar的构造器。
 一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想
要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、
MINUTE、SECOND
 public void set(int field,int value)
 public void add(int field,int amount)
 public final Date getTime()
 public final void setTime(Date date)
 注意:
 获取月份时:一月是0,二月是1,以此类推,12月是11
 获取星期时:周日是1,周二是2 , 。。。。周六是7

/*
    Calendar日历类(抽象类)的使用

     */
    @Test
    public void testCalendar(){
        //1.实例化
        //方式一:创建其子类(GregorianCalendar)的对象
        //方式二:调用其静态方法getInstance()
        Calendar calendar = Calendar.getInstance();
//        System.out.println(calendar.getClass());

        //2.常用方法
        //get()
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

        //set()
        //calendar可变性
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //add()
        calendar.add(Calendar.DAY_OF_MONTH,-3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime():日历类---> Date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime():Date ---> 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

    }
}

Calendar日历类(抽象类)的使用

 

 

"C:\Program Files\Java\jdk1.8.0_131\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\lib\idea_rt.jar=63309:C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\lib\idea_rt.jar;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\plugins\junit\lib\junit-rt.jar;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Users\Administrator\Desktop\JavaSenior\out\production\day04;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\plugins\testng\lib\testng.jar;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\plugins\testng\lib\jcommander.jar;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\lib\junit-4.12.jar;C:\MyProgramInstall\IntelliJ IDEA 2017.3.1\lib\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.atguigu.java.DateTimeTest,testCalendar
17
107
22
19
Mon Apr 19 10:37:59 CST 2021
17

Process finished with exit code 0

 

上一篇:最终我还是退订了131个公众号,只留下10个,每一个都经得住考验


下一篇:leetcode 131. 分割回文串