Home > Back-end >  I want to be written in Java for any number of days between two dates but always can't write ou
I want to be written in Java for any number of days between two dates but always can't write ou

Time:10-23

Package cn. CQSW;

import java.util.Scanner;

Public class java4 {

Public static void main (String [] args) {
//TODO Auto - generated method stub
Scanner sc=new Scanner (System. In);
System. The out. Println (" please input to calculate the distance of two years ");
System. The out. Println (" input in the first year of the year ");
Int nian=sc. NextInt ();
System. The out. Println (" input in the first year of ");
Int make=sc. NextInt ();
System. The out. Println (" the number of days the first year ");
Int ri=sc. NextInt ();
System. The out. Println (" input in the second year of the year ");
Int nian1=sc. NextInt ();
System. The out. Println (" enter the second year in ");
Int yue1=sc. NextInt ();
System. The out. Println (" a few days in the second year the number of ");
Int ri1=sc. NextInt ();
Int sum=0;
Sum=sum + ri1 + ri;
For (int I=1; I & lt; yue; I++) {
The switch (I) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
The sum +=31;
break;
Case 4:
Case 6:
Case 9:
Case 11:
The sum +=30;
break;
Case 2:
If (nian % 4==0 & amp; & Nian % 100!=0 | | nian %==0 400) {
The sum +=29;
} else {
The sum +=28;
}
}
If (nian % 4==0 & amp; & Nian % 100!=0 | | nian %==0 400) {
Sum=365 - sum;
} else {
Sum=366 - sum;
}
For (int i1=1; I1 & lt; Yue1; {i1 + +)
The switch (i1) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
The sum +=31;
break;
Case 4:
Case 6:
Case 9:
Case 11:
The sum +=30;
break;
Case 2:
If (nian1%4==0 & amp; & Nian1%100!=0 | | nian1 %==0 400) {
The sum +=29;
} else {
The sum +=28;
}
}

}
}
For (int I=nian; I & lt; Nian1; I++) {
If (I % 4==0 & amp; & I % 100!=0 | | I %==0 400) {
The sum +=366;
} else {
The sum +=365;
}
}
System.out.println(sum);
}
}


CodePudding user response:

Don't bother, use two Calendar, calculate their stay poor

CodePudding user response:

Directly into a millisecond, and then reduce the absolute value (so small can also reduce the large one, don't need to judge the time size), and then get the value of/1000/24/60/60 is days (remember to remove the whole), may not be the best method, but must have good understanding

CodePudding user response:

reference 1st floor qybao response:
don't bother, use two Calendar, calculate their days difference is

Now we only learned the if a for loop to harm
Can you teach me how to write??

CodePudding user response:

references a 3-yellow reply 2 floor:
directly into ms, and then reduce the take the absolute value (so small can also reduce the large one, don't need to judge the time size), then the resulting value/1000/24/60/60 days is rounded (remember), may not be the best method, but must have good understand

Oh listen to not understand well advanced

CodePudding user response:

//start time
The Date the Date=new Date ();
Long start_time=date. GetTime ();
Thread 2 days such as
Thread.sleep (1000 * 60 * 60 * 24 * 2);
The Date endDate=new Date ();
Long end_time=endDate. GetTime ();
//calculate milliseconds difference
Long sp=endTime - startTime;
//calculate day
Int day=sp/1000/60/60/24;
  • Related