我正在使用高斯算法制作未来 10 年的复活节计算器。
除了几年之外,它似乎工作得很好。例如,它告诉您 2016 年的复活节将在 2016-03-27,但它将在 2016-05-01。与其他年份搭配效果很好。
这是我的代码:
public class EasterCalculator {
public static void main(String[] args) {
EasterCalculator obj = new EasterCalculator();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyy");
for (int i = 2016; i < 2026; i++) {
System.out.println("Easter in " + i + " will be on " + obj.getEasterDate(i).format(formatter));
System.out.println("Trinity in " + i + " will be on " + obj.getEasterDate(i).plusWeeks(7).format(formatter));
System.out.println();
}
}
public LocalDate getEasterDate(int year) {
int a = year % 19;
int b = year % 4;
int c = year % 7;
int k = year / 100;
int p = (13 + 8 * k) / 25;
int q = k / 4;
int M = (15 - p + k - q) % 30;
int N = (4 + k - q) % 7;
int d = (19 * a + M) % 30;
int e = (2 * b + 4 * c + 6 * d + N) % 7;
if (d == 29 && e == 6) {
return LocalDate.of(year, 3, 22).plusDays(d + e).minusDays(7);
} else
return LocalDate.of(year, 3, 22).plusDays(d + e);
}
}
Run Code Online (Sandbox Code Playgroud)