Calendar: It provides a way for a specific instant to transform between a set of calendar fields such as year, MONTH, Day_of_month, HOUR,
and provides some methods for manipulating calendar fields, such as getting the date of the next week.
1, public int get (int field): Returns the value of the given Calendar field. Each calendar field in the Calendar class is a static member variable and is of type int.
2, public void Add (int field,int amount): Operates on the current calendar based on the given calendar field and the corresponding time.
3, public final void set (int year,int month,int date): Sets the month and day of the current calendar
1 ImportJava.util.Calendar;2 3 Public classCalendarDome1 {4 5 Public Static voidMain (string[] args) {6 //its calendar field has been initialized by the current date and time: You must obtain a calendar using the default time zone and locale. 7Calendar time =calendar.getinstance ();8 9 /*Ten * Abstract class Person {public static person Getperson () {return new One * Student ();} } A * - * Class Student extends person { - * the * } - */ - - //public int get (int field): Returns the value of the given Calendar field. Each calendar field in the Calendar class is a static member variable and is of type int. + intYear =Time.get (calendar.year); - intmonth = Time.get (calendar.month) + 1;//month is calculated starting from 0 + intDay =Time.get (calendar.date); A inthour =Time.get (calendar.hour_of_day); at intminute =Time.get (calendar.minute); - intSecond =Time.get (calendar.second); -SYSTEM.OUT.PRINTLN (Year + "years" + month + "Month" + Day + "days" + Hour + "when" -+ Minute + "min" + Second + "SEC"); - -System.out.println ("---------------------"); in - //Public void Add (int field,int amount): Operates on the current calendar based on the given calendar field and the corresponding time. to + //3 years later, 2 days ago -Time.add (Calendar.year, 3); theTime.add (Calendar.day_of_month, 2); * $ //OutputPanax NotoginsengYear =Time.get (calendar.year); -month = Time.get (calendar.month) + 1;//month is calculated starting from 0 theDay =Time.get (calendar.date); +hour =Time.get (calendar.hour_of_day); Aminute =Time.get (calendar.minute); theSecond =Time.get (calendar.second); +SYSTEM.OUT.PRINTLN (Year + "years" + month + "Month" + Day + "days" + Hour + "when" -+ Minute + "min" + Second + "SEC"); $System.out.println ("---------------------"); $ - //Public final void set (int year,int month,int date): Sets the month and day of the current calendar - the //The month of November 1, 2020 will add 1 -Time.set (2020, 10, 1);WuyiYear =Time.get (calendar.year); themonth = Time.get (calendar.month) + 1;//month is calculated starting from 0 -Day =Time.get (calendar.date); Wuhour =Time.get (calendar.hour_of_day); -minute =Time.get (calendar.minute); AboutSecond =Time.get (calendar.second); $SYSTEM.OUT.PRINTLN (Year + "years" + month + "Month" + Day + "days" + Hour + "when" -+ Minute + "min" + Second + "SEC"); - } - A}
Requirements: Enter any one year, get the February of the year there are a few days (can be used to determine whether leap years)
Analysis:
A: Keyboard input the year you want to query
B: For calculation:
A: Time.set (year,2,1) can be used to set the specified calendar method;
: Sets the year for the query, and 2,1 is the March 1 of the year
B: Then with Time.add (Calendar.day_of_month,-1);
: March 1 minus 1 days of that year, the last day of February of that year
C: Output This day is the number of days in February of this year
1 ImportJava.util.Calendar;2 ImportJava.util.Scanner;3 Public classCalendartest {4 5 Public Static voidMain (string[] args) {6 //Create keyboard input7Scanner sc =NewScanner (system.in);8System.out.println ("Please enter the year you wish to inquire:");9 intYear =sc.nextint ();Ten One //get a calendar using the default time zone and locale. ACalendar time =calendar.getinstance (); - //get the March 1 of that year -Time.set (Year, 2,1); the //minus 1 days to get the last day of February -Time.add (Calendar.date, 1); - - //Output This day +System.out.println (year+ "February of the Year" +time.get (calendar.date) + "Day"); - } + A at}
Java 14-10 Calendar classes and exercises