欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Calender类——字段值介绍

程序员文章站 2022-03-23 16:40:02
今天,看到有人这样编写代码,有点不明所以: Calendar calendar = Calendar.getInstance(); calendar.get(1); calendar.get(2)+1; 所以自己亲测一番: 测试输出: 可以看出日期类Calender的字段值都有默认的int类型的值所 ......

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

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类型的值所对应,这样只要记住这些值就可以直接用数值代替,但是感觉没有字段好用。