java求俩个时间间隔的天数

package zxd0001;
import java.util.Scanner;
public class A001 {
public static class Date{
public int year,month,day,sum,i1,i2,i3,flag;
public Date() {}
public Date(int year1, int month1 ,int day1) {year=year1;month=month1;day=day1;sum=0;}
public int DiffDays(Date d) {
i1=year;
i2=month;
i3=day;
int k;
k=month;
if(i1!=d.year) {
for(;i1<d.year;i1++)
{
if (i1==year) k=month;
else k=1;
if ((i1%4==0&&i1%100!=0)|(i1%400==0)){flag=1;}
else {flag=0;}
if(i1!=d.year) {
for(int j=k;j<=12;j++) {
if(j==1|j==3|j==5|j==7|j==8|j==10|j==12) {sum=sum+31;}
if(j==4|j==6|j==9|j==11) {sum=sum+30;}
if(flag==1&&j==2) {sum=sum+29;}
if(flag==0&&j==2) {sum=sum+28;}
}
}
}
}
if(i1==d.year) {
if ((i1%4==0&&i1%100!=0)|(i1%400==0)){flag=1;}
else {flag=0;}
for(int j=k;j<=d.month;j++) {
if(j!=d.month) {
if(j==1|j==3|j==5|j==7|j==8|j==10|j==12) {sum=sum+31;}
if(j==4|j==6|j==9|j==11) {sum=sum+30;}
if(flag==1&&j==2) {sum=sum+29;}
if(flag==0&&j==2) {sum=sum+28;}
}
else {
sum=sum+d.day-i3;
}
}
}
return sum;
}
}
public static void main(String args[]) {
int a,b,c;
Date d1,d2;
try{
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
a=Integer.parseInt(str1);
str1 = sc.next();
b=Integer.parseInt(str1);
str1 = sc.next();
c=Integer.parseInt(str1);
d1=new Date(a,b,c);
String str2 = sc.next();
a=Integer.parseInt(str2);
str2 = sc.next();
b=Integer.parseInt(str2);
str2 = sc.next();
c=Integer.parseInt(str2);
d2=new Date(a,b,c);
System.out.println("Daynum:"+d1.DiffDays(d2));
}catch(Exception e){
System.out.println("error");

} }
}

 

上一篇:js 获取传入月份的月初月末


下一篇:【Leetcode】NO.1185 一周中的第几天 (C++&Python) [每日一题]