java time java.util.Calendar in depth analysis

  • 2020-06-12 09:01:33
  • OfStack

java.util.Calendar

There are several classes of time in Java, but as Date has been disabled and its methods have taken on a cross, the remaining functions have been implemented in Calendar, and GregorianCalendar, a subclass of Calendar, is too deeply involved in special calendars to be used normally. We can believe that Calendar class will be the mainstream time class in the future. Let's start with the details of Calendar class. If there are any mistakes, please correct me.

(1) Instantiation

Calendar class is an abstract class, which cannot be instantiated. Therefore, there are two methods for this class to obtain a calendar instance:


 Calendar calendar = Calendar.getInstance(TimeZone zone , Locale locale); 

By calling the getInstance method, select the default Timezone and Locale attributes to return 1 calendar. You can also add the parameters Timezone or Locale to select the location. The specific parameters need to see java.util.Timezone, java.util.Locale.

In addition, there is a method that can be instantiated. Not surprisingly, the old java routine is instantiated using subclasses. There's only one subclass of Calendar -- GregorianCalendar, which translates to Gregorian calendar, and we'll talk more about GregorianCalendar. The second way of instantiation is as follows:


Calendar calendar = new GregorianCalendar();

(2) Class variables

The variables in Calendar are basically defined by final, and these variables include all the time contents such as year, month, hour, last afternoon, etc. Baidu 1 to find a lot, this specific need to use the best API, Here I will briefly paste 1 copy:


calendar.get(Calendar.YEAR); 
calendar.get(Calendar.MONTH); //  In the month 0 start  
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);    // 12 hours 
calendar.get(Calendar.HOUR_OF_DAY); // 24 hours  
calendar.get(Calendar.MINUTE); 
calendar.get(Calendar.SECOND); 
calendar.get(Calendar.MILLISECOND); 

These values are final variables in the source code of jdk, since it is int static final modification, it means that these variables have the initial value of int type. Indeed, the Calendar class Numbers these variables in turn and determines the range of parameters passed in as functions. Then this may happen if 1 is not careful, such as the following code:


System.out.println(Calendar.DAY_OF_MOUTH);

The output is 5, although today is not the 5th of the month. If you want to represent the date of the current month, 1 must list the instance of the class as an object. However, this error is quite common in classes whose variables can be directly pointed out. The correct method should be the get() method (calendar is the object of our instance) :


System.out.println(calendar.get(Calendar.DAY_OF_MOUTH));

(3) compareTo() after() before()

compareTo (Calendar othercalendar), returns the value int, if the time of the object returns a number greater than 0 after the parameter time, otherwise returns a number less than 0, in particular, if the same time returns 0, I think the implementation of this method may directly return the number of milliseconds to make a difference. , the difference in milliseconds to make the return value.

after (Calendar othercalendar) and before (Calendar othercalendar), which are also easy to guess, return the value boolean, after() if the time returns a positive value after the argument, and before() if the time returns a positive value before the argument.


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 is as follows:


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()

In the above example, add(int field, int amount) is a powerful function that can add or subtract the value of the first parameter to change the value of the corresponding item in the calendar entity.


Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
System.out.println(calendar.getTime());// The output date is before the current date 1 God, everything else remains the same 

Es1010en (int field), there's nothing to say about this, just put the value you want to get in and display it, that's all. By the way, getTimeInMillis() returns the number of milliseconds, which is used a lot in practice.

The set() method has a number of parameter input methods. The setTime() function puts an Date object in it and returns a calendar set by the date of Date. The other special thing to note is that the month is calculated from 0, so if you set the month to 0 it's actually January, if you set it to 1 it's actually February, and the first day of the week is Sunday, and the seventh day is Saturday.


calendar.get(Calendar.DATE);
calendar.getTimeInMillis();

calendar.set(field, value);
calendar.set(year, month, date);// From the month 0 Here we go. Same as below 
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()

The getTime() function returns 1 time, roughly in this format


Sun Jan 11 21:19:49 GMT+08:00 1970

You can use the method of time formatting to become your favorite look, specifically look at my other blog, this function does not have too many slots. clear() function in the case of no parameters is to empty all variables in the object, after the empty time directly back to the original form, become


Calendar calendar = new GregorianCalendar();
0

clear() can also be appended with the parameter int field to indicate that this value is cleared alone:


Calendar calendar = new GregorianCalendar();
1

The last year shown in the above code is 1970. , and so on.
The isSet() method determines whether the calendar field has been set to a value. Some values will be set because the get method triggers the calculation. In many cases, once initialized, many values have been set.


Calendar calendar = new GregorianCalendar();
2

(6) Summary

The Calendar class, as its name implies, can implement a calendar, operate on it and have more complete functions. This class isn't necessarily much faster than new Date() if you just need 1 time, but there's a lot to learn about the details of 1.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: