Algorithm Note _154: Algorithm improves date calculation (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem Descriptionknown November 11, 2011 is Friday, ask yyyy mm month DD Day is the days of the week? Note Consider the case of a leap year. In particular, every hundred years do not leap, the 400-year leap in the situation. Input FormatEnter only one row
YYYY MM DDoutput Formatoutput has only one row
Wdata size and conventions1599 <= YYYY <= 2999
1 <= MM <=
1 <= DD <= 31, and ensure that the test sample yyyy mm month DD Day is a reasonable date
1 <= W <= 7, respectively, representing Monday to SundaySample Input one by oneSample Output5

2 Solutions

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public BooleanJudgeyear (intYear ) {        if(Year% 400 = = 0 | | (year% 4 = = 0 && Year% 100! = 0))            return true;//This is a leap year        return false;//This is common year    }         Public intGetMonth (intYearintMonth) {//get the number of months        if(Month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | |Month= = 8 | | Month = = 10 | | month = = 12)            return31; if(Month = = 4 | | month = = 6 | | month = = 9 | | month = = 11)            return30; if(Judgeyear (year) && month = = 2)            return29; if(!judgeyear (year) && month = = 2)            return28; return0; }         Public voidGetResult (String date) {string[] temp= Date.split (""); intYear = integer.valueof (temp[0]); intmonth = integer.valueof (temp[1]); intDay = integer.valueof (temp[2]); intCountday = 0; intresult = 0;//Final Result        if(Year > 2011) {             for(inti = 1;i < month;i++) Countday+=GetMonth (year, i); Countday+=Day ;  for(inti = year-1;i > 2011;i--) {//when jumping out of the loop i =                if(Judgeyear (i)) Countday+ = 366; ElseCountday+ = 365; } countday= Countday + 19 + 31;//plus November 2011 remaining days and December 31 daysresult = 5 + countday% 7; if(Result! = 7) Result= result% 7; } Else if(Year < 2011) {Countday= Countday + (GetMonth (year, month)-Day ); Month++;  for(; month <= 12;month++) Countday+=GetMonth (year, month);  for(inti = year + 1;i < 2011;i++) {//when jumping out of the loop i =                if(Judgeyear (i)) Countday+ = 366; ElseCountday+ = 365; }             for(inti = 1;i <= 10;i++) Countday+ = GetMonth (2011, i); Countday+ = 11; Result= 5-countday% 7; if(Result = = 0) Result= 7; Else if(Result = =-1) Result= 6; Else if(Result = =-2) Result= 5; } Else if(Year = = 2011) {            if(Month > 11) {Countday= Countday + 19 +Day ; } Else if(Month = = 11) {                if(Day >= 11) {Countday= day-11; } Else if(Day < 11) Countday= 11-Day ; } Else if(Month < 11) {Countday+ = GetMonth (, month)-Day ; inti = month + 1;  for(; I <= 10;i++) Countday+ = GetMonth (2011, i); Countday+ = 11; }            if(Month >= && Day >= 11) {result= 5 + countday% 7; if(Result! = 7) Result= result% 7; } Else{result= 5-countday% 7; if(Result = = 0) Result= 7; Else if(Result = =-1) Result= 6; Else if(Result = =-2) Result= 5;    }} System.out.println (Result); }         Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); String Date=In.nextline ();    Test.getresult (date); }}

Algorithm Note _154: Algorithm improves date calculation (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.