📄 date.java
字号:
/**输入两个年份求出它们间的天数*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Date {
public static void main(String args[]){
try{BufferedReader input=new BufferedReader
(new InputStreamReader(System.in));
int year1,month1,day1;
System.out.println("输入年份:");
year1=Integer.parseInt(input.readLine());
if(year1<0)throw new InputException();
System.out.println("输入月份:");
month1=Integer.parseInt(input.readLine());
if(month1>12)throw new MonthException();
System.out.println("输入号数:");
day1=Integer.parseInt(input.readLine());
if(bigmonth(month1)&day1>31)throw new DayException();
else if(smallmonth(month1)&day1>30)throw new DayException();
else if(leap(year1)&month1==2&day1>29 )throw new DayException();
else if(!leap(year1)&month1==2&day1>28)throw new DayException ();
System.out.println("输入天数:") ;
int m=Integer.parseInt(input.readLine());
String s=getdate(year1,month1,day1,m);
System.out.println(s);
System.out.println("输入另一组日期:") ;
int year2,month2,day2;
System.out.println("输入年份:");
year2=Integer.parseInt(input.readLine());
if(year2<0)throw new InputException();
System.out.println("输入月份:");
month2=Integer.parseInt(input.readLine());
if(month2>12)throw new MonthException();
System.out.println("输入号数:");
day2=Integer.parseInt(input.readLine());
if(bigmonth(month2)&day2>31)throw new DayException();
else if(smallmonth(month2)&day2>30)throw new DayException();
else if(leap(year2)&month2==2&day2>29 )throw new DayException();
else if(!leap(year2)&month2==2&day2>28)throw new DayException ();
int n=days(year1,month1,day1,year2,month2,day2);
System.out.printf("%i 年 %i 月 %i 日 与 %i 年 %i 月 %i 日相隔 %i 天。\n ",
year1,month1,day1,year2,month2,day2,n) ;
}catch(MonthException e1){
System.out.println(e1.getMessage());
System.exit(0);
}catch(DayException e2){
System.out.println(e2.getMessage());
System.exit(0);
}catch(Exception e3){
System.out.printf("ERROR!");
}}
public static boolean bigmonth(int n){
switch(n){
case 1: case 3:case 5:case 7:case 8:case 10:case 12:return true;
default:return false;
}
}public static boolean smallmonth(int n){
switch(n){
case 4: case 6:case 9:case 11:return true;
default:return false;
}
}public static boolean leap(int i){
boolean leapyear;
if(i%400==0|(i%100!=0&i%4==0))
leapyear=true;
else leapyear=false;
return leapyear;
}public static int getday(int year,int month ,int day){
int sum=0,sum1=0,sum2=0,sum3=0;int n=0;
int[] a=new int[12];
a[0]=a[2]=a[4]=a[6]=a[7]=a[9]=a[10]=a[11]=31;
a[3]=a[5]=a[8]=a[10]=30;
sum1=365*(year-1);
for(int i=1;i<year;i++)if(leap(i))n++;
if(leap(year))a[1]=29;
for(int j=0;j<month;j++)sum2+=a[j];
sum3=n;
sum=sum1+sum2+sum3;
return sum;
}public static int days(int year1,int month1,int day1,
int year2,int month2,int day2){
int m=getday(year1,month1,day1);
int n=getday(year2,month2,day2);
return Math.abs(m-n);
}public static String getdate(int year,int month,int day,int m){
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -