[英]Date difference java implement with wrong output
我目前正在做一个日期差异 function 并且在这种情况下,我没有为闰年添加 function 但它倾向于我,即使不是闰年的日期差异 Z78E6221F6393D14566881ZD 与实际 DB39 不同天数取决于日期。
import java.util.Scanner;
public class Date_Difference {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int d1 = myObj.nextInt();
int m1 = myObj.nextInt();
int y1 = myObj.nextInt();
int d2 = myObj.nextInt();
int m2 = myObj.nextInt();
int y2 = myObj.nextInt();
System.out.println(get_Difference(d1,m1,y1,d2,m2,y2));
}
static int get_Difference(int d1, int m1, int y1, int d2, int m2, int y2 ) {
int n1 = y1 * 365 + d1;
int i;
for (i = 0; i < m1 - 1; i++) {
n1 += monthDays_notleapyear[i];
}
int n2 = y2 * 365 + d2;
for (i = 0; i < m2 - 1; i++) {
n2 += monthDays_notleapyear[i];
}
return Math.abs(n2 - n1);
}
static int monthDays_notleapyear[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
}
输入:d1=22 12 1999 d2=31 01 2019 output:6975 天实际 output:6980 天
选项1:
如果你可以使用 Java 日期 API 和 Java 8,你可以很容易地做到这一点。
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Date_Difference {
public static void main(String[] args) {
long daysBetween = ChronoUnit.DAYS.between(LocalDate.of(1999, 12, 22), LocalDate.of(2019, 01, 31));
System.out.println(daysBetween);
}
}
选项 2:如果您不应该使用 Java 日期 API,那么下面是一种间接计算方式。
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class FSD {
static int monthDays_notleapyear[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int d1 = myObj.nextInt();
int m1 = myObj.nextInt();
int y1 = myObj.nextInt();
int d2 = myObj.nextInt();
int m2 = myObj.nextInt();
int y2 = myObj.nextInt();
long daysBetween = ChronoUnit.DAYS.between(LocalDate.of(y1, m1, d1), LocalDate.of(y2, m2, d2));
System.out.println("Using Java Date: " + daysBetween);
get_Difference(d1, m1, y1, d2, m2, y2);
}
static int get_Difference(int d1, int m1, int y1, int d2, int m2, int y2) {
// int n1 = y1 * 365 + d1;
int yearDiff = y2 - y1 - 1;
int totalDays = yearDiff * 365;
int numberOfLeapDays = numberOfLeapYearBetween(y1, y2);
// Check if (m1 == 2 and d1 <=29 ) or if m1 == 1, increment by 1.
if ((m1 <= 2 && d1 <= 29) || m1 == 1) {
boolean isStartLeapYear = isLeapYear(y1);
if (isStartLeapYear) {
numberOfLeapDays++;
}
}
// Check if m2 is greater than 3. If yes, check if leap year, increment by 1.
if (m2 >= 3) {
boolean isEndLeapYear = isLeapYear(y2);
if (isEndLeapYear) {
numberOfLeapDays++;
}
}
int remainingDays = 0;
for (int i = m1; i <= 12; i++) {
if (i == m1) {
remainingDays = remainingDays + (monthDays_notleapyear[i - 1] - d1);
} else {
remainingDays = remainingDays + monthDays_notleapyear[i - 1];
}
}
for (int i = m2; i > 0; i--) {
if (i == m2) {
remainingDays = remainingDays + d2;
} else {
remainingDays = remainingDays + monthDays_notleapyear[i - 1];
}
}
totalDays = totalDays + numberOfLeapDays + remainingDays;
System.out.println("Total Days: " + totalDays);
return totalDays;
}
private static boolean isLeapYear(int year) {
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
// year is divisible by 400, hence the year is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
} else
leap = true;
} else
leap = false;
return leap;
}
/**
* Get Number of Leap Years excluding startYear and endYear.
*
* @param startYear
* @param endYear
* @return
*/
private static int numberOfLeapYearBetween(int startYear, int endYear) {
int counter = 0;
while (startYear < endYear - 1) {
startYear++;
boolean isLeapYear = isLeapYear(startYear);
if (isLeapYear) {
counter++;
}
}
return counter;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.