A summary example of using Java Calendar class
- 2021-07-06 11:03:53
- OfStack
In the actual project, we often involve the processing of time, such as landing on the website, we will see the website home page shows XXX, welcome! Today is XXXX. . . . Some websites will record the time when users log in, such as one of the bank's websites. For these problems that need to be dealt with frequently, Java provides Calendar, which is specially used for the operation of dates. So what's special about this class? First, let's look at the statement of Calendar
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>
This class is modified by abstract, which shows that the instance cannot be obtained by new. To this end, Calendar provides a class method getInstance to obtain a general object of this type, and getInstance method returns an Calendar object (this object is a subclass object of Calendar), whose calendar field has been initialized by the current date and time:
Calendar rightNow = Calendar.getInstance();
Why say return is Calendar subclass object, because each country has its own 1 set of calendar algorithm, for example, the first week in Western countries is mostly Sunday, while China is Monday, let's take a look at getInstance method to obtain the source code of the instance
/**
* Gets a calendar using the default time zone and locale. The
* <code>Calendar</code> returned is based on the current time
* in the default time zone with the default locale.
*
* @return a Calendar.
*/
public static Calendar getInstance()
{
Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
cal.sharedZone = true;
return cal;
}
The createCalendar method returns the corresponding date subclass according to different countries and regions
private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
Calendar cal = null;
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype == null) {
// Calendar type is not specified.
// If the specified locale is a Thai locale,
// returns a BuddhistCalendar instance.
if ("th".equals(aLocale.getLanguage())
&& ("TH".equals(aLocale.getCountry()))) {
cal = new BuddhistCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
} else if (caltype.equals("japanese")) {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else if (caltype.equals("buddhist")) {
cal = new BuddhistCalendar(zone, aLocale);
} else {
// Unsupported calendar type.
// Use Gregorian calendar as a fallback.
cal = new GregorianCalendar(zone, aLocale);
}
return cal;
}
In order to operate dates more conveniently, Calendar class provides some methods for converting calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc., and provides some methods for operating calendar fields (such as obtaining the date of next week). The moment can be expressed as a millisecond value, which is the offset from the epoch (that is, 00:00:00. 000 GMT on January 1, 1970, Gregorian calendar).
Let's take a look at the common methods of Calendar
package com.test.calendar;
import java.util.Calendar;
import org.junit.Before;
import org.junit.Test;
public class CalendarDemo {
Calendar calendar = null;
@Before
public void test() {
calendar = Calendar.getInstance();
}
// Basic usage, get year, month, day, hour, second and week
@Test
public void test1() {
// Acquisition year
int year = calendar.get(Calendar.YEAR);
// Get the month, where the range of required months is 0~11 Therefore, when getting the month, you need +1 Is the current month value
int month = calendar.get(Calendar.MONTH) + 1;
// Acquisition date
int day = calendar.get(Calendar.DAY_OF_MONTH);
// When getting
int hour = calendar.get(Calendar.HOUR);
// int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24 Hour representation
// Acquire points
int minute = calendar.get(Calendar.MINUTE);
// Get seconds
int second = calendar.get(Calendar.SECOND);
// Week, week in English-speaking countries is counted from Sunday
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(" Now it is " + year + " Year " + month + " Month " + day + " Day " + hour
+ " Hour " + minute + " Points " + second + " Seconds " + " Week " + weekday);
}
// 1 Today, years later
@Test
public void test2() {
// Similarly, change to today next month calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.YEAR, 1);
// Acquisition year
int year = calendar.get(Calendar.YEAR);
// Acquisition month
int month = calendar.get(Calendar.MONTH) + 1;
// Acquisition date
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("1 Today, years later: " + year + " Year " + month + " Month " + day + " Day ");
}
// Get any 1 The end of the month 1 Days
@Test
public void test3() {
// Hypothetical solution 6 The end of the month 1 Days
int currentMonth = 6;
// Find out first 7 The first of the month 1 God, actually here 6 Passed in for the outside currentMonth Variable
// 1
calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);
calendar.add(Calendar.DATE, -1);
// Acquisition date
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("6 The end of the month 1 Tianwei " + day + " No. ");
}
// Set Date
@Test
public void test4() {
calendar.set(Calendar.YEAR, 2000);
System.out.println(" Now it is " + calendar.get(Calendar.YEAR) + " Year ");
calendar.set(2008, 8, 8);
// Acquisition year
int year = calendar.get(Calendar.YEAR);
// Acquisition month
int month = calendar.get(Calendar.MONTH);
// Acquisition date
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(" Now it is " + year + " Year " + month + " Month " + day + " Day ");
}
}
Program output results:
1 It's November 7, 2016, 11:42:18, Tuesday
2 1 years later: November 7, 2017
The last day of March and June is the 30th
4 It's 2000
5 It is August 8, 2008
The Calendar class also has methods such as before, after, compareTo, and so on, which are used similarly to the Date class, except that the Calendar class is now recommended for operation dates