1 PackageTraining;2 3 //import Java.util.Calendar;4 ImportJava.util.Scanner;5 /**6 * Title: Enter a certain day of the year, judging the day is the first of the year? 7 * 1. Procedure analysis: Take March 5 as an example, should first add up the first two months, and then add 5 days is the first day of the year, special cases, leap years and enter the month greater than 3 o'clock need to consider more than one day. 8 * @authorMorua9 * @version1.0Ten * One */ A - Public classTime11 { - the Public Static intGetTime (BooleanIsleap,intMonthintDaythrowsThrowable { - intSum=0; - - if(month>12| | month<1| | day>31| | Day<1) { + Throw NewException ("Day ERROR"); - } + A for(inti=1;i<=month;i++) { at Switch(i) {//1 3 5 7 8 --2 4 6 8 9 12+ - - Case1: -Sum+=0; - Break; - in Case2: - Case4: to Case6: + Case8: - Case9: the Case11: * if(day>30) { $ Throw NewException ("Day ERROR");Panax Notoginseng } -Sum+=31; the Break; + A Case3: the if(isleap) { +sum+=29; - } $ Else $Sum+=28; - Break; - the Case5: - Case7:Wuyi Case10: the Case12: -Sum+=30; Wu Break; - About default: $ Break; - } - } -sum+=Day ; A returnsum; + } the - Public Static BooleanIsleapyear (intYear ) { $ if((year%4==0&&year%100!=0) | | Year%400==0) the return true; the return false; the } the - Public Static voidMain (string[] args)throwsThrowable { in intYear ; the intmonth; the intDay ; About@SuppressWarnings ("Resource") theScanner Scanner =NewScanner (system.in); theYear=scanner.nextint (); theMonth=scanner.nextint (); +day=scanner.nextint (); - LongStartTime =System.currenttimemillis (); the Bayi Try { theSystem.out.println ("" +GetTime (Isleapyear (year), month, day)); the}Catch(Exception e) { - System.out.println (E.getmessage ()); - } the the the //Calendar calendar=calendar.getinstance (); the //Calendar.set (year,month,day);//Set calendar Month Day - //int Temp=calendar.get (calendar.day_of_year);//get the day of the year the //System.out.println ("The date you entered is the current year" + (temp) + "Day"); the the LongEndTime =System.currenttimemillis ();94System.out.println ("Time:" + (endtime-startTime)); the } the}
Only recently began to practice Java, writing is very slow, but want to write a write it well.
Add some judgment on the input, ensure the correctness of the input, and the security of the program.
The code inside the note was found on the web, but the runtime was incorrect, as if it were adding a total number of days in a month.
Calendar This object is free to see it again.
JAVA Title: Enter a certain day of the year, judging the day is the first of the year?