Calculate two days separated by the number of ideas: Suppose 1998-10-10 2010-5-5 first get 1889-10-10 This date how many days left in this year again get 2010-5-5 this day has been over in this year

Source: Internet
Author: User

Class FunDemo6

{

public static void Main (string[] args)

{

test function getDays

System.out.println (GetDays (1992,4,20));

System.out.println (Subdays (1999,1,5,2001,3,10));

}

determine if a leap year

public static Boolean isleap (int y)

{

if (y%4==0&&y%100!=0| | y%400==0)

return true;

Else

return false;

}

get how many days this year has passed

public static int getDays (int y,int m,int D)

{

int sum=0;

Switch (m-1) {

Case 11:sum=sum+30;

Case 10:sum=sum+31;

Case 9:sum=sum+30;

Case 8:sum=sum+31;

Case 7:sum=sum+31;

Case 6:sum=sum+30;

Case 5:sum=sum+31;

Case 4:sum=sum+30;

Case 3:sum=sum+31;

Case 2:if (Isleap (y))// a function called another function

sum+=29;// functions cannot nest declarations

Else

sum+=28;

Case 1:sum=sum+31;

}

return sum+d;

}

Write a function that calculates how many days are left in the year for this date

public static int otherdays (int y,int m,int D)

{

if (Isleap (y))

Return 366-getdays (Y,M,D);

else// can be omitted

Return 365-getdays (Y,M,D);

}

write a function directly to calculate the number of days separated by two dates

public static int subdays (int y1,int m1,int d1,int y2,int m2,int D2)

{

if (y1==y2)

{

int Days1=getdays (Y1,M1,D1)-getdays (Y2,M2,D2);

Return ABS (DAYS1);

}

else if (y1<y2)

{

Small year remaining + big year already over + Middle year

int sum1=otherdays (Y1,M1,D1);

int sum2=getdays (Y2,M2,D2);

int sum3=0;

for (int i=y1+1;i<y2;i++)

{

if (Isleap (i))

sum3+=366;

Else

sum3+=365;

}

return sum1+sum2+sum3;

}

when else//y2>y1

{

int sum1=otherdays (Y1,M1,D1);

int sum2=getdays (Y2,M2,D2);

int sum3=0;

for (int i=y2+1;i<y1;i++)

{

if (Isleap (i))

sum3+=366;

Else

sum+=365;

}

return sum1+sum2+sum3;

}

}

public static int abs (int n) {

Return n>=0?n:-n;

}

}

Calculate two days separated by the number of ideas: Suppose 1998-10-10 2010-5-5 first get 1889-10-10 This date how many days left in this year again get 2010-5-5 this day has been over in this year

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.