LoginSignup
0
0

More than 5 years have passed since last update.

Zeller's 公式 (曜日を求める)

Posted at
public class Main {

    public static void main(String[] args) {
        System.out.println(calc(2018, 3, 28)); // 水曜日
        System.out.println(calc(2018, 2, 28)); // 水曜日
        System.out.println(calc(2018, 1, 28)); // 日曜日
    }

    public static String calc(
            double y,
            double m,
            double d) {

        double M = 0;
        if (m < 3) {
            M = m + 12;
            y = y - 1;
        } else {
            M = m;
        }

        int C = (int) (y / 100);
        int Y = (int) (y % 100);

        double R = 0;

        if (y >= 1582) {
            R = (C / 4) - C - C;
        } else if (y >= 4) {
            R = 5 - C;
        } else {
            throw new IllegalArgumentException("year >= 4");
        }

        Double h = (d + ((26 * (M + 1)) / 10) + Y + (Y / 4) + R) % 7;

        String[] weekday = new String[] {
                "土",
                "日",
                "月",
                "火",
                "水",
                "木",
                "金"
        };
        return weekday[h.intValue()];

    }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0