本文有两个小程序:输入两个年月日,该程序输出两时间的间隔天数。
第一个可以对用户输入的不恰当时间报错,第二个是第一个程序的简化版本,不会报错,只有用户输入正确的日期后才能得出正确的天数差。
日期天数差计算程序:
import java.util.Scanner;
public class Time3 {
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("Input the present time (Format:year(Press enter) month(Press Enter) day(Press Enter)):");
//Y1、M1、D1与Y2、M2、D2分别代表“当前年月日”和“先于当前的指定年月日”,为计算的需要,其中Y1、Y2定义为double类型
double Y1=in.nextInt();
int M1=in.nextInt();
while(M1<1||M1>12){
System.out.println("The "+M1+"th month dosen't exist,please try again");
M1=in.nextInt();}//确保输入的月份是1-12中整数
int D1=in.nextInt();
while(D1>MaxDay(Y1,M1)||D1<1){
System.out.println("The "+M1+"th month doesn't have the "+D1+"th day,please input the day again");
D1=in.nextInt();}//确保输入的日期在指定月份天数范围(函数MaxDay)内
System.out.println("Input the former time (Format:year(Press enter) month(Press Enter) day(Press Enter)):");
double Y2=in.nextInt();
while(Y2>Y1){
System.out.println("the former year can't be later than the present time,please try again");
Y2=in.nextInt();}
int M2=in.nextInt();
while(M2<1||M2>12){
System.out.println("The "+M2+"th month dosen't exist,please try again");
M2=in.nextInt();
if(Y2==Y1){
while(M2>M1){
System.out.println("the former month in the same year can't be later than the present month,please input the month again");
M2=in.nextInt();}
}//确保输入的月份是1-12中整数并且当同一年时,月份2(M2)要早于月份1(M1)
}
int D2=in.nextInt();
while(D2>MaxDay(Y2,M2)||D2<1){
System.out.println("The "+M2+"th month doesn't have the "+D2+"th day,please input the day again");
D2=in.nextInt();
if(M2==M1){
while(D2>D1){
System.out.println("the former day in the same month and year can't be later than the present day,please input the day again");
D2=in.nextInt();}}//确保输入的日期在指定月份天数范围(函数MaxDay)内,并且当同年同月时确保日期2(D2)早于日期1(D1)
}
in.close();
int m=(int)(F(Y1,M1,D1)-F(Y2,M2,D2));//F()计算两个不同时间与基准日期的距离,再计算它们的差,就是这两个时间的差
int Y1_int=(int)Y1;//类型转化,输出的Y1、Y2不含小数位
int Y2_int=(int)Y2;
//下面根据天数决定输出语句的单复数形式
if(m>1)
System.out.println("The time interval between "+Y1_int+"-"+M1+"-"+D1+" and "+Y2_int+"-"+M2+"-"+D2+" are "+m+" days");
else
System.out.println("The time interval between "+Y1_int+"-"+M1+"-"+D1+" and "+Y2_int+"-"+M2+"-"+D2+" is "+m+" day");
}
//F(x,y,z)返回"x年y月z日"与"公元0001年01月01日"的差值天数
public static double F(double year,int month,int day){
double Intervals_to_1_1_1;int i;int sum=0;
int []M={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0;i<month-1;i++)
sum=sum+M[i];
if(Judge_leap(year)&&month>2)
sum++;
Intervals_to_1_1_1=365*(year-1)+Math.floor((year-1)/4)-Math.floor((year-1)/100)+Math.floor((year-1)/400)+sum+day-1;
return Intervals_to_1_1_1;
}
//Judge——leap(year)函数判断year年是否是闰年
public static boolean Judge_leap(double year){
boolean leap;
leap=(year%400==0||(year%100!=0&&year%4==0)) ;
return leap;
}
//MaxDay(year,month)返回 year年year月的总天数
public static int MaxDay(double year,int month){
int []M={31,28,31,30,31,30,31,31,30,31,30,31};
if(Judge_leap(year)&&month==2)
return 29;
else
return M[month-1];
}
}
简化版本:
///这是一个简化版本,它要求用户输入两个【正确】的时间,才能正确的输出时间间隔天数
import java.util.Scanner;
public class Time_Simplified {
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("Input the present time (Format:year(Press enter) month(Press Enter) day(Press Enter)):");
double Y1=in.nextInt();
int M1=in.nextInt();
int D1=in.nextInt();
System.out.println("Input the former time (Format:year(Press enter) month(Press Enter) day(Press Enter)):");
double Y2=in.nextInt();
int M2=in.nextInt();
int D2=in.nextInt();
in.close();
int m=(int)(F(Y1,M1,D1)-F(Y2,M2,D2));
System.out.println(m+"day(s)");
}
public static double F(double year,int month,int day){
double Intervals_to_1_1_1;int i;int sum=0;
int []M={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0;i<month-1;i++)
sum=sum+M[i];
if((year%400==0||(year%100!=0&&year%4==0))&&month>2)
sum++;
Intervals_to_1_1_1=365*(year-1)+Math.floor((year-1)/4)-Math.floor((year-1)/100)+Math.floor((year-1)/400)+sum+day-1;
return Intervals_to_1_1_1;
}
}