Calendar class of java
The Calendar in java is often ignored during development. This blog summarizes this class, which is helpful for later project use periods.
Functions of the Calendar constant (field)
Calendar cal = Calendar. getInstance (); cal. get (Calendar. DATE); // ----------------------- 1-31cal.get (Calendar. DAY_OF_MONTH); // --------------- 1-31cal.get (Calendar. DAY_OF_WEEK); // ---------------- calculated from Sunday. If today is Tuesday, 3cal is returned. get (Calendar. DAY_OF_YEAR); // ---------------- cal. get (Calendar. HOUR); // ----------------------- 12-HOUR cal. get (Calendar. HOUR_OF_DAY); // ---------------- 24-hour system. This attribute is generally used to assign values to 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); // ----------------------- the MONTH needs to be obtained by + 1. Therefore, when assigning values, we need-1 to sum up: 1) the true meaning of constants is as above. We generally use these constants to assign values. In other words, you can use it to obtain values. You can also use it to assign values. 2) when assigning values, week and month are worth noting. week needs to specify setFirstDayOfWeek. However, you need to add or subtract 13 from the month) when assigning values, we generally use the year, month, day, hour, minute, second Calendar ar. YEAR, Calendar. MONTH, Calendar. DAY_OF_MONTH, Calendar. HOUR_OF_DAY, Calendar. MINUTE, Calendar. SECOND
Major Assignment Statement
Cal. set (Calendar. XXX, VVVV); // --------------------- assign values to each of the preceding fields. The Code repeats a large cal. set (year, month, date, hour, minute, second); // ----- assign values to fields respectively, which is highly efficient.
Main Computing
Cal1.roll (Calendar. MONTH, 3); // ---------------------- this method is generally not used because it is calculated cyclically only within one MONTH, and its size does not exceed the maximum value of cal1.add (Calendar ar. YEAR,-1); // ----------------------- it is better to use the field of XX_OF_XX for addition and subtraction, and the computation accuracy is cal1.add (field, value); // ------------------------ conclusion: 1) about roll calculation, cal. roll (Calendar. DAY_OF_MONTH, 32); although 32 has exceeded the maximum possibility of 31, but cal does not actually exceed this month, rather it recalculates the remaining days after 32 minus the number of days of this month; 2) cal1.add (Calendar ar. MONTH, 1); if the current value is 8-31, then the value of a MONTH is 9-30, which is truly accurate.
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) obtain the maximum value and the minimum value is a common method. 2) after obtaining the number of milliseconds, you can calculate the value by 1000*60*60.
Calendar to get the current day, current month, and current 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 ;}
// This week's 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 ;}
// This month's date segment 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); String start = sdf. format (cc2.getTime (); return start + "|" + end ;}