English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.Calendar
There are several time classes in Java, but as Date is gradually deprecated, its methods are slowly marked with crosses, and the remaining functions that can be used are already implemented in Calendar. The subclass GregorianCalendar of Calendar is too specialized for the study of special calendars, and we will not use this subclass in daily life. We can believe that the Calendar class will be the mainstream time class in the future. Let's take a detailed look at the Calendar class together, and everyone is welcome to correct any errors.
(I) Instantiation
The Calendar class is an abstract class and cannot be instantiated. There are two methods to get a calendar instance from this class:
Calendar calendar = Calendar.getInstance(TimeZone zone, Locale locale);
By calling the getInstance method, you can choose the default Timezone and Locale attributes to return a calendar. You can also add parameters Timezone or Locale to choose the geographical location. For specific parameters, please refer to the java.util.Timezone and java.util.Locale packages. Generally, the default time is the common time, and we actually do not need to change it.
In addition, there is another method that can be instantiated. It's nothing special, just the old Java routine using subclasses for instantiation. Calendar has only one subclass, which is GregorianCalendar, which means Gregorian calendar in English. We will talk about GregorianCalendar in detail later. The second way to instantiate is as follows:
Calendar calendar = new GregorianCalendar();
(II) Class Variables
Most of the variables in Calendar are defined with the final keyword, which includes all time elements such as year, month, hour, AM/PM, etc. You can find a lot of information on Baidu, but it's best to refer to the API when you need to use it. I will briefly paste a part here for reference:
calendar.get(Calendar.YEAR); calendar.get(Calendar.MONTH); // The month starts from 0 calendar.get(Calendar.DAY_OF_MONTH); calendar.get(Calendar.DAY_OF_WEEK); calendar.get(Calendar.WEEK_OF_YEAR); calendar.get(Calendar.WEEK_OF_MONTH); calendar.get(Calendar.HOUR); // 12hour calendar.get(Calendar.HOUR_OF_DAY); // 24hour calendar.get(Calendar.MINUTE); calendar.get(Calendar.SECOND); calendar.get(Calendar.MILLISECOND);
These values are all final variables in the JDK source code. Since they are int static final, it means that these variables all have an initial int value. Indeed, the Calendar class numbers these variables sequentially as some function parameters for range judgment. Therefore, it is possible to make such a mistake accidentally, for example, the following code:
System.out.println(Calendar.DAY_OF_MONTH);
The output is5Although it is not the date of this month today,5This is actually an error. In fact, what you output is the initial value of DAY_OF_MONTH in this class.5If you want to represent the date of the current month, you must instantiate the class object, but in the class variable, it is still common to make this kind of mistake. The correct method should use the get() method to obtain (calendar is the instance object):
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
(3) compareTo(), after(), before() functions
compareTo(Calendar othercalendar) returns an int value. If the object's time is after the parameter time, it returns a number greater than 0. Otherwise, it returns a number less than 0. Particularly, if the time is the same, it returns 0. I think the implementation of this method may directly return the difference in milliseconds (I feel like my guess is very reasonable...), and the difference in milliseconds is used as the return value.
The functions after(Calendar othercalendar) and before(Calendar othercalendar) are also easy to guess. They return boolean values. The after() function returns a positive value if the time is after the parameter, and the before() function returns a positive value if the time is before the parameter.
Calendar calendar = Calendar.getInstance(); Calendar calendarother = Calendar.getInstance(); calendarother.add(Calendar.DATE, -20); if(calendar.after(calendarother)) System.out.println("after"); calendarother.add(Calendar.DATE, 100); if(calendar.before(calendarother)) System.out.println("before"); if(calendar.compareTo(calendarother)>0) System.out.println(calendar.getTime()+">"+calendarother.getTime());
The output result is:
after before Sun Jan 11 21:19:49 GMT+08:00 1970>Thu Jan 01 00:00:00 GMT+08:00 1970
(4) get() add() set() setTime() functions
In the above example, the function add(int field, int amount) appeared, which is a powerful function that can add or subtract the value of the first parameter to modify the value of the corresponding item in this calendar entity.
Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); System.out.println(calendar.getTime());//The output date is one day before the current date, and all others remain unchanged
get(int field) has nothing much to say, just put the value to be obtained in and display it, that's all. By the way, getTimeInMillis() returns milliseconds, and this millisecond is used quite a lot in practical applications.
The set() method has many parameter input modes, it is easy to understand if it is written well, the setTime() function takes a Date object and returns a calendar set according to the date of the Date. Another point that needs special attention is that the month starts from 0, setting the month to 0 is actually January, and setting1Actually it is February, the1It is Sunday, the7It is Saturday.
calendar.get(Calendar.DATE); calendar.getTimeInMillis(); calendar.set(field, value); calendar.set(year, month, date);//The month starts from 0, the same as follows calendar.set(year, month, date, hourOfDay, minute); calendar.set(year, month, date, hourOfDay, minute, second); calendar.setTime(Date date);//Date Object
(5) getTime() clear() isSet() Functions
The getTime() function returns a time, which is roughly in this format
Sun Jan 11 21:19:49 GMT+08:00 1970
You can use the time formatting method to change it to the style you like, see my other blog for details, this function also has few drawbacks. The clear() function in the absence of parameters clears all variables in the object, and the time directly returns to its original form, becoming
Thu Jan 01 00:00:00 GMT+08:00 1970
The clear() method can also be accompanied by the parameter int field, indicating that only this value is cleared:
calendar.clear(Calendar.YEAR); System.out.println(calendar.getTime());
The year displayed at the end of the above code is1970 years (it is impossible to clear it to 0000...), and the rest can be inferred in the same way.
The isSet() method determines whether the calendar field has been set to a value, some values may be set due to the calculation triggered by the get method. Many times, many values have been set just after initialization, but as a function that returns a boolean value, we still believe that it will play a role in detection.
if(calendar.isSet(Calendar.DATE))
(6) Summary
The Calendar class, as the name implies, can implement a calendar, operate on it, and has relatively complete functions. If you just need a time, this class may not be much faster than new Date(), but there are still many details worth learning from.
Thank you for reading, I hope it can help everyone, thank you for your support to our website!