在接下来的十年中,我将使用高斯算法制作复活节星期日计算器。
除了几年,它似乎工作正常。例如,它告知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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句