1: Calendar Class
- Calendar comes from Java.util package
- Calendar is an abstract class (abstract classes cannot be instantiated, but can only be subclassed)
- Calendar is created through Calendar.getInstance()
Relevant codes:
Calendar rightNow = Calendar.getInstance();
Screenshot of official documents:
Related learning links:
On whether abstract classes can be instantiated
Official API document Calendar Class
2, Calendar class, common attributes, methods
2.1 constants
The following figure shows the constants commonly used by the Calendar Class: public static final data type constant name
The following figure comes from rookie programming: Java date and time
2.2 setting and obtaining calendar class object information
2.3 setting of calendar class object information
The setting of Calendar object includes two parts: 1. Modify the year, month, day, hour, minute and second of Calendar through set(). 2: add() is used to add and subtract the Calendar time on the original basis.
1.set
All overload methods of the official AP for set() are as follows:
void set(int field, int value) //Sets the given calendar field to the given value. void set(int year, int month, int date) //Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. void set(int year, int month, int date, int hourOfDay, int minute) //Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE. void set(int year, int month, int date, int hourOfDay, int minute, int second) //Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND. void setFirstDayOfWeek(int value) //Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France. void setLenient(boolean lenient) //Specifies whether or not date/time interpretation is to be lenient. void setMinimalDaysInFirstWeek(int value) //Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that //contains the first day of the first month of a year, call this method with value 1. void setTime(Date date) //Sets this Calendar's time with the given Date. void setTimeInMillis(long millis) //Sets this Calendar's current time from the given long value. void setTimeZone(TimeZone value) //Sets the time zone with the given time zone value. void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) //Sets the date of this Calendar with the given date specifiers - week year, week of year, and day of week.
Code example:
1.1 use public final void set(int year,int month,int date) to modify year, month and day
Operation effect:
1.2 use void set (int year, int month, int date, int hourfday, int minute, int second) to modify year, month, day, hour, minute and second
c1.set(2009, 6, 12,20,32,30);// Set the year, day, hour, minute and second of Calendar object C1 as 2009, 6, 12, 20, 32 and 30 respectively
1.3 use field type settings:
If you only set the value of a field, such as a date, you can use the following set method:
public void set(int field,int value)
Set the date represented by the c1 object to the 10th, and all other values will be recalculated
c1.set(Calendar.DATE,10);
Set the year represented by c1 object to 2008, and all other values will be recalculated
c1.set(Calendar.YEAR,2008);
The meaning of the other field property set, and so on
2.add
The add() method of the official AP is as follows:
abstract void add(int field, int amount) //Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
Add 10 to the date of c1 object, that is, c1 is expressed as the date after 10 days, and all other values will be recalculated
c1.add(Calendar.DATE, 10);
Subtract 10 from the date of c1 object, that is, c1 is expressed as the date 10 days ago, and all other values will be recalculated
c1.add(Calendar.DATE, -10);
The meaning of add of other field properties, and so on
2.4 acquisition of calendar class object information
The Calendar class object information is obtained through get()
Note: the month starts from 0 (0 represents January), so the obtained month needs to be added with 1. The week starts from Sunday, so 1 represents Sunday, 2 represents Monday, 3 represents Tuesday... 7 means Saturday)
Calendar c1 = Calendar.getInstance(); // Year of acquisition int year = c1.get(Calendar.YEAR); // Get month int month = c1.get(Calendar.MONTH) + 1; // Date obtained int date = c1.get(Calendar.DATE); // Get hours int hour = c1.get(Calendar.HOUR_OF_DAY); // Get minutes int minute = c1.get(Calendar.MINUTE); // Get seconds int second = c1.get(Calendar.SECOND); // Get the day of the week (note (this is different from the Date class): 1 for Sunday, 2 for Monday, 3 for Tuesday, and so on) int day = c1.get(Calendar.DAY_OF_WEEK);
3, GregorianCalendar class
3.1 Gregorian calendar related concepts
- Gregorian calendar comes from the Java.util package
- Gregorian Calendar is a subclass of Calendar (it inherits the subclass of the abstract class and implements all abstract methods in the abstract class)
- Provides a standard calendar system used in most countries in the world
Previous Calendar rightNow = Calendar.getInstance(); Method returns a GregorianCalendar object initialized with the current locale and time zone by default. If you want to operate on time in different countries / time zones, you need to use GregorianCalendar.
3.2 Gregorian calendar construction method
1. Several construction methods of GregorianCalendar objects are listed below:
3.3 Gregorian calendar method
Adds the specified (signed) amount of time to the given calendar field according to the rules of the calendar.
void add(int field, int amount)
Converts the time value (millisecond offset time) to the calendar field value.
protected void computeFields()
Convert calendar field values to time values (from times)
protected void computeTime()
Compare Gregorian calendar to the specified Object
boolean equals(Object obj)
Gets the default locale in Gregorian calendar. ZonedDateTime object.
static GregorianCalendar from(ZonedDateTime zdt)
Returns the maximum value that this calendar field may have, taking into account the given time value and the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange, and getTimeZone methods.
int getActualMaximum(int field)
Returns the minimum value that this calendar field may have, taking into account the given time value and the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange, and getTimeZone methods.
int getActualMinimum(int field)
Return "gregory" as the calendar type.
String getCalendarType()
The maximum or minimum value of the given calendar field. Gregorian calendar, for example.
int getGreatestMinimum(int field)
Gets the Gregorian calendar change date.
Date getGregorianChange()
The minimum or maximum value of the given calendar field. Gregorian calendar, for example.
int getLeastMaximum(int field)
The maximum value of the given calendar field for. Gregorian calendar, for example.
int getMaximum(int field)
The minimum value of the given calendar field for. Gregorian calendar, for example.
int getMinimum(int field)
Number of weeks in the. The anniversary represents this Gregorian calendar
int getWeeksInWeekYear()
The return anniversary represents this Gregorian calendar
int getWeekYear()
Hash code for. GregorianCalendar object.
int hashCode()
Determines whether a given year is a leap year.
boolean isLeapYear(int year)
Return true indicates the number of weeks GregorianCalendar supports.
boolean isWeekDateSupported()
Add or subtract (up / down) a time unit from a given time field without changing the larger field.
void roll(int field, boolean up)
Adds a signed amount to the specified calendar field without changing the larger field.
void roll(int field, int amount)
Set the Gregorian calendar change date.
void setGregorianChange(Date date)
Set this Gregorian calendar to the specified date - weekYear, weekOfYear, and dayOfWeek
void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
Convert this object to ZonedDateTime, which represents the same point on the timeline as the following. GregorianCalendar.
ZonedDateTime toZonedDateTime()
3.4 code examples
import java.util.*; public class GregorianCalendarDemo { public static void main(String[] args) { String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; int year; // Initialize Gregorian calendar // Use current time and date // The default is local time and time zone GregorianCalendar gcalendar = new GregorianCalendar(); // Displays information about the current time and date System.out.print("Date: "); System.out.print(months[gcalendar.get(Calendar.MONTH)]); System.out.print(" " + gcalendar.get(Calendar.DATE) + " "); System.out.println(year = gcalendar.get(Calendar.YEAR)); System.out.print("Time: "); System.out.print(gcalendar.get(Calendar.HOUR) + ":"); System.out.print(gcalendar.get(Calendar.MINUTE) + ":"); System.out.println(gcalendar.get(Calendar.SECOND)); // Test whether the current year is a leap year if(gcalendar.isLeapYear(year)) { System.out.println("The current year is a leap year"); } else { System.out.println("The current year is not a leap year"); } } }
4, SimpleDateFormat
You can define the format of time through SimpleDateFormat. The constructor parameter is the format (for example, yyyy year mm month dd day kk point mm minute ss second SSS millisecond, where yyyy will be replaced with the year minute of the specific time (time in format(date)), corresponding to MM represents month, dd represents day, kk represents hour, mm represents minute, ss represents second, SSS represents millisecond).
Code example:
@Test public void test() { Calendar calendar = Calendar.getInstance(); calendar.set(2016, 11, 30, 24, 59, 59); Date date = new Date(calendar.getTimeInMillis()); String pattern = "G yyyy year MM month dd day HH spot mm branch ss second SSS millisecond"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day kk spot mm branch ss second SSS millisecond"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day hh spot mm branch ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day KK spot mm branch ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G YYYY year MM month dd day KK spot mm branch ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 Year w week"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 December 2012 W week"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 Year D day"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 December 2012 d day"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 On December 31, 2016, it was in the second position in December 2016 F week"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "E"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "2016 December 31, is the fourth day of the week u day"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Atlantic Standard Time: z"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Z"; System.out.println("RFC822 Time zone:" + new SimpleDateFormat(pattern).format(date)); pattern = "X"; System.out.println("ISO8601 Time zone:" + new SimpleDateFormat(pattern).format(date)); }
Related learning links:
How to use SimpleDateFormat in Java