How to Calculate Easter With Java
According to Christians, the Easter holiday commemorates the date of the resurrection of Jesus Christ from the dead. It can be difficult to calculate since the date depends on the date of Passover. Eastern Christians (Eastern Orthodox) and Western Christians (Roman Catholics and most Protestant sects) calculate the date differently. The given Java program provides the date according to Western tradition.
Instructions
-
-
1
Open a Java Integrated Development Environment (IDE). This tutorial will assume the Netbeans IDE that comes with the official Java SDK is being used, but others are possible.
-
2
Click "File" and "New class." Name the class "EasterCalculator" when prompted.
-
-
3
Ensure that the following code is in the class, and paste it into the class if it is not:
public class EasterCalculator {
public static void main(String[] args) {
}
}
-
4
Add a "calculate" method between the line "EasterCalculator" and "main":
public static Calendar calculate(int year) {
int remain = year % 19;
int firstDigits = year / 100;
int temp = (firstDigits - 15) / 2 + 202 - 11 * remain;
switch (firstDigits) {
case 21:
case 24:
case 25:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 34:
case 35:
case 38:
temp--;
break;
case 33:
case 36:
case 37:
case 39:
case 40:
temp--;
temp--;
break;
}
temp %= 30;
int tA = temp + 21;
if (temp == 29) {
tA--;
}
if (temp == 28 && remain > 10) {
tA--;
}
int tB = (tA - 19) % 7;
int tC = (40 - firstDigits) % 4;
if (tC == 3) {
tC++;
}
if (tC > 1) {
tC++;
}
temp = year % 100;
int tD = (temp + temp / 4) % 7;
int tE = ((20 - tB - tC - tD) % 7) + 1;
int day = tA + tE;
int month = Calendar.MARCH;
if (day > 31) {
day -= 31;
month = Calendar.APRIL;
}
Calendar easterDate = Calendar.getInstance();
easterDate.set(year, month, day);
return easterDate;
}
-
5
Paste the following within the main method:
public static void main(String[] args) {
Calendar easter2011 = EasterCalculator.calculate(2011);
String easterMonthString = easter2011.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
String date = DateFormat.getDateInstance(DateFormat.SHORT).format(easter2011.getTime());
System.out.println("Easter in 2011 will be on " + date);
}
-
1
Tips & Warnings
This algorithm for calculating Easter according to the Western tradition is valid only for years between 1583, when the Gregorian calendar was introduced, and 4099. It also assumes that there will be no significant changes in the calendar during that time or in the method used to calculate the date of Easter.
References
- Photo Credit Jupiterimages/Photos.com/Getty Images