JAVA - Calendar 示例代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarDemo01 {
    public static void main(String[] args) throws ParseException {
        // Calendar 是抽象类,不能直接实例化
        // 实例化 可以通过static方法Calendar.getInstance()
        // 或者使用 Calendar的子类GregorianCalendar,其实结果是一样的都是GregorianCalendar对象
        Calendar c1 = Calendar.getInstance();
        System.out.println(c1.getClass());
        // 常用方法
        // get 返回日历相关的int值 MONTH一月返回的是0,DAY_OF_WEEK 周日返回的是1
        System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
        System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
        System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
        System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
        //add
        System.out.println("-- add ------------------------------");
        c1.add(Calendar.MONTH, 3);
        System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
        System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
        System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
        System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
        c1.add(Calendar.MONTH, -4);
        System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
        System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
        System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
        System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
        //set
        System.out.println("-- set ------------------------------");
        c1.set(Calendar.YEAR, 2022);
        System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
        System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
        System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
        System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
        //setTime
        System.out.println("---setTime-----------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date d = sdf.parse("2020-12-10 08:59:44");
        c1.setTime(d);
        System.out.println("FIELD-DAY_OF_YEAR:" + c1.get(Calendar.DAY_OF_YEAR));
        System.out.println("FIELD-YEAR:" + c1.get(Calendar.YEAR) + "年");
        System.out.println("FIELD-MONTH:" + c1.get(Calendar.MONTH) + "月");
        System.out.println("FIELD-DAY_OF_MONTH:" + c1.get(Calendar.DAY_OF_MONTH) + "日");
        //getTime
        System.out.println("---getTime-----------------------------");
        d = c1.getTime();
        System.out.println(sdf.format(d));

    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值