Skip to content

Commit 0fc105b

Browse files
pnemonic78Moshe Waisberg
authored and
Moshe Waisberg
committed
calendar constants
1 parent 4a15861 commit 0fc105b

File tree

9 files changed

+132
-149
lines changed

9 files changed

+132
-149
lines changed

src/main/java/com/kosherjava/zmanim/hebrewcalendar/HebrewDateFormatter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -758,15 +758,15 @@ public String formatMonth(JewishDate jewishDate) {
758758
final int month = jewishDate.getJewishMonth();
759759
if (isHebrewFormat()) {
760760
if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
761-
return hebrewMonths[13] + (useGershGershayim ? GERESH : ""); // return Adar I, not Adar in a leap year
761+
return hebrewMonths[JewishDate.ADAR_II] + (useGershGershayim ? GERESH : ""); // return Adar I, not Adar in a leap year
762762
} else if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR_II) {
763-
return hebrewMonths[12] + (useGershGershayim ? GERESH : "");
763+
return hebrewMonths[JewishDate.ADAR] + (useGershGershayim ? GERESH : "");
764764
} else {
765765
return hebrewMonths[month - 1];
766766
}
767767
} else {
768768
if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
769-
return transliteratedMonths[13]; // return Adar I, not Adar in a leap year
769+
return transliteratedMonths[JewishDate.ADAR_II]; // return Adar I, not Adar in a leap year
770770
} else {
771771
return transliteratedMonths[month - 1];
772772
}

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

+35-36
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public class JewishDate implements Comparable<JewishDate>, Cloneable {
153153
/** The number of <em>chalakim</em> (1080) in an hour.*/
154154
private static final int CHALAKIM_PER_HOUR = 1080;
155155
/** The number of <em>chalakim</em> (25,920) in a 24 hour day .*/
156-
private static final int CHALAKIM_PER_DAY = 25920; // 24 * 1080
156+
private static final long CHALAKIM_PER_DAY = 25920; // 24 * 1080
157157
/** The number of <em>chalakim</em> in an average Jewish month. A month has 29 days, 12 hours and 793
158158
* <em>chalakim</em> (44 minutes and 3.3 seconds) for a total of 765,433 <em>chalakim</em>*/
159159
private static final long CHALAKIM_PER_MONTH = 765433; // (29 * 24 + 12) * 1080 + 793
@@ -336,23 +336,23 @@ boolean isGregorianLeapYear(int year) {
336336
* Returns the number of days in a given month in a given month and year.
337337
*
338338
* @param month
339-
* the month. As with other cases in this class, this is 1-based, not zero-based.
339+
* the month.
340340
* @param year
341341
* the year (only impacts February)
342342
* @return the number of days in the month in the given year
343343
*/
344344
private static int getLastDayOfGregorianMonth(int month, int year) {
345345
switch (month) {
346-
case 2:
346+
case Calendar.FEBRUARY:
347347
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
348348
return 29;
349349
} else {
350350
return 28;
351351
}
352-
case 4:
353-
case 6:
354-
case 9:
355-
case 11:
352+
case Calendar.APRIL:
353+
case Calendar.JUNE:
354+
case Calendar.SEPTEMBER:
355+
case Calendar.NOVEMBER:
356356
return 30;
357357
default:
358358
return 31;
@@ -365,11 +365,11 @@ private static int getLastDayOfGregorianMonth(int month, int year) {
365365
*/
366366
private void absDateToDate(int absDate) {
367367
int year = absDate / 366; // Search forward year by year from approximate year
368-
while (absDate >= gregorianDateToAbsDate(year + 1, 1, 1)) {
368+
while (absDate >= gregorianDateToAbsDate(year + 1, Calendar.JANUARY, 1)) {
369369
year++;
370370
}
371371

372-
int month = 1; // Search forward month by month from January
372+
int month = Calendar.JANUARY; // Search forward month by month from January
373373
while (absDate > gregorianDateToAbsDate(year, month, getLastDayOfGregorianMonth(month, year))) {
374374
month++;
375375
}
@@ -401,14 +401,15 @@ public int getAbsDate() {
401401
*/
402402
private static int gregorianDateToAbsDate(int year, int month, int dayOfMonth) {
403403
int absDate = dayOfMonth;
404-
for (int m = month - 1; m > 0; m--) {
404+
for (int m = Calendar.JANUARY; m < month; m++) {
405405
absDate += getLastDayOfGregorianMonth(m, year); // days in prior months of the year
406406
}
407+
int yearPrevious = year - 1;
407408
return (absDate // days this year
408-
+ 365 * (year - 1) // days in previous years ignoring leap days
409-
+ (year - 1) / 4 // Julian leap days before this year
410-
- (year - 1) / 100 // minus prior century years
411-
+ (year - 1) / 400); // plus prior years divisible by 400
409+
+ 365 * yearPrevious // days in previous years ignoring leap days
410+
+ yearPrevious / 4 // Julian leap days before this year
411+
- yearPrevious / 100 // minus prior century years
412+
+ yearPrevious / 400); // plus prior years divisible by 400
412413
}
413414

414415
/**
@@ -460,8 +461,8 @@ private static int getLastMonthOfJewishYear(int year) {
460461
*/
461462
public static int getJewishCalendarElapsedDays(int year) {
462463
long chalakimSince = getChalakimSinceMoladTohu(year, TISHREI);
463-
int moladDay = (int) (chalakimSince / (long) CHALAKIM_PER_DAY);
464-
int moladParts = (int) (chalakimSince - moladDay * (long) CHALAKIM_PER_DAY);
464+
int moladDay = (int) (chalakimSince / CHALAKIM_PER_DAY);
465+
int moladParts = (int) (chalakimSince - moladDay * CHALAKIM_PER_DAY);
465466
// delay Rosh Hashana for the 4 dechiyos
466467
return addDechiyos(year, moladDay, moladParts);
467468
}
@@ -670,7 +671,7 @@ private static void validateGregorianDate(int year, int month, int dayOfMonth) {
670671
* {@link GregorianCalendar}, where {@link Calendar#JANUARY} has a value of 0.
671672
*/
672673
private static void validateGregorianMonth(int month) {
673-
if (month > 11 || month < 0) {
674+
if (month > Calendar.DECEMBER || month < Calendar.JANUARY) {
674675
throw new IllegalArgumentException("The Gregorian month has to be between 0 - 11. " + month
675676
+ " is invalid.");
676677
}
@@ -908,8 +909,8 @@ private static int moladToAbsDate(long chalakim) {
908909
public JewishDate(long molad) {
909910
absDateToDate(moladToAbsDate(molad));
910911
// long chalakimSince = getChalakimSinceMoladTohu(year, TISHREI);// tishrei
911-
int conjunctionDay = (int) (molad / (long) CHALAKIM_PER_DAY);
912-
int conjunctionParts = (int) (molad - conjunctionDay * (long) CHALAKIM_PER_DAY);
912+
long conjunctionDay = molad / CHALAKIM_PER_DAY;
913+
int conjunctionParts = (int) (molad - conjunctionDay * CHALAKIM_PER_DAY);
913914
setMoladTime(conjunctionParts);
914915
}
915916

@@ -1042,13 +1043,10 @@ public void setDate(Calendar calendar) {
10421043
throw new IllegalArgumentException("Calendars with a BC era are not supported. The year "
10431044
+ calendar.get(Calendar.YEAR) + " BC is invalid.");
10441045
}
1045-
gregorianMonth = calendar.get(Calendar.MONTH) + 1;
1046-
gregorianDayOfMonth = calendar.get(Calendar.DATE);
1047-
gregorianYear = calendar.get(Calendar.YEAR);
1048-
gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init the date
1049-
absDateToJewishDate();
1050-
1051-
dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week
1046+
int month = calendar.get(Calendar.MONTH);
1047+
int dayOfMonth = calendar.get(Calendar.DATE);
1048+
int year = calendar.get(Calendar.YEAR);
1049+
setInternalGregorianDate(year, month, dayOfMonth);
10521050
}
10531051

10541052
/**
@@ -1095,7 +1093,7 @@ public void setDate(LocalDate localDate) {
10951093
*/
10961094
public void setGregorianDate(int year, int month, int dayOfMonth) {
10971095
validateGregorianDate(year, month, dayOfMonth);
1098-
setInternalGregorianDate(year, month + 1, dayOfMonth);
1096+
setInternalGregorianDate(year, month, dayOfMonth);
10991097
}
11001098

11011099
/**
@@ -1110,15 +1108,16 @@ public void setGregorianDate(int year, int month, int dayOfMonth) {
11101108
*/
11111109
private void setInternalGregorianDate(int year, int month, int dayOfMonth) {
11121110
// make sure date is a valid date for the given month, if not, set to last day of month
1113-
if (dayOfMonth > getLastDayOfGregorianMonth(month, year)) {
1114-
dayOfMonth = getLastDayOfGregorianMonth(month, year);
1111+
int lastDayOfMonth = getLastDayOfGregorianMonth(month, year);
1112+
if (dayOfMonth > lastDayOfMonth) {
1113+
dayOfMonth = lastDayOfMonth;
11151114
}
11161115
// init month, date, year
11171116
gregorianMonth = month;
11181117
gregorianDayOfMonth = dayOfMonth;
11191118
gregorianYear = year;
11201119

1121-
gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init date
1120+
gregorianAbsDate = gregorianDateToAbsDate(year, month, dayOfMonth); // init date
11221121
absDateToJewishDate();
11231122

11241123
dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week
@@ -1267,9 +1266,9 @@ public void forward(int field, int amount) {
12671266
if (gregorianDayOfMonth == getLastDayOfGregorianMonth(gregorianMonth, gregorianYear)) {
12681267
gregorianDayOfMonth = 1;
12691268
// if last day of year
1270-
if (gregorianMonth == 12) {
1269+
if (gregorianMonth == Calendar.DECEMBER) {
12711270
gregorianYear++;
1272-
gregorianMonth = 1;
1271+
gregorianMonth = Calendar.JANUARY;
12731272
} else {
12741273
gregorianMonth++;
12751274
}
@@ -1357,8 +1356,8 @@ private void forwardJewishMonth(int amount) {
13571356
public void back() {
13581357
// Change Gregorian date
13591358
if (gregorianDayOfMonth == 1) { // if first day of month
1360-
if (gregorianMonth == 1) { // if first day of year
1361-
gregorianMonth = 12;
1359+
if (gregorianMonth == Calendar.JANUARY) { // if first day of year
1360+
gregorianMonth = Calendar.DECEMBER;
13621361
gregorianYear--;
13631362
} else {
13641363
gregorianMonth--;
@@ -1421,7 +1420,7 @@ public int compareTo(JewishDate jewishDate) {
14211420
* @return the Gregorian month (between 0-11). Like the java.util.Calendar, months are 0 based.
14221421
*/
14231422
public int getGregorianMonth() {
1424-
return gregorianMonth - 1;
1423+
return gregorianMonth;
14251424
}
14261425

14271426
/**
@@ -1491,7 +1490,7 @@ public int getDayOfWeek() {
14911490
*/
14921491
public void setGregorianMonth(int month) {
14931492
validateGregorianMonth(month);
1494-
setInternalGregorianDate(gregorianYear, month + 1, gregorianDayOfMonth);
1493+
setInternalGregorianDate(gregorianYear, month, gregorianDayOfMonth);
14951494
}
14961495

14971496
/**

src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInGregorianMonth.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package com.kosherjava.zmanim.hebrewcalendar;
66

7+
import static org.junit.Assert.assertEquals;
8+
79
import org.junit.*;
810

911
import java.util.Calendar;
@@ -18,7 +20,6 @@ public class UT_DaysInGregorianMonth {
1820

1921
@Test
2022
public void testDaysInMonth() {
21-
2223
JewishDate hebrewDate = new JewishDate();
2324

2425
Calendar cal = Calendar.getInstance();
@@ -33,7 +34,6 @@ public void testDaysInMonth() {
3334

3435
@Test
3536
public void testDaysInMonthLeapYear() {
36-
3737
JewishDate hebrewDate = new JewishDate();
3838

3939
Calendar cal = Calendar.getInstance();
@@ -47,7 +47,6 @@ public void testDaysInMonthLeapYear() {
4747

4848
@Test
4949
public void testDaysInMonth100Year() {
50-
5150
JewishDate hebrewDate = new JewishDate();
5251

5352
Calendar cal = Calendar.getInstance();
@@ -61,7 +60,6 @@ public void testDaysInMonth100Year() {
6160

6261
@Test
6362
public void testDaysInMonth400Year() {
64-
6563
JewishDate hebrewDate = new JewishDate();
6664

6765
Calendar cal = Calendar.getInstance();
@@ -77,19 +75,18 @@ private void assertDaysInMonth(
7775
boolean febIsLeap,
7876
JewishDate hebrewDate
7977
) {
80-
81-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(1));
82-
Assert.assertEquals(febIsLeap ? 29 : 28, hebrewDate.getLastDayOfGregorianMonth(2));
83-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(3));
84-
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(4));
85-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(5));
86-
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(6));
87-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(7));
88-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(8));
89-
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(9));
90-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(10));
91-
Assert.assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(11));
92-
Assert.assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(12));
78+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.JANUARY));
79+
assertEquals(febIsLeap ? 29 : 28, hebrewDate.getLastDayOfGregorianMonth(Calendar.FEBRUARY));
80+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.MARCH));
81+
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.APRIL));
82+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.MAY));
83+
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.JUNE));
84+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.JULY));
85+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.AUGUST));
86+
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.SEPTEMBER));
87+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.OCTOBER));
88+
assertEquals(30, hebrewDate.getLastDayOfGregorianMonth(Calendar.NOVEMBER));
89+
assertEquals(31, hebrewDate.getLastDayOfGregorianMonth(Calendar.DECEMBER));
9390
}
9491

9592

src/test/java/com/kosherjava/zmanim/hebrewcalendar/UT_DaysInJewishMonth.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class UT_DaysInJewishMonth {
1414

1515
@Test
1616
public void daysInMonthsInHaserYear() {
17-
1817
assertHaser(5773);
1918
assertHaser(5777);
2019
assertHaser(5781);
@@ -26,7 +25,6 @@ public void daysInMonthsInHaserYear() {
2625

2726
@Test
2827
public void daysInMonthsInQesidrahYear() {
29-
3028
assertQesidrah(5769);
3129
assertQesidrah(5772);
3230
assertQesidrah(5778);
@@ -40,7 +38,6 @@ public void daysInMonthsInQesidrahYear() {
4038

4139
@Test
4240
public void daysInMonthsInShalemYear() {
43-
4441
assertShalem(5770);
4542
assertShalem(5780);
4643
assertShalem(5783);
@@ -80,8 +77,8 @@ private void assertQesidrah(int year) {
8077
JewishDate jewishDate = new JewishDate();
8178
jewishDate.setJewishYear(year);
8279

83-
Assert.assertFalse(jewishDate.isCheshvanLong( ));
84-
Assert.assertFalse(jewishDate.isKislevShort( ));
80+
Assert.assertFalse(jewishDate.isCheshvanLong());
81+
Assert.assertFalse(jewishDate.isKislevShort());
8582
}
8683

8784

@@ -90,16 +87,16 @@ private void assertQesidrahLeap(int year) {
9087
jewishDate.setJewishYear(year);
9188

9289
assertQesidrah(year);
93-
Assert.assertTrue(jewishDate.isJewishLeapYear( ));
90+
Assert.assertTrue(jewishDate.isJewishLeapYear());
9491
}
9592

9693

9794
private void assertShalem(int year) {
9895
JewishDate jewishDate = new JewishDate();
9996
jewishDate.setJewishYear(year);
10097

101-
Assert.assertTrue(jewishDate.isCheshvanLong( ));
102-
Assert.assertFalse(jewishDate.isKislevShort( ));
98+
Assert.assertTrue(jewishDate.isCheshvanLong());
99+
Assert.assertFalse(jewishDate.isKislevShort());
103100
}
104101

105102

@@ -108,7 +105,7 @@ private void assertShalemLeap(int year) {
108105
jewishDate.setJewishYear(year);
109106

110107
assertShalem(year);
111-
Assert.assertTrue(jewishDate.isJewishLeapYear( ));
108+
Assert.assertTrue(jewishDate.isJewishLeapYear());
112109
}
113110

114111
} // End of UT_DaysInJewishMonth class

0 commit comments

Comments
 (0)