Java Calendar class use summary and use example
- 2021-07-06 11:04:15
- OfStack
Usually, when browsing some websites, some websites will show the current time, such as xx minutes and xx seconds in xx year, xx month, xx day and xx. In the actual development process, the calculation of date and time will also be involved. Java provides a special class Calendar to deal with date and time.
Next, we will explain the usage scenarios and methods of Calendar class.
1. Get an Calendar instance
First, let's look at the definition of the Calendar class
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>
Because it is defined as abstract, we cannot create an instance of the Calendar class through new, but the Calendar class provides a class method getInstance () to return an instance of Calendar.
public static Calendar getInstance()
{
return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}
Thus, the code to get the Calendar instance is as follows:
Calendar calendar = Calendar.getInstance();
2. Get the current time
package com.zwwhnly.springbootdemo;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
// The month is subscript from the 0 Beginning, that is, 0~11 Respectively representing 1~12 Month, so it is necessary +1
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24 Hourly system
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(" Now it is: " + year + " Year " + month + " Month " + day + " Day " + hour + " Hour " + minute + " Points " + second + " Seconds ");
}
}
Run results:
Now it is: February 21, 2019 at 15:36:38
Note: The subscript of the month starts from 0, that is, 0 ~ 11 represents January ~ December respectively
Step 3 Set the time
Suppose we now need to set the time to 2019-02-21 23:59:59
3.1 (1 setup):
Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.FEBRUARY, 21, 23, 59, 59);
System.out.println(calendar.getTime());
3.2 (set separately):
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2019);
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 21);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
System.out.println(calendar.getTime());
The running results of 3.1 and 3.2 are as follows:
Thu Feb 21 23:59:59 CST 2019
4. Time calculation
4.1 Increased seconds
We add one second to 3.1, so the time should be 2019-02-22 00: 00:00
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.FEBRUARY, 21, 23, 59, 59);
calendar.add(Calendar.SECOND, 1);
System.out.println(calendar.getTime());
}
Run results:
Fri Feb 22 00:00:00 CST 2019
4.2 Increased months
First, we set the time to 2019-01-31, then add one month first, and then add two months
Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.JANUARY, 31);
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 1);
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 2);
System.out.println(calendar.getTime());
Run results:
Thu Jan 31 15:58:03 CST 2019
Thu Feb 28 15:58:03 CST 2019
Sun Apr 28 15:58:03 CST 2019
Note: When the month does not have that date, if there is no 31st in February, the last day of the month (February 28th) will be returned
5. Extension: Get the first and last days of a month
package com.zwwhnly.springbootdemo;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
System.out.println(getFirstDayOfMonth(2019, 2));
System.out.println(getLastDayOfMonth(2019, 2));
System.out.println(getFirstDayOfMonth(2019, 3));
System.out.println(getLastDayOfMonth(2019, 3));
}
public static String getLastDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
}
public static String getFirstDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
}
}
Run results:
2019-02-01
2019-02-28
2019-03-01
2019-03-31
6. Reference links
Common Methods of Calendar Class in Java
Summary of Java Class Calendar
java Time Class Date, Calendar and Their Usage