java.util.Calendar

package com.etc.usual;

import java.util.Calendar;
import java.util.Date;
/**
 * public final static int YEAR = 1;
 * public final static int MONTH = 2;
 * public final static int WEEK_OF_YEAR = 3;
 * public final static int WEEK_OF_MONTH = 4;
 * public final static int DATE = 5;
 * public final static int DAY_OF_MONTH = 5;
 * public final static int DAY_OF_YEAR = 6;
 * public final static int DAY_OF_WEEK = 7;
 * public final static int DAY_OF_WEEK_IN_MONTH = 8;     //
 * 
 * 
 * 
 */
public class TestCalendar {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar c=Calendar.getInstance();//获取Calendar对象的一种方法
		
		System.out.println("Calendar.YEAR:"+Calendar.YEAR);  
		System.out.println("Calendar.MONTH:"+Calendar.MONTH);  
		System.out.println("Calendar.DATE:"+Calendar.DATE);  
		System.out.println("Calendar.WEEK_OF_YEAR:"+Calendar.WEEK_OF_YEAR);  
		System.out.println("Calendar.WEEK_OF_MONTH:"+Calendar.WEEK_OF_MONTH);  
		System.out.println("Calendar.DAY_OF_MONTH:"+Calendar.DAY_OF_MONTH);  
		System.out.println("Calendar.DAY_OF_YEAR:"+Calendar.DAY_OF_YEAR);  
		System.out.println("Calendar.DAY_OF_WEEK:"+Calendar.DAY_OF_WEEK);  
		
		
		//查看Calendar是什么 
		c=Calendar.getInstance();
		System.out.println("\nCalendar:"+c);//输出的是时间的详细信息
		Date date=new Date();
		System.out.println("date_getTime:"+date.getTime()); 
		
		//setTime:使用给定的 Date 设置此 Calendar 的时间。
		date.setYear(2015-1900);
		c.setTime(date);
		System.out.println("\nsetTime:"+c); 
		System.out.println("setTime:"+c.get(Calendar.YEAR));
		System.out.println("setTime:"+c.get(1));
		
		//将Calendar转换为Date    getTime():返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。
		Date d1 = c.getTime();
		System.out.println("\ngetTime:"+d1+"----"+d1.getYear());  
		
		//set(int field, int value) 将给定的日历字段设置为给定值。
		c.set(Calendar.YEAR,2000); 
		System.out.println("\nc.set(Calendar.YEAR,2000):"+c.get(Calendar.YEAR));
		
		c.set(Calendar.MONTH,11); 
		System.out.println("c.set(Calendar.MONTH,11):"+c.get(Calendar.MONTH)+"-------"+(c.getTime()));
		
		c.set(Calendar.MONTH,12); 
		System.out.println("c.set(Calendar.MONTH,12):"+c.get(Calendar.MONTH)+"-------"+(c.getTime()));
		
		c.set(Calendar.DATE,20); 
		System.out.println("c.set(Calendar.DATE,2000):"+c.get(Calendar.DATE)+"-------"+(c.getTime()));
		
		//set(int year, int month, int date)  设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
		c.set(2020,11,31);
		System.out.println("\nc.set(2020,11,31):"+c.getTime()); 
		
		//set(int year, int month, int date, int hourOfDay, int minute, int second) 
        //设置字段 YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值。
		//c.set(2000,1,10,23,55,20);
		c.set(2017,0,1,7,59,60);  //闰秒:2017年1月1日7时59分59秒
		System.out.println("\nc.set(2022,1,10,23,55,20):"+c.getTime()); 
		
		//setLenient(boolean lenient)指定日期/时间解释是否是宽松的。
//		c.setLenient(true); 
//		c.set(Calendar.MONTH,15);
//		System.out.println("\nc.setLenient(true):"+c.getTime());
		
//		c.setLenient(false);
//		c.set(Calendar.MONTH,2);
//		System.out.println("\nc.setLenient(false):"+c.getTime());
		
		//getActualMaximum(int field)  给定此 Calendar 的时间值,返回指定日历字段可能拥有的最大值。 
		//能被4整除且又能不能被100整除 是闰年 能直接被400整除也是闰年
		System.out.println("\ngetActualMaximum(Calendar.YEAR):"+c.getActualMaximum(Calendar.YEAR));
		System.out.println("getActualMaximum(Calendar.MONTH):"+c.getActualMaximum(Calendar.MONTH));
		System.out.println("getActualMaximum(Calendar.DATE):"+c.getActualMaximum(Calendar.DATE));//返回的是指定年月的天数最大值
		System.out.println("getActualMaximum(Calendar.SECOND):"+c.getActualMaximum(Calendar.SECOND));//返回的是指定年月的天数最大值
		
		//getActualMinimum(int field)  给定此 Calendar 的时间值,返回指定日历字段可能拥有的最小值。
		System.out.println("\ngetActualMinimum(Calendar.YEAR):"+c.getActualMinimum(Calendar.YEAR));//年份从公元元年计时
		System.out.println("getActualMinimum(Calendar.MONTH):"+c.getActualMinimum(Calendar.MONTH));
		System.out.println("getActualMinimum(Calendar.DATE):"+c.getActualMinimum(Calendar.DATE));
		
		//getFirstDayOfWeek()  获取一星期的第一天;例如,在美国,这一天是 SUNDAY,而在法国,这一天是 MONDAY。
		System.out.println("\ngetFirstDayOfWeek():"+c.getFirstDayOfWeek());  
		System.out.println("Calendar.SUNDAY:"+Calendar.SUNDAY); 
		System.out.println("Calendar.MONDAY:"+Calendar.MONDAY);
		
		//getMaximum(int field)  返回此 Calendar 实例给定日历字段的最大值 
		//getMaximum是指整个的年月日范围内的最大值     例如:天数就是在28,29,30,31之间的最大值
		//getActualMaximum是指日期设定好的年月日的当前最大值    例如:天数就是设定的月份的实际天数最大值
		System.out.println("\nc.getMaximum(Calendar.YEAR):"+c.getMaximum(Calendar.YEAR));
		System.out.println("c.getMaximum(Calendar.MONTH):"+c.getMaximum(Calendar.MONTH));
		System.out.println("c.getMaximum(Calendar.DATE):"+c.getMaximum(Calendar.DATE));//最大值取决于日历系统实例的特定参数。   即日历的值
		System.out.println("c.getMaximum(Calendar.SECOND):"+c.getMaximum(Calendar.SECOND));
		   
		//add(int field, int amount)    根据日历的规则,为给定的日历字段添加或减去指定的时间量。
		//Sun Jan 01 08:00:00 CST 2017
		c.add(Calendar.YEAR,3); 
		System.out.println("\nc.add(Calendar.YEAR,3):"+c.getTime()); 
		c.add(Calendar.YEAR,-3); 
		System.out.println("c.add(Calendar.YEAR,-3):"+c.getTime()); 
//		c.add(Calendar.MONTH,11);//添加11个月,不是设置月份
//		System.out.println("c.add(Calendar.MONTH,11):"+c.getTime()); 
//		c.add(Calendar.MONTH,5);//当前是12月,添加5个月后,年份加一,月份变为五月
//		System.out.println("c.add(Calendar.MONTH,5):"+c.getTime()); 
		
		
		//roll(int field, int amount)  向指定日历字段添加指定(有符号的)时间量,不更改更大的字段。
		c.roll(Calendar.YEAR,3);
		System.out.println("\nc.roll(Calendar.YEAR,3):"+c.getTime()); 
		c.roll(Calendar.MONTH,11);//添加11个月,不是设置月份
		System.out.println("c.roll(Calendar.MONTH,11):"+c.getTime()); 
		c.roll(Calendar.MONTH,5);//当前是12月,添加5个月后,年份没有加一,而月份变成了五月
		System.out.println("c.roll(Calendar.MONTH,5):"+c.getTime()); 
		
		//after(Object when) )  判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后。
		Calendar c2=Calendar.getInstance(); 
		System.out.println("\nd1.after(d2):"+c.after(c2));   //c:2020   c2:2017
		
		//before(Object when) )  判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前。 
		System.out.println("d1.before(d2):"+c.before(c2));
	}

}

  

 

Calendar.YEAR:1
Calendar.MONTH:2
Calendar.DATE:5
Calendar.WEEK_OF_YEAR:3
Calendar.WEEK_OF_MONTH:4
Calendar.DAY_OF_MONTH:5
Calendar.DAY_OF_YEAR:6
Calendar.DAY_OF_WEEK:7

Calendar:java.util.GregorianCalendar[time=1493957201656,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=4,WEEK_OF_YEAR=18,WEEK_OF_MONTH=1,DAY_OF_MONTH=5,DAY_OF_YEAR=125,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=6,SECOND=41,MILLISECOND=656,ZONE_OFFSET=28800000,DST_OFFSET=0]
date_getTime:1493957201657

setTime:java.util.GregorianCalendar[time=1430798801657,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=125,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=6,SECOND=41,MILLISECOND=657,ZONE_OFFSET=28800000,DST_OFFSET=0]
setTime:2015
setTime:2015

getTime:Tue May 05 12:06:41 CST 2015----115

c.set(Calendar.YEAR,2000):2000
c.set(Calendar.MONTH,11):11-------Tue Dec 05 12:06:41 CST 2000
c.set(Calendar.MONTH,12):0-------Fri Jan 05 12:06:41 CST 2001
c.set(Calendar.DATE,2000):20-------Sat Jan 20 12:06:41 CST 2001

c.set(2020,11,31):Thu Dec 31 12:06:41 CST 2020

c.set(2022,1,10,23,55,20):Sun Jan 01 08:00:00 CST 2017

getActualMaximum(Calendar.YEAR):292278994
getActualMaximum(Calendar.MONTH):11
getActualMaximum(Calendar.DATE):31
getActualMaximum(Calendar.SECOND):59

getActualMinimum(Calendar.YEAR):1
getActualMinimum(Calendar.MONTH):0
getActualMinimum(Calendar.DATE):1

getFirstDayOfWeek():1
Calendar.SUNDAY:1
Calendar.MONDAY:2

c.getMaximum(Calendar.YEAR):292278994
c.getMaximum(Calendar.MONTH):11
c.getMaximum(Calendar.DATE):31
c.getMaximum(Calendar.SECOND):59

c.add(Calendar.YEAR,3):Wed Jan 01 08:00:00 CST 2020
c.add(Calendar.YEAR,-3):Sun Jan 01 08:00:00 CST 2017

c.roll(Calendar.YEAR,3):Wed Jan 01 08:00:00 CST 2020
c.roll(Calendar.MONTH,11):Tue Dec 01 08:00:00 CST 2020
c.roll(Calendar.MONTH,5):Fri May 01 08:00:00 CST 2020

d1.after(d2):true
d1.before(d2):false

posted @ 2017-05-05 12:13  奋斗的少年WH  阅读(378)  评论(0编辑  收藏  举报