gpt4 book ai didi

java - 日期差异java实现错误输出

转载 作者:行者123 更新时间:2023-11-30 01:44:11 25 4
gpt4 key购买 nike

我目前正在做一个日期差异函数,在这种情况下,我没有添加闰年函数,但对我来说,即使非闰年输出的日期差异也与实际日期差异不同5 至 8 天,具体取决于日期。

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 1999d2= 31 01 2019产出:6975天实际产量:6980天

最佳答案

选项 1:

如果您可以使用 Java Date 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 Date 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;

}

}

关于java - 日期差异java实现错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58775374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com