1 Public classDemo9_calendar {2 3 /*4 * A:calendar is an abstract class5 * B: Member Method6 * public static Calendar getinstance ()7 * public int get (int field)8 * C: Member Method9 * public void Add (int field, int amount) to add or subtract the specified field forward or backwardTen * Public final void set (int year, int month, int date) to modify the specified field One */ A Public Static voidMain (string[] args) { - - //demo1 (); theCalendar C =calendar.getinstance (); -C.add (Calendar.month,-1);//month minus 1 -C.set (Calendar.year, 2088);//year modified to 2088 -System.out.println (C.get (calendar.year) + "year" + Getnum (C.get (calendar.month) + 1) + "Month" ++ Getnum (C.get (calendar.day_of_month)) + "Day" +Getweek (C.get (Calendar.day_of_week))); - + A } at - Public Static voiddemo1 () { -Calendar C = calendar.getinstance ();//equivalent to parent class reference to child class object - System.out.println (C.get (calendar.year)); -System.out.println (C.get (calendar.month));//the month is starting from 0 - System.out.println (C.get (Calendar.day_of_month)); inSystem.out.println (C.get (Calendar.day_of_week));//Sunday is the first day -System.out.println (C.get (calendar.year) + "year" + Getnum (C.get (calendar.month) + 1) + "Month" to+ Getnum (C.get (calendar.day_of_month)) + "Day" +Getweek (C.get (Calendar.day_of_week))); + } - the /** * * Check the table in the weekly storage table $ * @paramWeekPanax Notoginseng * @return - */ the Public StaticString Getweek (intweek) { +String[] arr = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; A returnArr[week]; the } + - /** $ * @paramNum $ * @returnString - */ - Public StaticString Getnum (intnum) { the /*if (num <) { - return "0" + num;Wuyi }else { the return "" + num; - }*/ Wu returnnum > 9? "+ num:" 0 "+num; - } About}
Use the Calendar class to calculate whether a year is a leap years:
1 /*2 * Given a year, determine if it is leap years3 * 1. Prompt for keyboard input year4 * 2. Create a Calendar object5 * 3. Set the date by the set method to the March 1 of which year6 * 4. The day minus 17 * 5. Determine whether the day is 29 and get the result8 */9 Public Static voidMain (string[] args) {Ten OneScanner sc =NewScanner (system.in); ASystem.out.println ("Please enter the year you want to judge:"); - //int year = Sc.nextint (); - //System.out.println (GetYear (year)); the - //optimized to determine if the input is a 4-bit numeric string - intYear = 0; -String s =sc.nextline (); +String regex = "\\d{4}"; - if(S.matches (regex)) { +Year =Integer.parseint (s); A System.out.println (GetYear (year)); at -}Else { -SYSTEM.OUT.PRINTLN ("Wrong input! "); - } - - } in - Public Static BooleanGetYear (intYear ) { toCalendar C =calendar.getinstance (); +C.set (year, 2, 1); -C.add (Calendar.day_of_month, 1); the System.out.println (C.get (Calendar.day_of_month)); * returnC.get (calendar.day_of_month) = = 29; $}
The Calendar class for Java