Skip to content

Latest commit

 

History

History
43 lines (30 loc) · 996 Bytes

5. Calendar.md

File metadata and controls

43 lines (30 loc) · 996 Bytes

5. Calendar

GregorianCalendar:

·一月是0,。。。,十二月是11

·周日是1,。。。周六是7

package com.alexanderli95.calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 测试日期类
 */
public class TestCalendar{
    public static void main(String[] args){
        Calendar c=new GregorianCalendar();    //Calendar是抽象类,要用子类来new对象
        //c.set(2019,03,05,11,20,30);    //03是四月

        //另一种设置参数的方式,缺省的参数被设置为当前时间参数
        c.set(Calendar.YEAR,2019);
        c.set(Calendar.MONTH,03);

        //返回单一参数
        int y=c.get(Calendar.YEAR);
        System.out.println(y);

        Date d=c.getTime();  //返回时间对象
        System.out.println(d);

        //日期计算
        c.add(Calendar.YEAR,30);  //加30年
        System.out.println(c);
    }
}