Easter Date
/*
* Copyright (C) 2008 Marco Ratto
*
* This file is part of the project EasterJ2ME.
*
* EasterJ2ME is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* EasterJ2ME 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasterJ2ME; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//package it.openalmanac;
import java.util.Calendar;
import java.util.Date;
/**
*
* Questo algoritmo, sviluppato dal matematico tedesco Carl Friedrich Gauss,
* e fornisce direttamente la data della Pasqua. L'anno di cui si calcola la Pasqua
* sia contrassegnato da Y; mod ?'operatore modulo che restituisce il resto
* della divisione fra numeri interi (ad esempio, 13 mod 5 = 3 perch?3
* diviso 5 fa 2 con resto 3).
*
* Si calcolano dapprima a, b e c nel seguente modo:
*
* a = Y mod 19 b = Y mod 4 c = Y mod 7
*
* Poi si calcolano
*
* d = (19a + M) mod 30 e = (2b + 4c + 6d + N) mod 7
*
* Secondo il calendario giuliano si deve usare M = 15 e N = 6, mentre per il
* calendario gregoriano i valori di M and N variano a seconda degli anni
* considerati, secondo la seguente tabella:
*
* Anni M N 1583-1699 22 2 1700-1799 23 3 1800-1899 23 4 1900-2099 24 5
* 2100-2199 24 6 2200-2299 25 0 2300-2399 26 1 2400-2499 25 1
*
* Se d + e < 10, allora la Pasqua cade il giorno (d + e + 22) del mese di
* marzo, altrimenti si verificher?l (d + e - 9)-esimo giorno del mese di
* aprile.
*
* Si tenga tuttavia conto delle seguenti eccezioni:
*
* Se la data risultante dalla formula ?l 26 aprile, allora la Pasqua
* cadr?l giorno 19 aprile; Se la data risultante dalla formula ?l 25
* aprile e contemporaneamente d = 28, e = 6 e a > 10, allora la Pasqua cadr? * il 18 aprile.
*
* Esempio: Data della Pasqua 2007 secondo il calendario gregoriano, in uso in
* Italia (quindi M = 24, N = 5)
*
* a = 2007 mod 19 = 12 b = 2007 mod 4 = 3 c = 2007 mod 7 = 5
*
* d = (19 x 12 + 24) mod 30 = 12 e = (2 x 3 + 4 x 5 + 6 x 12 + 5) mod 7 = 5
*
* Siccome d + e = 12 + 5 = 17 > 10, allora nel 2007 Pasqua cadra' il (12 + 5 -
* 9) = 8 aprile.
*
* @author Marco Ratto
*
*/
public class Easter {
public final static boolean isEaster(Date date) throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int dateYMD = year * 10000 + calendar.get(Calendar.MONTH) * 100
+ calendar.get(Calendar.DAY_OF_MONTH);
Date easter = find(year);
calendar.setTime(easter);
int easterYMD = year * 10000 + calendar.get(Calendar.MONTH) * 100
+ calendar.get(Calendar.DAY_OF_MONTH);
return (easterYMD == dateYMD);
}
public final static Date find(int year) throws Exception {
if ((year < 1573) || (year > 2499)) {
throw new Exception("Year out of range [1753-2499]");
}
int a = year % 19;
int b = year % 4;
int c = year % 7;
int m = 0;
int n = 0;
if ((year >= 1583) && (year <= 1699)) {
m = 22;
n = 2;
}
if ((year >= 1700) && (year <= 1799)) {
m = 23;
n = 3;
}
if ((year >= 1800) && (year <= 1899)) {
m = 23;
n = 4;
}
if ((year >= 1900) && (year <= 2099)) {
m = 24;
n = 5;
}
if ((year >= 2100) && (year <= 2199)) {
m = 24;
n = 6;
}
if ((year >= 2200) && (year <= 2299)) {
m = 25;
n = 0;
}
if ((year >= 2300) && (year <= 2399)) {
m = 26;
n = 1;
}
if ((year >= 2400) && (year <= 2499)) {
m = 25;
n = 1;
}
int d = (19 * a + m) % 30;
int e = (2 * b + 4 * c + 6 * d + n) % 7;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
if (d + e < 10) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.MARCH);
calendar.set(Calendar.DAY_OF_MONTH, d + e + 22);
} else {
calendar.set(Calendar.MONTH, Calendar.APRIL);
int day = d + e - 9;
if (26 == day) {
day = 19;
}
if ((25 == day) && (28 == d) && (e == 6) && (a > 10)) {
day = 18;
}
calendar.set(Calendar.DAY_OF_MONTH, day);
}
return calendar.getTime();
}
}
Related examples in the same category
1. | Write Date to AudioTrack | | |
2. | Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel. | | |
3. | A formatter that formats dates to show the elapsed time relative to some base date. | | |
4. | Iso Date | | |
5. | Get the string of the date using format "yyyy-MM-dd HH:mm:ss" | | |
6. | Parse a string that contains date, return a date object using format "yyyy-MM-dd HH:mm:ss" | | |
7. | Create Date from timestamp | | |
8. | Convert date in RFC2822 and UTC strings, and to build Date from string of dates in RFC2822 and UTC format | | |
9. | Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date | | |
10. | Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date | | |
11. | Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date | | |
12. | Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date | | |
13. | Formatting and parsing the various date formats we expect to encounter. | | |
14. | ISO8601, ISO8601, RFC822 Date format | | |
15. | Parse Date | | |
16. | compare Two dates with Day value | | |
17. | compare Two dates with Second value | | |
18. | Get start of a date | | |
19. | Get start date of a Month | | |
20. | Get start date of a year | | |
21. | Create date from year, month and day value | | |
22. | Create Date from year, month, day, hour, minute, second | | |
23. | Get year value from Date | | |
24. | Get Month value from Date | | |
25. | Get Day value from Date | | |
26. | Get now in Date and Millis-seconds | | |
27. | Add day, Month and year to a Date | | |
28. | Parse date in format of yyyyMMddHHmmss or yyyyMMdd | | |
29. | String to Date | | |
30. | Convert date value in long to YYYYMMDDHHMMSS format | | |
31. | Convert Date to Number | | |
32. | Get short and long date String | | |
33. | Convert Java Date To Xml Time | | |
34. | Convert dates to Julian dates. | | |
35. | Parse an RFC 822 date string. | | |
36. | Format a date into a format suitable for SQLite | | |
37. | Dot String to Date | | |
38. | Parses an RFC822 formatted date string. | | |
39. | Formats a Date according to RFC822. | | |
40. | DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date); | | |
41. | yyyy-MM-dd HH:mm:ss date format | | |
42. | Utility class for formatting and parsing the various date formats we expect to encounter. | | |
43. | Get a string representation of a date as it relates to the current time. | | |
44. | get Date String from milliseconds | | |
45. | Generate a ISO 8601 date | | |
46. | Generate a Calendar from ISO 8601 date | | |
47. | parse Date for list of possible formats | | |
48. | date To String | | |
49. | Get date string for Locale tr | | |
50. | RFC3339 Date | | |
51. | Date formats | | |
52. | build Date Format day-of-week Short | | |
53. | Get end of each day | | |
54. | Get the end of each Month | | |
55. | Get end of a year | | |
56. | calculate Month Distance | | |
57. | calculate Day Distance | | |
58. | The month, and just the month, is zero-based. Add 1 for display. | | |
59. | Get hour different | | |
60. | format Millis Into Human Readable | | |