The calendar API usage in Java is fully parsed
The first part introduces the definition of calendar:
Calendar can be regarded as an abstract class. Its implementation adopts the factory method in design pattern. When we get the calendar instance, the calendar will return the corresponding calendar object according to the passed parameters. There are two ways to get the calendar instance: (1) when we use calendar When getInstance () gets the calendar, the default is a gregoriancalendar object returned. Gregoriancalendar is an implementation class of calendar. It provides the standard calendar system used in most countries / regions in the world. (2) When we pass calendar GetInstance (timezone, timezone, locale) or calendar GetInstance (timezone) or calendar When getInstance (locale) gets the calendar, it returns "the calendar used by the corresponding time zone or region". For example, in the case of Japan, the Japanese Imperial calendar object is returned. Refer to the following codes:
After we get the calendar instance, we can operate the calendar through some column methods provided by calendar.
The second part is the principle and idea of calendar. We use calendar to operate the fields of "year, month, day, week, hour, minute and second" of calendar. Next, we will learn the source, definition and calculation method of these fields. 1. The source of the values in the fields of calendar. We use calendar, which is nothing more than "year, month, day, week, hour, minute, second" and other information. So how does it do it? In essence, calendar saves a time. As defined below:
Calendar calculates "calendar's year, month, day, week, hour, minute, second" and other information according to time.
2. Definition of each field of calendar and initialization of "year, month, day, week, hour, minute and second" of calendar. There are 17 fields in total. When we use calendar, we just use these 17 fields. They are defined as follows: (field 0) public final static int era = 0; Description: era. Value: can only be 0 or 1. 0 stands for BC ("before Christ", i.e. BC), and 1 stands for AD (Latin "Anno Domini", i.e. AD). (field 1) public final static int year = 1; Description: year. (field 2) public final static int month = 2; Note: monthly values can be January, February, March, April, may, June, July, August, September, October, November, December, undecimber. The first month is January, which is 0. (field 3) public final static int week_ OF_ YEAR = 3; Note: the current date corresponds to the week in the current year. The value for the first week of the year is 1. (field 4) public final static int week_ OF_ MONTH = 4; Note: the current date corresponds to the week of the month. The value for the first week of the month is 1. (field 5) public final static int date = 5; Description: day. The value for the first day of the month is 1. (field 5) public final static int day_ OF_ MONTH = 5; Note: the same as "date" means "day". (field 6) public final static int day_ OF_ YEAR = 6; Note: the current date corresponds to the day of the year. The value for the first day of the year is 1. (field 7) public final static int day_ OF_ WEEK = 7; Description: day of the week. Values: can be Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. Where Sunday is 1, Monday is 2, and so on. (field 8) public final static int day_ OF_ WEEK_ IN_ MONTH = 8; Description: the week ordinal of the current month. Value: day_ OF_ Month 1 to 7 always correspond to day_ OF_ WEEK_ IN_ MONTH 1; 8 to 14 always correspond to day_ OF_ WEEK_ IN_ Month 2, and so on. (field 9) public final static int am_ PM = 9; Note: morning or afternoon value: can be am or PM. Am is 0, indicating morning; PM is 1, indicating afternoon. (field 10) public final static int hour = 10; Description: indicates the hour of the day. Hour is used for 12 hour clock (0 - 11). Noon and midnight are represented by 0 instead of 12. (field 11) public final static int hour_ OF_ DAY = 11; Description: indicates the hour of the day. HOUR_ OF_ Day is used for 24-hour clock. For example, at 10:04:15.250 PM, hour_ OF_ Day is 22. (field 12) public final static int minute = 12; Description: the minute of an hour. For example, at 10:04:15.250 PM, minute is 4. (field 13) public final static int second = 13; Description: the second of a minute. For example, at 10:04:15.250 PM, second is 15. (field 14) public final static int millisecond = 14; Description: the millisecond of a second. For example, at 10:04:15.250 PM, millisecond is 250. (field 15) public final static int zone_ OFFSET = 15; Description: milliseconds indicates the approximate offset from GMT. (field 16) public final static int DST_ OFFSET = 16; Description: indicates the offset of daylight saving time in milliseconds. public final static int FIELD_ COUNT = 17; These 17 fields are saved in the int array. It is defined as follows:
Protected calendar (timezone, zone, locale, a locale) this is the constructor of calendar. It will be called by the constructor of its subclass to create a new array of "saving 17 field data of calendar".
3. Calculation of values of all fields in calendar. Take get (int field) as an example to briefly describe the calculation and operation of 17 fields in calendar. Get (int field) is to get the value of the "field" field. It is defined as follows:
Description: the code of get (int field) is very simple. First, calculate the value of each field through complete(), and then return the "value of field" through internalget (field). The function of complete () is to calculate the values of each field of calendar. It is defined in calendar In Java, the code is as follows:
Next, let's look at the definition of internalget (field). As follows:
From this, we can see that get (int field) finally returns the value through internalget (int field). Internalget (int field) actually returns the field element in the field array. This corresponds to the 17 elements of calendar! In short, what we need to know is that calendar calculates "mm / DD / yyyy, hours, minutes and seconds" based on a time (milliseconds), so as to facilitate our operation of "mm / DD / yyyy, hours, minutes and seconds". Next, the related operation functions provided by calendar are described.
Part 3 calendar function interface 1 The 17 fields of calendar support the following public function interfaces. For examples of using these public interfaces, please refer to calendartest The testallcalendarsections() function in the java example. (1) Getmaximum (int field) function: get the maximum value of the field. Note "compare it with getactualmaximum()". Example: take the "month" field as an example. Usage:
To get the maximum value of other fields, you only need to replace the corresponding month in the example with other field names. (2) Getactualmaximum (int field) function: get the "maximum value of this field under the current date". Example: take the "month" field as an example. Usage:
To get the maximum value of other fields, you only need to replace the corresponding month in the example with other field names. Note: compare the difference between getactualmaximum () and getmaximum (). Referring to the following comparative example, the "field maximum value" obtained by a and getmaximum () refers to the "field maximum value" obtained from all these dates. For example, the purpose of getmaximum (calendar. Date) is to "get the maximum value of the day". Combining all the dates, the maximum number of days in a month is 31. Therefore, the return value of getmaximum (calendar. Date) is "31"! B. Getactualmaximum() gets the "maximum value of this field at the current date". For example, when the date is September 1, 2013, getactualmaximum (calendar. Date) is to get the "maximum value of day" is "30". The current date is September, which has only 30 days. Therefore, the return value of getactualmaximum (calendar. Date) is "30"! (3) Getminimum (int field) function: obtain the "minimum value of the field". Note "compare it with getactualminimum()". Example: take the "month" field as an example. Usage:
To get the minimum value of other fields, you only need to replace the corresponding month in the example with other field names. (4) getActualMinimum(int field)
To get the minimum value of other fields, you only need to replace the corresponding month in the example with other field names. Note: in the Java default calendar, although getminimum () and getactualminimum () have different meanings; However, their return values are the same. Because the default of calendar is to return the Gregorian calendar object, while in Gregorian calendar In Java, the return values of getminimum () and getactualminimum () are the same. (5) Get (int field) function: get the current value of the field. Gets the current value of the field. Example: take the "month" field as an example. The method of "get current value of month" is:
To get the current value of other fields, you only need to replace the corresponding month in the example with other field names. (6) Set (int field, int value) is used to set the current value of the field. Set the current value of the field to value. Example: take the "month" field as an example. The method of "setting the current value of month" is:
Note: A and 1988 are the current values of month to be set. This setting value must be an integer. B. To set the current value of other fields, you only need to replace the corresponding month in the example with other field names. (7) Add (int field, int value): adds a value to the current value of the field. Add value to the current value of the field. Example: take the "month" field as an example. The method is as follows:
Note: A, - 10 are added values. The added value can be positive or negative. A positive number increases the date and a negative number decreases the date. Suppose: now the value of CAL is "2013-09-01", now we increase the value of month field by - 10. The result is: "October 1, 2012". Why? Add - 10 to "September 1, 2013", that is, reduce the date by 10 months; The result is "2012-10-01". B. In the 17 fields of calendar: except rollback calendar ZONE_ During offset, an illegalargumentexception exception will be thrown; Other fields support this operation. C. To set the current value of other fields, you only need to replace the corresponding month in the example with other field names. (8) Roll (int field, int value) function: roll back the "current value of the field". Example: take the "month" field as an example. The method of "rolling back the current value of month" is:
Note: A, - 10 are rollback values. When the rollback value is negative, it means that the current field is rolled forward; When the rollback value is a positive number, it means that the current field is rolled back. When rolling back a field in calendar, do not change the larger field! This is the difference between roll() and add()! Add() may change larger fields, such as "modifying the 'month' field with add() may cause the 'year' field to change"; However, roll() will not change larger fields. For example, "using roll() to modify the 'month' field will not cause the 'year' field to change." Suppose: now the value of CAL is "2013-09-01", now we increase the value of month field by - 10. The result is: "October 1, 2013". Why? This is because "rollback" is "rolling back and forth between minimum and maximum". In this example, month is September, the previous rollback is 10, and the obtained value is October. However, roll() will not change the field larger than month, so the year field will not change. So the result is "October 1, 2013". B. In the 17 fields of calendar: except rollback calendar ZONE_ During offset, an illegalargumentexception exception will be thrown; Other fields support this operation. C. To set the current value of other fields, you only need to replace the corresponding month in the example with other field names. (9) Clear (int field) function: clear the current value of the field. The so-called emptying actually sets the value of "field" to 0; If the minimum value of field is 1, it is set to 1. Example: take the "month" field as an example. The method of "emptying month" is:
To clear other fields, just replace the corresponding month in the example with other field names. (10) Isset (int field) function: judge whether the "field" is set. If clear() is called to clear, the field will change to "no setting status". Example: take the "month" field as an example. The method of "judging whether month is set" is as follows:
To judge other fields, you only need to replace the corresponding month in the example with other field names.
2. Other functions of calendar (1) date comparison functionThe comparison functions of calendar mainly include the following:
For examples of using these functions, refer to calendartest The testcomparatorapis() function in the java example. Example: suppose cal1 and cal2 are both objects of calendar.
(2) Tolerance function
For examples of using these functions, refer to calendartest Testlenientapis() function in java example. Note: calendar has two modes to interpret calendar fields, namely lenient and non lenient. A. When calendar is in lenient mode, it can accept a wider range of values than the range of calendar fields it generates. When calendar recalculates the calendar field values so that they are returned by get (), all calendar fields are normalized. For example, Gregorian calendar in lenient mode will be month = = January, day_ OF_ Month = = 32 is interpreted as February 1. B. When calendar is in non lenient mode, it will throw an exception if there is any inconsistency in its calendar field. For example, Gregorian calendar always generates days between January and the length of the month_ OF_ Month value. If any out of range field value has been set, Gregorian calendar in non lenient mode will throw an exception when calculating the time or calendar field value. Note: the exception in step (02) will not be thrown when using set (), but only in functions such as get (), gettimeinmillis (), gettime (), add (), and roll (). Because set () only sets a modification flag, and get () and other methods will cause the recalculation of time, and an exception will be thrown at this time! (3) "Mm / DD / yyyy (H / min / s)", date, timezone, millisecond functions
For examples of using these functions, refer to calendartest The testtimeapis() function in the java example. (4) Other operations
For examples of using these functions, refer to calendartest Testotherapis() function in java example.
The fourth part is the calendar usage example. Next, let's learn to use the calendar API through the example. CalendarTest. The source code of Java is as follows:
The fifth part is an example of the custom calendar interface. These interfaces may be used when writing calendar programs. The source code is as follows (calendarselfdefinetest. Java):