Java教程

java求俩个时间间隔的天数

本文主要是介绍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");

} }
}

 

这篇关于java求俩个时间间隔的天数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!