Skip to content

Commit dfcb12d

Browse files
authored
JewishDate - first part of PR 272
- getLastDayOfGregorianMonth and getDaysInJewishMonth - change parameter order to year, month, matching the rest of the class - Make isJewishLeapYear public - avoid repeatedly subtracting one from year in calculations
1 parent 9df03e1 commit dfcb12d

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

‎src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java‎

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,15 @@ public int getMoladChalakim() {
301301
private int gregorianAbsDate;
302302

303303
/**
304-
* Returns the number of days in a given month in a given month and year.
304+
* Returns the number of days in a given month in a given year and month.
305305
*
306-
* @param month
307-
* the month. As with other cases in this class, this is 1-based, not zero-based.
308306
* @param year
309307
* the year (only impacts February)
308+
* @param month
309+
* the month. As with other cases in this class, this is 1-based, not zero-based.
310310
* @return the number of days in the month in the given year
311311
*/
312-
private static int getLastDayOfGregorianMonth(int month, int year) {
312+
private static int getLastDayOfGregorianMonth(int year, int month) {
313313
return YearMonth.of(year, month).lengthOfMonth();
314314
}
315315

@@ -325,7 +325,7 @@ private static LocalDate absDateToDate(int absDate) {
325325
}
326326

327327
int month = 1; // Search forward month by month from January
328-
while (absDate > gregorianDateToAbsDate(year, month, getLastDayOfGregorianMonth(month, year))) {
328+
while (absDate > gregorianDateToAbsDate(year, month, getLastDayOfGregorianMonth(year, month))) {
329329
month++;
330330
}
331331

@@ -357,13 +357,15 @@ public int getAbsDate() {
357357
private static int gregorianDateToAbsDate(int year, int month, int dayOfMonth) {
358358
int absDate = dayOfMonth;
359359
for (int m = month - 1; m > 0; m--) {
360-
absDate += getLastDayOfGregorianMonth(m, year); // days in prior months of the year
360+
absDate += getLastDayOfGregorianMonth(year, m); // days in prior months of the year
361361
}
362-
return (absDate // days this year
363-
+ 365 * (year - 1) // days in previous years ignoring leap days
364-
+ (year - 1) / 4 // Julian leap days before this year
365-
- (year - 1) / 100 // minus prior century years
366-
+ (year - 1) / 400); // plus prior years divisible by 400
362+
363+
int y = year - 1;
364+
return (absDate // days this year
365+
+ 365 * y // days in previous years ignoring leap days
366+
+ y / 4 // Julian leap days before this year
367+
- y / 100 // minus prior century years
368+
+ y / 400); // plus prior years divisible by 400
367369
}
368370

369371
/**
@@ -374,7 +376,7 @@ private static int gregorianDateToAbsDate(int year, int month, int dayOfMonth) {
374376
* @return true if it is a leap year
375377
* @see #isJewishLeapYear()
376378
*/
377-
private static boolean isJewishLeapYear(int year) {
379+
public static boolean isJewishLeapYear(int year) {
378380
return ((7 * year) + 1) % 19 < 7;
379381
}
380382

@@ -473,15 +475,16 @@ private static int addDechiyos(int year, int moladDay, int moladParts) {
473475
* @return the number of <em>chalakim</em> (parts - 1080 to the hour) from the original hypothetical <em>Molad Tohu</em>
474476
*/
475477
private static long getChalakimSinceMoladTohu(int year, int month) {
476-
// Jewish lunar month = 29 days, 12 hours and 793 chalakim
477-
// chalakim since Molad Tohu BeHaRaD - 1 day, 5 hours and 204 chalakim
478-
int monthOfYear = getJewishMonthOfYear(year, month);
479-
int monthsElapsed = (235 * ((year - 1) / 19)) // Months in complete 19-year lunar (Metonic) cycles so far
480-
+ (12 * ((year - 1) % 19)) // Regular months in this cycle
481-
+ ((7 * ((year - 1) % 19) + 1) / 19) // Leap months this cycle
482-
+ (monthOfYear - 1); // add elapsed months till the start of the molad of the month
483-
// return chalakim prior to BeHaRaD + number of chalakim since
484-
return CHALAKIM_MOLAD_TOHU + (CHALAKIM_PER_MONTH * monthsElapsed);
478+
int y = year - 1;
479+
// Jewish lunar month = 29 days, 12 hours and 793 chalakim
480+
// chalakim since Molad Tohu BeHaRaD - 1 day, 5 hours and 204 chalakim
481+
int monthOfYear = getJewishMonthOfYear(year, month);
482+
int monthsElapsed = (235 * (y / 19)) // Months in complete 19-year lunar (Metonic) cycles so far
483+
+ (12 * (y % 19)) // Regular months in this cycle
484+
+ ((7 * (y % 19) + 1) / 19) // Leap months this cycle
485+
+ (monthOfYear - 1); // add elapsed months till the start of the molad of the month
486+
// return chalakim prior to BeHaRaD + number of chalakim since
487+
return CHALAKIM_MOLAD_TOHU + (CHALAKIM_PER_MONTH * monthsElapsed);
485488
}
486489

487490
/**
@@ -672,13 +675,13 @@ public int getCheshvanKislevKviah() {
672675
/**
673676
* Returns the number of days of a Jewish month for a given month and year.
674677
*
675-
* @param month
676-
* the Jewish month
677678
* @param year
678679
* the Jewish Year
680+
* @param month
681+
* the Jewish month
679682
* @return the number of days for a given Jewish month
680683
*/
681-
private static int getDaysInJewishMonth(int month, int year) {
684+
private static int getDaysInJewishMonth(int year, int month) {
682685
if ((month == IYAR) || (month == TAMMUZ) || (month == ELUL) || ((month == CHESHVAN) && !(isCheshvanLong(year)))
683686
|| ((month == KISLEV) && isKislevShort(year)) || (month == TEVES)
684687
|| ((month == ADAR) && !(isJewishLeapYear(year))) || (month == ADAR_II)) {
@@ -694,7 +697,7 @@ private static int getDaysInJewishMonth(int month, int year) {
694697
* @return the number of days for the Jewish month that the calendar is currently set to.
695698
*/
696699
public int getDaysInJewishMonth() {
697-
return getDaysInJewishMonth(getJewishMonth(), getJewishYear());
700+
return getDaysInJewishMonth(getJewishYear(), getJewishMonth());
698701
}
699702

700703
/**
@@ -813,14 +816,14 @@ private static int getDaysSinceStartOfJewishYear(int year, int month, int dayOfM
813816
if (month < TISHREI) {
814817
// this year before and after Nissan.
815818
for (int m = TISHREI; m <= getLastMonthOfJewishYear(year); m++) {
816-
elapsedDays += getDaysInJewishMonth(m, year);
819+
elapsedDays += getDaysInJewishMonth(year, m);
817820
}
818821
for (int m = NISSAN; m < month; m++) {
819-
elapsedDays += getDaysInJewishMonth(m, year);
822+
elapsedDays += getDaysInJewishMonth(year, m);
820823
}
821824
} else { // Add days in prior months this year
822825
for (int m = TISHREI; m < month; m++) {
823-
elapsedDays += getDaysInJewishMonth(m, year);
826+
elapsedDays += getDaysInJewishMonth(year, m);
824827
}
825828
}
826829
return elapsedDays;
@@ -956,9 +959,9 @@ public synchronized void setJewishDate(int year, int month, int dayOfMonth, int
956959

957960
validateJewishDate(year, month, dayOfMonth, hours, minutes, chalakim);
958961

962+
jewishYear = year;
959963
jewishMonth = month;
960964
jewishDay = dayOfMonth;
961-
jewishYear = year;
962965
moladHours = hours;
963966
moladMinutes = minutes;
964967
moladChalakim = chalakim;
@@ -1188,6 +1191,7 @@ public void minusYears(int years, boolean useAdarAlephForLeapYear){
11881191
* @return the Jewish date in the form "day Month, year" e.g. "21 Shevat, 5729"
11891192
* @see HebrewDateFormatter#format(JewishDate)
11901193
*/
1194+
@Override
11911195
public String toString() {
11921196
return new HebrewDateFormatter().format(this);
11931197
}
@@ -1259,6 +1263,7 @@ public int getDayOfWeek() {
12591263
*
12601264
* @see Object#clone()
12611265
*/
1266+
@Override
12621267
public Object clone() {
12631268
JewishDate clone = null;
12641269
try {
@@ -1279,6 +1284,7 @@ public Object clone() {
12791284
* Overrides {@link Object#hashCode()}.
12801285
* @see Object#hashCode()
12811286
*/
1287+
@Override
12821288
public int hashCode() {
12831289
return Integer.hashCode(gregorianAbsDate);
12841290
}

0 commit comments

Comments
 (0)