Calender类——字段值介绍

Posted zxf-java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Calender类——字段值介绍相关的知识,希望对你有一定的参考价值。

今天,看到有人这样编写代码,有点不明所以:

Calendar calendar = Calendar.getInstance();

calendar.get(1);

calendar.get(2)+1;

所以自己亲测一番:

public class TestCalendar {
	public static void main(String[] args) {
             System.out.println("Calendar总字段数:" + Calendar.FIELD_COUNT);
		System.out.println("era = " + Calendar.ERA);
		System.out.println("year = " + Calendar.YEAR);
		System.out.println("month = " + Calendar.MONTH);
		System.out.println("week_of_year = " + Calendar.WEEK_OF_YEAR);
		System.out.println("week_of_month = " + Calendar.WEEK_OF_MONTH);
		System.out.println("date = " + Calendar.DATE);
		System.out.println("day_of_month = " + Calendar.DAY_OF_MONTH);
		System.out.println("day_of_year = " + Calendar.DAY_OF_YEAR);
		System.out.println("day_of_week = " + Calendar.DAY_OF_WEEK);
		System.out.println("day_of_week_in_month = " + Calendar.DAY_OF_WEEK_IN_MONTH);
		System.out.println("am_pm = " + Calendar.AM_PM);
		System.out.println("hour = " + Calendar.HOUR);
		System.out.println("hour_of_day = " + Calendar.HOUR_OF_DAY);
		System.out.println("minute = " + Calendar.MINUTE);
		System.out.println("second = " + Calendar.SECOND);
		System.out.println("millisecond = " + Calendar.MILLISECOND);
		System.out.println("zone_offset = " + Calendar.ZONE_OFFSET);
		System.out.println("dst_offset = " + Calendar.DST_OFFSET);   
        }
}            

  测试输出:

Calendar总字段数:17
era = 0
year = 1
month = 2
week_of_year = 3
week_of_month = 4
date = 5
day_of_month = 5
day_of_year = 6
day_of_week = 7
day_of_week_in_month = 8
am_pm = 9
hour = 10
hour_of_day = 11
minute = 12
second = 13
millisecond = 14
zone_offset = 15
dst_offset = 16

  可以看出日期类Calender的字段值都有默认的int类型的值所对应,这样只要记住这些值就可以直接用数值代替,但是感觉没有字段好用。

以上是关于Calender类——字段值介绍的主要内容,如果未能解决你的问题,请参考以下文章

java之Calender类精选

java中的calender类如何使用

使用Calender类获取系统时间和时间和运算

Date类和Calender类

Python时间calender模块介绍

Java Calender 类详解