這篇文章主要介紹“JAVA怎么求兩個(gè)日期的差值”,在日常操作中,相信很多人在JAVA怎么求兩個(gè)日期的差值問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”JAVA怎么求兩個(gè)日期的差值”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)頭屯河免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。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;
}
}
到此,關(guān)于“JAVA怎么求兩個(gè)日期的差值”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!