这篇文章主要介绍“JAVA怎么求两个日期的差值”,在日常操作中,相信很多人在JAVA怎么求两个日期的差值问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JAVA怎么求两个日期的差值”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
import java.util.Scanner;
class fundemo6{
public static void main(String[] arg){
Scanner s=new Scanner(System.in);
System.out.println("Please input first date year:");
int y1=s.nextInt();
System.out.println("Please input first date month:");
int m1=s.nextInt();
System.out.println("Please input first date day:");
int d1=s.nextInt();
System.out.println("The first date you input is:"+y1+"-"+m1+"-"+d1);
System.out.println("Please input second date year:");
int y2=s.nextInt();
System.out.println("Please input second date month:");
int m2=s.nextInt();
System.out.println("Please input second date day:");
int d2=s.nextInt();
System.out.println("The second date you input is:"+y2+"-"+m2+"-"+d2);
System.out.println("sub days of two years is:"+subDays(y1,m1,d1,y2,m2,d2));
}
public static int getDays(int y,int m,int d){
int sum=0;
switch(m-1){
case 11:sum+=30;
case 10:sum+=31;
case 9:sum+=30;
case 8:sum+=31;
case 7:sum+=31;
case 6:sum+=30;
case 5:sum+=31;
case 4:sum+=30;
case 3:sum+=31;
case 2:if(isLeap(y))
sum+=29;
else
sum+=28;
case 1:sum+=31;
}
sum+=d;
return sum;
}
public static boolean isLeap(int y){
if(y%100!=0&&y%4==0||y%400==0)
return true;
return false;
}
public static int otherDays(int y,int m,int d){
if(isLeap(y))
return 366-getDays(y,m,d);
return 365-getDays(y,m,d);
}
public static int subDays(int y1,int m1,int d1,int y2,int m2,int d2){
if(y1==y1)
int day1=getDays(y1,m1,d1)-getDays(y2,m2,d2);
return abs(day1);
}
public static int abs(int n){
return n>=0?n:-n;
}
}
到此,关于“JAVA怎么求两个日期的差值”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!