Import Java.util.*;import java.text.*;class chinesedate{private Calendar aday; public chinesedate (int, Int. Month,int day) throws mydateexception{//this.aday= new Calendar ();//No, because Calendar is abstract class/ /this (); This.aday = Calendar.getinstance (); ※※※this.set (Year,month,day); } public Chinesedate () {this.aday = Calendar.getinstance (); } public void Set (int year, int month, Int. day) throws mydateexception{//throw exception//handle the legality of business logic if (year<=0 | | year>250 0) {throw new Mydateexception ("Year error, valid year: 0~2500. "); } if (Month<=0 | | month>12) {throw new Mydateexception ("Month error!"); } if (Day<=0 | | day>daysofmonth (year,month)) {throw new Mydateexception ("date error"); } this.aday.set (Year,month-1,day); ※※※} public static Boolean isleapyear (int year) {Boolean boo=year%400==0 | | year%100!=0&&year%4==0; return boo; } public boolean isleapyear () {return this.isleapyear (Aday.get (calendar.year)); } public static int daysofmonth (int year,inT month) {switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31; Case 4:case 6:case 9:case 11:return 30; Case 2:return isleapyear (year)? 29:28; Default:return 0; }}/*//※※※public int daysofmonth () {return Daysofmonth (Aday.get (calendar.year), Aday.get (Calendar.month)); }/Public String toString () {return new SimpleDateFormat ("yyyy mm month DD Day eeeee a hh mm min ss sec"). Format (The new Date (AD Ay.gettimeinmillis ())); } public static void Main (String args[]) {try{//exception, exception handling System.out.println (new Chinesedate (2015,1,23)); }catch (mydateexception e) {System.out.println ("AAAA----:" +e.tostring ()); if (e.tostring (). Equals ("Year error, valid year: 0~2500. ") {System.out.println (" the exception that was found to be faulted for the year, to be processed accordingly .... "); } if (E.tostring (). Equals ("Month error!")) {System.out.println ("the exception for the discovery of month error, to be processed accordingly ...."); } if (E.tostring () equals ("Date error")) {System.out.println ("Exception found on date error, process accordingly ...."); }}catch (Exception e) {E.printstackTrace (); } System.out.println ("I'm fine ..."); }}
Java------Exception Handling