compute Easter Day - Java java.util

Java examples for java.util:Calendar Holiday

Description

compute Easter Day

Demo Code

/**/*from w ww .j a va2s  .c  om*/
 * This file is part of JEMMA - http://jemma.energy-home.org
 * (C) Copyright 2013 Telecom Italia (http://www.telecomitalia.it)
 *
 * JEMMA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL) version 3
 * or later as published by the Free Software Foundation, which accompanies
 * this distribution and is available at http://www.gnu.org/licenses/lgpl.html
 *
 * JEMMA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License (LGPL) for more details.
 *
 */
//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        computeEasterDay();
    }

    private static int easterDay, easterMonth, mondayEasterDay,
            mondayEasterMonth;

    private static void computeEasterDay() {
        int year, golden, century, x, z, d, epact, n;

        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        golden = (year % 19) + 1; /* E1: metonic cycle */
        century = (year / 100) + 1; /* E2: e.g. 1984 was in 20th C */
        x = (3 * century / 4) - 12; /* E3: leap year correction */
        z = ((8 * century + 5) / 25) - 5; /* E3: sync with moon's orbit */
        d = (5 * year / 4) - x - 10;
        epact = (11 * golden + 20 + z - x) % 30; /* E5: epact */
        if ((epact == 25 && golden > 11) || epact == 24)
            epact++;
        n = 44 - epact;
        n += 30 * (n < 21 ? 1 : 0); /* E6: */
        n += 7 - ((d + n) % 7);
        if (n > 31) {/* E7: */
            easterDay = n - 31;
            mondayEasterDay = easterDay + 1;
            easterMonth = mondayEasterMonth = Calendar.APRIL;
        } else {
            easterDay = n;
            easterMonth = Calendar.MARCH;
            if (++n > 31) {
                mondayEasterDay = n - 31;
                mondayEasterMonth = Calendar.APRIL;
            } else {
                mondayEasterDay = easterDay + 1;
                mondayEasterMonth = Calendar.APRIL;
            }
        }
    }
}

Related Tutorials