日本祝日用java计算
时间: 2023-08-22 17:07:46 浏览: 156
计算机Java实习报告.docx
以下是一个使用Java计算日本祝日的示例代码:
```java
import java.time.LocalDate;
import java.time.Month;
public class JapaneseHolidays {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2021, Month.JANUARY, 1);
for (int i = 0; i < 12; i++) {
System.out.println(getHolidayName(date));
date = date.plusMonths(1);
}
}
public static String getHolidayName(LocalDate date) {
int year = date.getYear();
Month month = date.getMonth();
int day = date.getDayOfMonth();
switch (month) {
case JANUARY:
if (day == 1) {
return "元日";
} else if (year >= 2000 && day == 2) {
return "振替休日";
}
break;
case FEBRUARY:
if (day == 11) {
return "建国記念の日";
} else if (year >= 2000 && day == 12) {
return "振替休日";
}
break;
case MARCH:
if (day == getVernalEquinoxDay(year)) {
return "春分の日";
}
break;
case APRIL:
if (day == 29) {
return "昭和の日";
} else if (year >= 2000 && day == 30) {
return "振替休日";
}
break;
case MAY:
if (day == 3) {
return "憲法記念日";
} else if (day == 4) {
return "みどりの日";
} else if (day == 5) {
return "こどもの日";
} else if (year >= 2000 && day == 6 && date.getDayOfWeek().getValue() == 1) {
return "振替休日";
}
break;
case JULY:
if (day == 23) {
return "海の日";
} else if (year >= 2020 && day == 24) {
return "スポーツの日";
} else if (year >= 2000 && day == 20 && year != 2020) {
return "海の日";
} else if (year >= 2000 && day == 22 && date.getDayOfWeek().getValue() == 1 && year != 2020) {
return "振替休日";
}
break;
case AUGUST:
if (day == 8) {
return "山の日";
} else if (year >= 2016 && day == 11) {
return "山の日";
} else if (year >= 2000 && day == 10 && date.getDayOfWeek().getValue() == 1 && year != 2020 && year != 2016) {
return "振替休日";
}
break;
case SEPTEMBER:
if (day == getAutumnalEquinoxDay(year)) {
return "秋分の日";
}
break;
case OCTOBER:
if (day == 11) {
return "体育の日";
} else if (year >= 2000 && day == 12) {
return "振替休日";
}
break;
case NOVEMBER:
if (day == 3) {
return "文化の日";
} else if (day == 23) {
return "勤労感謝の日";
} else if (year >= 2000 && day == 4 && date.getDayOfWeek().getValue() == 1) {
return "振替休日";
}
break;
case DECEMBER:
if (day == 23) {
return "天皇誕生日";
} else if (year >= 1989 && month == Month.DECEMBER && day == 31) {
return "年末休暇";
}
break;
}
return "";
}
public static int getVernalEquinoxDay(int year) {
if (year <= 1947) {
return 0;
} else if (year <= 1979) {
return (int) (20.8357 + (0.242194 * (year - 1980)) - ((year - 1983) / 4));
} else if (year <= 2099) {
return (int) (20.8431 + (0.242194 * (year - 1980)) - ((year - 1980) / 4));
} else if (year <= 2150) {
return (int) (21.851 + (0.242194 * (year - 1980)) - ((year - 1980) / 4));
} else {
return 0;
}
}
public static int getAutumnalEquinoxDay(int year) {
if (year <= 1947) {
return 0;
} else if (year <= 1979) {
return (int) (23.2588 + (0.242194 * (year - 1980)) - ((year - 1983) / 4));
} else if (year <= 2099) {
return (int) (23.2488 + (0.242194 * (year - 1980)) - ((year - 1980) / 4));
} else if (year <= 2150) {
return (int) (24.2488 + (0.242194 * (year - 1980)) - ((year - 1980) / 4));
} else {
return 0;
}
}
}
```
这个代码可以计算从2021年1月1日开始的所有日本祝日,并将它们的名称打印到控制台上。要计算不同年份的祝日,只需更改代码中的年份即可。
阅读全文