简体   繁体   中英

Sorting leap year not working

I have these following criteria's.

January and February dates in leap years: subtract 1 from step 5 Dates in the 1600s: add 6 to step 5 Dates in the 1700s: add 4 to step 5 Dates in the 1800s: add 2 to step 5 Dates in the 2000s: add 6 to step 5 Dates in the 2100s: add 4 to step 5

for some reason my Leap year condition doesn't work

        if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) && month == FEBURARY || month == JANURARY){
                localYear6 = localMonth - 1;
            }

         else if(year >= 1600 && year <= 1699){
             localYear6 = localMonth + 6;
         }
         else if(year >= 1700 && year <= 1799 ){
             localYear6 = localMonth + 4;
         }
         else if(year >= 1800 && year <= 1899){
             localYear6 = localMonth + 2;
         }
         else if(year >= 2000 && year <= 2099 ){
             localYear6 = localMonth + 6;
         }
         else if(year >= 2100 && year <= 2199 ){
             localYear6 = localMonth + 4;
         }

         else{
             localYear6 = localMonth;
         }

Considering the problem statement

January and February dates in leap years: subtract 1 from step 5 Dates

means that the dates (of a leap year) which have month either January or February, your logic needs an extra parenthesis -

if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
   if(month == FEBURARY || month == JANURARY) {
       localYear6 = localMonth - 1;
   }
}

OR simply

if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) && (month == FEBURARY || month == JANURARY)) {
    localYear6 = localMonth - 1;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM