Home >Java >javaTutorial >Introduction to the use of Calendar class in Java

Introduction to the use of Calendar class in Java

黄舟
黄舟Original
2017-10-12 10:31:492041browse

This article mainly introduces the usage of Calendar class in Java, and analyzes the functions, method functions and related usage skills of Calendar class in detail in the form of examples. Friends in need can refer to the following

The examples of this article are described Learn how to use the Calendar class in java. Share it with everyone for your reference, the details are as follows:

Calendar in Java is often ignored during development. This blog summarizes this class, which will be helpful when using periods in subsequent projects.

The role of Calendar constant (field)


##

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

Summary:

1) The real meaning of constants is as above. We generally use these constants for assignment. In other words, we can get the value through it and also perform corresponding assignment through it.

2) When assigning values, week and month are very different. It is worth noting that setFirstDayOfWeek needs to be specified for week, however, month needs to be added or subtracted by 1
3) When assigning values, we generally use year, month, day, hour, minute and second

Calendar.YEAR, Calendar .MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND

Main assignment statement


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

Main calculation


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

Summary:

1) Regarding the calculation of roll, cal.roll(Calendar.DAY_OF_MONTH, 32); Although 32 has exceeded the maximum possible 31, cal In fact, it will not exceed the month. Instead, after subtracting the number of days in the month from 32, the remaining days will be recalculated;

2) Regarding the calculation of add, cal1.add(Calendar.MONTH, 1); If the current is 8-31, then, if you add one month, it will be 9-30. This is the real accuracy

Main value statement


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

Summary:

1) Obtaining the maximum value and the minimum value are very common methods

2) After obtaining the milliseconds, you can pass 1000 *60*60 Calculation

Calendar Gets the day\month\week


// 当天
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;
}

The above is the detailed content of Introduction to the use of Calendar class in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn