Java日期算法写出代码考虑全面,闫年问题、年未问题、月未问题等
时间: 2023-06-09 14:02:23 浏览: 114
。好的,我会为您编写Java日期算法的代码,考虑到了闰年问题、年月问题以及月未问题。下面是代码示例:
```java
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateAlgorithm {
public static void main(String[] args) {
int year = 2022;
int month = 2;
int day = 25;
System.out.println(getNextDay(year, month, day));
}
public static String getNextDay(int year, int month, int day) {
Calendar calendar = new GregorianCalendar(year, month - 1, day);
calendar.add(Calendar.DATE, 1);
int nextYear = calendar.get(Calendar.YEAR);
int nextMonth = calendar.get(Calendar.MONTH) + 1;
int nextDay = calendar.get(Calendar.DAY_OF_MONTH);
if (nextMonth == 2 && nextDay == 29) {
if ((nextYear % 4 == 0 && nextYear % 100 != 0) || nextYear % 400 == 0) {
return nextYear + "-" + nextMonth + "-" + nextDay;
} else {
return nextYear + "-" + 3 + "-" + 1;
}
} else if (nextMonth == 4 || nextMonth == 6 || nextMonth == 9 || nextMonth == 11) {
if (nextDay == 31) {
return nextYear + "-" + (nextMonth + 1) + "-" + 1;
}
} else {
if (nextDay == 32) {
return nextYear + "-" + (nextMonth + 1) + "-" + 1;
}
}
return nextYear + "-" + nextMonth + "-" + nextDay;
}
}
```
希望能够满足您的需求。如果您有其他问题,请随时提出。
阅读全文