1. Analysis:
(1) Keyboard entry for any year
(2) Set the date of the Calendar object
Year: That is (1) the data entered
Month: Yes 2 (March)
Day: Yes 1
(3) Push the time forward one day, the last day of February
(4) Output this day
2. code example:
1 Packagecn.itcast_03;2 3 ImportJava.util.Calendar;4 ImportJava.util.Scanner;5 6 /*7 * Get how many days in February of any year8 * 9 * Analysis:Ten * A: Keyboard entry for any year One * B: Set Calendar object's Month day A * Year is the data of a input - * Month is 2 - * Day is 1 the * C: Push the time forward one day, the last day of February - * D: Get this day's output - */ - Public classCalendartest { + Public Static voidMain (string[] args) { - //Keyboard entry for any year +Scanner sc =NewScanner (system.in); ASystem.out.println ("Please enter the year:"); at intYear =sc.nextint (); - - //set Calendar object's Month day -Calendar C =calendar.getinstance (); -C.set (year, 2, 1);//This is actually the March 1 of the year. - //Push the time forward one day, the last day of February inC.add (Calendar.date, 1); - to //get this day's output + System.out.println (C.get (calendar.date)); - } the}
The results are as follows:
Java Basics Hardening the 97:calendar class to get the number of days of February in any year of the case