Skip to content

Commit 0f8f92b

Browse files
committed
check for valid dates with forwarding dates
1 parent c944303 commit 0f8f92b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,28 +1301,36 @@ public void forward(int field, int amount) {
13011301
}
13021302

13031303
/**
1304-
* Forward the Jewish date by the number of months passed in.
1305-
* FIXME: Deal with forwarding a date such as 30 Nissan by a month. 30 Iyar does not exist. This should be dealt with similar to
1306-
* the way that the Java Calendar behaves (not that simple since there is a difference between add() or roll().
1304+
* Advances the Jewish date forward by the specified number of months.
1305+
* If the day doesn't exist in the target month (e.g., 30 Iyar), it adjusts to the last day of that month (29 Iyar).
13071306
*
13081307
* @throws IllegalArgumentException if the amount is less than 1
1309-
* @param amount the number of months to roll the month forward
1308+
* @param amount the number of months to advance (must be at least 1)
13101309
*/
13111310
private void forwardJewishMonth(int amount) {
13121311
if (amount < 1) {
13131312
throw new IllegalArgumentException("the amount of months to forward has to be greater than zero.");
13141313
}
1314+
int currentMonth = getJewishMonth();
1315+
int currentYear = getJewishYear();
1316+
int currentDay = getJewishDayOfMonth();
13151317
for (int i = 0; i < amount; i++) {
1316-
if (getJewishMonth() == ELUL) {
1317-
setJewishMonth(TISHREI);
1318-
setJewishYear(getJewishYear() + 1);
1319-
} else if ((! isJewishLeapYear() && getJewishMonth() == ADAR)
1320-
|| (isJewishLeapYear() && getJewishMonth() == ADAR_II)){
1321-
setJewishMonth(NISSAN);
1318+
boolean isLeapYear = JewishDate.isJewishLeapYear(currentYear);
1319+
if (currentMonth == ELUL) {
1320+
currentMonth = TISHREI;
1321+
currentYear = currentYear + 1;
1322+
} else if ((!isLeapYear && currentMonth == ADAR)
1323+
|| (isLeapYear && currentMonth == ADAR_II)){
1324+
currentMonth = NISSAN;
13221325
} else {
1323-
setJewishMonth(getJewishMonth() + 1);
1326+
currentMonth = currentMonth + 1;
13241327
}
13251328
}
1329+
int maxDaysInMonth = JewishDate.getDaysInJewishMonth(currentMonth, currentYear);
1330+
if (currentDay > maxDaysInMonth) {
1331+
currentDay = maxDaysInMonth;
1332+
}
1333+
setJewishDate(currentYear, currentMonth, currentDay);
13261334
}
13271335

13281336
/**

0 commit comments

Comments
 (0)