Details and examples of Java Calendar class
- 2020-07-21 08:14:07
- OfStack
Summary of Java Calendar class usage
In actual projects, we often deal with time. For example, when we log in the website, we will see XXX displayed on the homepage of the website. Welcome! Today is the year of XXXX... Some websites record the time users log in, such as some websites of Banks. For these frequent problems, Java provides Calendar, a special class for operating on dates. What's special about this class
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>
This class is decorated by abstract to indicate that instances cannot be obtained by new. For this, Calendar provides a class method, getInstance, to obtain a generic object of this type. The getInstance method returns an Calendar object (which is a subclass of Calendar object) whose calendar field has been initialized by the current date and time:
Calendar rightNow = Calendar.getInstance();
Why is the return of Calendar subclass object, because each country has its own 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 the getInstance method to obtain the source code of the example
/**
* 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
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;
}
To make it easier to manipulate dates, the Calendar class provides methods for converting between calendar fields like YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and provides methods for manipulating calendar fields, such as getting next week's date. An instant can be represented by a millisecond value, which is the offset from the epoch (00:00:00.000, Greenwich Mean time, January 1, 1970, Gregorian calendar).
Here's a look at some common Calendar methods
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, minute, second week
@Test
public void test1() {
// For years
int year = calendar.get(Calendar.YEAR);
// Get month, where the range of required months is 0~11 , so you need to get the month +1 Is the current month value
int month = calendar.get(Calendar.MONTH) + 1;
// To obtain,
int day = calendar.get(Calendar.DAY_OF_MONTH);
// To obtain the
int hour = calendar.get(Calendar.HOUR);
// int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24 Hours said
// Access points
int minute = calendar.get(Calendar.MINUTE);
// For seconds
int second = calendar.get(Calendar.SECOND);
// Week, The Week in English-speaking countries begins on Sunday
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(" Now it is " + year + " years " + month + " month " + day + " day " + hour
+ " when " + minute + " points " + second + " seconds " + " week " + weekday);
}
// 1 Years from now
@Test
public void test2() {
// The same goes for today next month calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.YEAR, 1);
// For years
int year = calendar.get(Calendar.YEAR);
// For months
int month = calendar.get(Calendar.MONTH) + 1;
// To obtain,
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("1 Year: " + year + " years " + month + " month " + day + " day ");
}
// Access to any 1 At the end of the month 1 day
@Test
public void test3() {
// Assume that o 6 In the end of the 1 day
int currentMonth = 6;
// To find out first 7 In the first 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);
// To obtain,
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("6 The end of the month 1 Day for " + day + " No. ");
}
// Set the date
@Test
public void test4() {
calendar.set(Calendar.YEAR, 2000);
System.out.println(" Now it is " + calendar.get(Calendar.YEAR) + " years ");
calendar.set(2008, 8, 8);
// For years
int year = calendar.get(Calendar.YEAR);
// For months
int month = calendar.get(Calendar.MONTH);
// To obtain,
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(" Now it is " + year + " years " + month + " month " + day + " day ");
}
}
Program output results:
Now it is 2016 years 11 month 7 day 11 when 42 points 18 Second week 2
1 Year: 2017 years 11 month 7 day
6 The end of the month 1 Day for 30 No.
Now it is 2000 years
Now it is 2008 years 8 month 8 day
before, after, compareTo and other methods are also available in the Calendar class. The usage is similar to that of the Date class, except that the Calendar class is now recommended for date manipulation.
Thank you for reading, I hope to help you, thank you for your support to this site!