public static void main(String[] args) {
String star="2020-02-03";
String end="2025-03-02";
String[] star1=star.split("-");
String[] end1=end.split("-");
int days=0;
if(Integer.parseInt(star1[0])<Integer.parseInt(end1[0])){
for(int i=Integer.parseInt(star1[0]);i<Integer.parseInt(end1[0]);i++){
//计算是否是瑞年
if(i%4==0&&i%100!=0||i%400==0){
days+=366;
}else{
days+=365;
}
}
}
//得到开始那一年已过去的日期
int starday=days(star1[0],star1[1],star1[2]);
//得到结束那一年已过去的日期
int endday=days(end1[0],end1[1],end1[2]);
//减去开始那一年已过去的日期,加上结束那一年已过去的日期
days=days-starday+endday;
System.out.println("相差的天数:"+days);
}
public static int days(String year,String month,String day){
int days=0;
int nowyear=Integer.parseInt(year);
int[] monthday={0,31,28,31,30,31,30,31,31,30,31,30,31};
int[] monthday1={0,31,29,31,30,31,30,31,31,30,31,30,31};
boolean flag=true;
if(nowyear%4==0&&nowyear%100!=0||nowyear%400==0){
}else{
flag=false;
}
for(int i=0;i<Integer.parseInt(month);i++){
if(flag){
days+=monthday1[i];
}else{
days+=monthday[i];
}
}
days+=Integer.parseInt(day);
return days;
}
|