Calendar类概述
Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。
Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,将语言敏感内容处理好,再返回子类对象,如下:
Calendar类静态方法

Calendar c = Calendar.getInstance(); //返回当前时间
Calendar类常用方法

package com.itheima_05;
import java.util.Calendar;
/*
* Calendar:日历,提供了一些操作年月日时的方法
*
* 获取
* 修改
* 添加
*/
public class CalenderDemo {
public static void main(String[] args) {
//static Calendar getInstance()
Calendar c = Calendar.getInstance();
// void set(int field, int value) :把指定的字段修改成指定的值
// c.set(Calendar.DAY_OF_MONTH, 20);//2018年10月20日
//abstract void add(int field, int amount) :在指定的字段上加上指定的值
c.add(Calendar.DAY_OF_MONTH, 2);//2018年10月12日
//int get(int field) //返回给定日历字段值
//public static final int YEAR 1
// System.out.println(Calendar.YEAR);//1
// int year = c.get(1);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
}
}
网友评论