首页 > 代码库 > Java Calendar使用

Java Calendar使用

刚刚在敲代码,需要获取当前的时间,来总结下Java Calendar的使用:

The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Following are the important points about Calendar

 

API 文档上给的官方解释:

java.util.Calendar类是一个抽象类,在某一特定的瞬间或日历上,提供年、月、日、小时之间的转换提供方法。

只要来眼界下Calendar中的常量(filed)的作用

Calendar cal = Calendar.getInstance();
		 cal.get(Calendar.DATE);//-----------------------当天 1-31
		 cal.get(Calendar.DAY_OF_MONTH);//---------------当天 1-31
	cal.get(Calendar.DAY_OF_WEEK);//----------------从星期天开始计算,如果今天星期二,那么返回3
		 cal.get(Calendar.DAY_OF_YEAR);//----------------
		 cal.get(Calendar.HOUR);//-----------------------12小时制
		 cal.get(Calendar.HOUR_OF_DAY);//----------------24小时制,一般使用这个属性赋值
		 cal.get(Calendar.MILLISECOND);//----------------
		 cal.get(Calendar.MINUTE);//---------------------
		 cal.get(Calendar.SECOND);//---------------------
		 cal.get(Calendar.WEEK_OF_MONTH);//--------------
		 cal.get(Calendar.WEEK_OF_YEAR);//---------------
	cal.get(Calendar.MONTH);//-----------------------月份获取需要 +1,那么,赋值时需要 -1

 1.运用这些常量赋值,通过这些常量获取值,同样可以通过它进行赋值

2.赋值时,week与mooth是需要注意,week需要制定setFirstDayOfWeek,然而,月份则需要加减1

3.赋值时,一般采用年、月、日、时、分、秒

  Calendar.YEAR 、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECON

 

 

主要赋值:

 

                cal.set(Calendar.XXX, VVVV);//--------------------- 对以上每个字段(field)进行赋值,代码重复较大
		cal.set(year,month,date,hour,minute,second);//----- 分别对字段(field)进行赋值,效率高

 

 

 

主要计算:

  cal1.roll(Calendar.MONTH,3);//---------------------- 一般不使用,原因是该方法只在一个月里面循环计算,其大小不会超过该月最值
                cal1.add(Calendar.YEAR,-1);//----------------------- 使用 XX_OF_XX 的field进行加减计算效果更佳,而且计算准确
                cal1.add(field,value);//----------------------------

 

1.对于roll的计算,cal.roll(Calendar.DAY_OF_MONTH,32),虽然32已经超出了最大的可能31,但是cal实际是不会超出该月,而是把32减去该月天数之后,重新计算剩下的天数;

2。关于add的计算,cal1.add(Calendar.MONTH, 1); 如果当前为8-31,那么,加一个月的话就是9-30,这个才是真正的准确

 

 取值语句:

cal.getMaximum(Calendar.DATE);
        cal.get(Calendar.DATE);
        cal.getMinimum(Calendar.DATE);
        cal.setTimeInMillis(cal.getTime().getTime());
        cal.setTimeInMillis(new Date().getTime());

 

 

// 当天
	public String getThisToday(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Calendar cal = Calendar.getInstance();
	    cal.set(Calendar.HOUR_OF_DAY,0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND,0);
            String start = sdf.format(cal.getTime());
            cal.set(Calendar.HOUR_OF_DAY,23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND,59);
            String end = sdf.format(cal.getTime());
            return start+"|"+end;
	}

 

   // 本周
	 public String getThisWeekDate(){
		   	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar ca = Calendar.getInstance();
			ca.setFirstDayOfWeek(Calendar.MONDAY);
			ca.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
	ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),ca.get(Calendar.DATE),23,59,59);
			String end = sdf.format(ca.getTime());
			ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			ca.set(Calendar.HOUR_OF_DAY,0);
			ca.set(Calendar.MINUTE, 0);
			ca.set(Calendar.SECOND,0);
			String start = sdf.format(ca.getTime());
			return start+"|"+end;
		    }

 

 

   //本月日期段
	    public String getThisMonthDate(){
	   	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cc2 = Calendar.getInstance();
		int maxMonthDay = cc2.getActualMaximum(Calendar.DAY_OF_MONTH);
		cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),maxMonthDay,23,59,59);
		String end = sdf.format(cc2.getTime());
		cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),1,0,0,0);
		String start = sdf.format(cc2.getTime());
		return start+"|"+end;
	    }

 

Java Calendar使用