Calendar日历类常见方法

发布时间 2023-11-18 10:05:38作者: 夭夭如也

Calendar类

类是一个抽象类,它为特定瞬间与一组诸如 YEARMONTHDAY_OF_MONTHHOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。

Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。Calendar getInstance方法返回一个 Calendar对象,其日历字段已由当前日期和时间初始化:

Calendar calendar = Calendar.getInstance();
calender.set(2021, 2, 2); // 设置年月日

Calendar对象能够生成为特定语言和日历风格实现日期-时间格式化所需的所有日历字段值,例如,日语-格里高里历,日语-传统日历。Calendar定义了某些日历字段返回值的范围,以及这些值的含义。例如,对于所有日历,日历系统第一个月的值是 MONTH == JANUARY

一、通过创建子类初化Calendar类对象

因为Calendar类是抽象类无法实例化,但是它有实现类所以可以通过创建子类初始化Calendar对象。

下图是Calendar实现类结构图。

在这里插入图片描述

通过子类创建初化对象

Calendar sun = new GregorianCalendar(2021, 1, 2);

二、常见方法

返回值 方法名 描述
void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间
boolean after(Object when) 判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后,返回判断结果。
boolean before(Object when) 判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前,返回判断结果。
int compareTo(Calendar ca) 比较两个 Calendar 对象表示的时间值
void set(int field, int value) 将给定的日历字段设置为给定值。
int get(int field) 返回给定日历字段的值。
Date getTime() 返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。
long getTimeInMillis() 返回此 Calendar 的时间值,以毫秒为单位。

三、字段

类型 字段名 描述
static int YEAR
static int MONTH
static int DATE
static int HOUR_OF_DAY
static int MINUTE
static int SECOND
static int MILLISECON 毫秒
static int DAY_OF_MONTH 指示一个月中的某天,和DATE一样
static int DAY_OF_WEEK 指示一个星期中的某天
static int DAY_OF_WEEK_IN_MONTH 指示当前月中的第几个星期
static int WEEK_OF_MONTH 指示当前月中的星期数
static int DAY_OF_YEAR 指示当前年中的天数
static int WEEK_OF_YEAR 指示当前年中的星期数

注意:month是从0开始的,而月份是从1开始的,所以month需要加一。

Calendar calendar = new GregorianCalendar(2023,12,1);
int i = calendar.get(Calendar.MONTH);
System.out.println(i);  						// 输出0,所以设置指定月份时要减一

Calendar now = Calendar.getInstance();  		// 现在日期 2023-11-17
System.out.println(now.get(Calendar.MONTH)); 	// 输出10

三、示例

// 初化Calaendar对象
Calendar future = Calendar.getInstance();
future.set(2021, 2, 2);
Calendar calendar = Calendar.getInstance();
calendar.set(2011, 2, 1);
String futureStr = future.get(Calendar.YEAR) + "-" + future.get(Calendar.MONTH) +
    "-" + future.get(Calendar.DAY_OF_MONTH);
String calendarStr = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) +
    "-" + calendar.get(Calendar.DAY_OF_MONTH);
// 判断此 future 表示的时间是否在指定 calendar 表示的时间之后,返回判断结果。
if (future.after(calendar)) {
    System.out.println("future日期" + futureStr + ",在calendar日期" +
                       calendarStr + "之后"); 
}

输出结果:

future日期2021-2-2,在calendar日期2011-2-1之后

compareTo比较器

// compareTo比较器
int i = future.compareTo(calendar);
if (i > 0) {
    System.out.println("future日期" + futureStr + ",在calendar日期" +
                       calendarStr + "之后"); // <--- 输出这个
} else if (i < 0) {
    System.out.println("future日期" + futureStr + ",在calendar日期" +
                       calendarStr + "之前");
} else {
    System.out.println("相等");
}

输出结果:

future日期2021-2-2,在calendar日期2011-2-1之后

转Date类型格式化输出

Calendar sun = new GregorianCalendar(2021, 1, 2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(sun.getTime()); // 获取Date对象格式化输出
System.out.println(format); 

输出结果:

2021-02-02 00:00:00

end