使用 Gauss 算法的 2016 年 Java 复活节计算器 [英] Java Easter calculator for 2016 using the Gauss algorithm

查看:26
本文介绍了使用 Gauss 算法的 2016 年 Java 复活节计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Gauss 算法在未来 10 年制作复活节周日计算器.

I'm making Easter Sunday calculator using the Gauss algorithm for the next 10 years.

它似乎工作正常,除了几年.例如,它告诉 2016 年复活节将在 2016-03-27,但它将在 2016-05-01.它适用于其他年份.

It seems to work fine except several years. For example it tells that in 2016 Easter will be on 2016-03-27, but it will be on the 2016-05-01. It works fine with other years.

这是我的代码:

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);
}
}

推荐答案

根据 this这个 复活节周日是 2016 年 3 月 27 日.我还测试了您的代码,它似乎有效.我不知道我是否误解了你的问题,但在我看来,高斯仍然是对的.

According to this and this Easter Sunday is on 2016-03-27. I also tested your code and it seems to work. I do not know if I misunderstood your question, but it seems to me, that Gauss is still right.

经过一些研究和作者的评论,我了解到西方复活节和正统复活节不一定在同一天(参见 此处).如您所见,有几个匹配项,但大部分都没有.由于高斯是德国数学家,因此他似乎为西方复活节开发了算法.

after some research and the comment of the author, I learned that the western Easter and the orthodox Easter are not necessarily on the same date (see here). As you can see, there are several matches but mostly not. As Gauss was a German mathematician, it appears that he developed the algorithm for western Easter.

这篇关于使用 Gauss 算法的 2016 年 Java 复活节计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆