Create DateCalculation.java

This commit is contained in:
xixu-me committed 2024-05-24 16:04:45 +08:00
1 parent 2af341fcdc
commit eec518632e
1 file changed
+26
+26
View File
@@ -0,0 +1,26 @@
package finalexam.StandardIO;
import java.util.Scanner;
public class DateCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] arr = s.split("[-]");
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);
int days[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year))
days[2] = 28;
int ans = day;
for (int i = 1; i < month; i++) {
ans += days[i];
}
System.out.println(ans);
}
public static boolean isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
}