diff --git a/src/main/java/com/kosherjava/zmanim/ZmanimCalendar.java b/src/main/java/com/kosherjava/zmanim/ZmanimCalendar.java index 8cfd7274..2bb1f04a 100644 --- a/src/main/java/com/kosherjava/zmanim/ZmanimCalendar.java +++ b/src/main/java/com/kosherjava/zmanim/ZmanimCalendar.java @@ -1086,8 +1086,7 @@ public void setCandleLightingOffset(double candleLightingOffset) { */ public boolean isAssurBemlacha(Instant currentTime, Instant tzais, boolean inIsrael) { JewishCalendar jewishCalendar = new JewishCalendar(); - jewishCalendar.setGregorianDate(getLocalDate().getYear(), getLocalDate().getMonthValue(), - getLocalDate().getDayOfMonth()); + jewishCalendar.setGregorianDate(getLocalDate()); jewishCalendar.setInIsrael(inIsrael); diff --git a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishCalendar.java b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishCalendar.java index c2bd6960..9541b27e 100644 --- a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishCalendar.java +++ b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishCalendar.java @@ -20,9 +20,10 @@ import java.time.Duration; import java.time.Instant; import java.time.LocalDate; +import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.Calendar; +import java.util.Calendar; // We still use the old Calendar.WEEKDAY constants /** * The JewishCalendar extends the JewishDate class and adds calendar methods. @@ -506,12 +507,12 @@ public Parsha getUpcomingParshah() { JewishCalendar clone = (JewishCalendar) clone(); int daysToShabbos = (Calendar.SATURDAY - getDayOfWeek() + 7) % 7; if (getDayOfWeek() != Calendar.SATURDAY) { - clone.forward(Calendar.DATE, daysToShabbos); + clone.addDays(daysToShabbos); } else { - clone.forward(Calendar.DATE, 7); + clone.addDays( 7); } while(clone.getParshah() == Parsha.NONE) { //Yom Kippur / Sukkos or Pesach with 2 potential non-parsha Shabbosim in a row - clone.forward(Calendar.DATE, 7); + clone.addDays(7); } return clone.getParshah(); } @@ -1233,16 +1234,9 @@ public Instant getMoladAsInstant() { int seconds = (int) moladSeconds; int nanos = (int) ((moladSeconds - seconds) * 1_000_000_000); // convert remainder to nanos - ZonedDateTime moladZdt = ZonedDateTime.of( - molad.getGregorianYear(), - molad.getGregorianMonth() + 1, // 1-based FIXME - molad.getGregorianDayOfMonth(), - molad.getMoladHours(), - molad.getMoladMinutes(), - seconds, - nanos, - jerusalemStandardOffset - ); + LocalTime time = LocalTime.of(molad.getMoladHours(),molad.getMoladMinutes(),seconds,nanos); + + ZonedDateTime moladZdt = ZonedDateTime.of(molad.getLocalDate(),time,jerusalemStandardOffset); // Har Habayis at a longitude of 35.2354 offset vs longitude 35 in standard time, so we subtract the time difference // of 20.94 minutes (20 minutes and 56 seconds and 496 millis) to get to Standard time from local mean time @@ -1402,9 +1396,9 @@ public boolean equals(Object object) { if (this == object) { return true; } - if (!(object instanceof JewishCalendar)) { - return false; - } + if (object == null || getClass() != object.getClass()) { + return false; + } JewishCalendar jewishCalendar = (JewishCalendar) object; return getAbsDate() == jewishCalendar.getAbsDate() && getInIsrael() == jewishCalendar.getInIsrael(); } @@ -1414,9 +1408,8 @@ public boolean equals(Object object) { * @see Object#hashCode() */ public int hashCode() { - int result = 17; - result = 37 * result + getClass().hashCode(); // needed or this and subclasses will return identical hash - result += 37 * result + getAbsDate() + (getInIsrael() ? 1 : 3); - return result; + int result = Integer.hashCode(getAbsDate()); + result = 31 * result + Boolean.hashCode(getInIsrael()); + return result; } } diff --git a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java index db74d09d..1a3d30a9 100644 --- a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java +++ b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/JewishDate.java @@ -17,9 +17,8 @@ package com.kosherjava.zmanim.hebrewcalendar; import java.time.LocalDate; +import java.time.YearMonth; import java.time.ZonedDateTime; -import java.util.Calendar; -import java.util.GregorianCalendar; /** * The JewishDate is the base calendar class, that supports maintenance of a {@link java.util.GregorianCalendar} @@ -290,51 +289,18 @@ public int getMoladChalakim() { return moladChalakim; } - /** - * Returns the last day in a gregorian month - * - * @param month - * the Gregorian month - * @return the last day of the Gregorian month - */ - int getLastDayOfGregorianMonth(int month) { - return getLastDayOfGregorianMonth(month, gregorianYear); - } - - /** - * Returns is the year passed in is a Gregorian leap year. - * @param year the Gregorian year - * @return if the year in question is a leap year. - */ - boolean isGregorianLeapYear(int year) { - return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); - } - - /** - * The month, where 1 == January, 2 == February, etc... Note that this is different than Java's Calendar class - * where January == 0. - */ - private int gregorianMonth; - - /** The day of the Gregorian month */ - private int gregorianDayOfMonth; - - /** The Gregorian year */ - private int gregorianYear; - /** 1 == Sunday, 2 == Monday, etc... */ private int dayOfWeek; /** Returns the absolute date (days since January 1, 0001 of the Gregorian calendar). * @see #getAbsDate() - * @see #absDateToJewishDate() + * @see #setJewishDateFromAbsDate() */ private int gregorianAbsDate; /** * Returns the number of days in a given month in a given month and year. - * + * * @param month * the month. As with other cases in this class, this is 1-based, not zero-based. * @param year @@ -342,28 +308,14 @@ boolean isGregorianLeapYear(int year) { * @return the number of days in the month in the given year */ private static int getLastDayOfGregorianMonth(int month, int year) { - switch (month) { - case 2: - if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { - return 29; - } else { - return 28; - } - case 4: - case 6: - case 9: - case 11: - return 30; - default: - return 31; - } + return YearMonth.of(year, month).lengthOfMonth(); } /** * Computes the Gregorian date from the absolute date. ND+ER * @param absDate the absolute date */ - private void absDateToDate(int absDate) { + private static LocalDate absDateToDate(int absDate) { int year = absDate / 366; // Search forward year by year from approximate year while (absDate >= gregorianDateToAbsDate(year + 1, 1, 1)) { year++; @@ -375,7 +327,7 @@ private void absDateToDate(int absDate) { } int dayOfMonth = absDate - gregorianDateToAbsDate(year, month, 1) + 1; - setInternalGregorianDate(year, month, dayOfMonth); + return LocalDate.of(year, month, dayOfMonth); } /** @@ -590,9 +542,11 @@ private static void validateJewishDate(int year, int month, int dayOfMonth, int throw new IllegalArgumentException("The Jewish month has to be between 1 and 12 (or 13 on a leap year). " + month + " is invalid for the year " + year + "."); } - if (dayOfMonth < 1 || dayOfMonth > 30) { - throw new IllegalArgumentException("The Jewish day of month can't be < 1 or > 30. " + dayOfMonth - + " is invalid."); + int monthLength = getDaysInJewishMonth(month, year); + if (dayOfMonth < 1 || dayOfMonth > monthLength) { + throw new IllegalArgumentException( + "The Jewish day of month can't be < 1 or > " + monthLength + + " for the month index " + monthLength + ". " + dayOfMonth + " is invalid."); } // reject dates prior to 18 Teves, 3761 (1/1/1 AD). This restriction can be relaxed if the date coding is // changed/corrected @@ -618,71 +572,6 @@ private static void validateJewishDate(int year, int month, int dayOfMonth, int } } - /** - * Validates the components of a Gregorian date for validity. It will throw an {@link IllegalArgumentException} if a - * year of < 1, a month < 0 or > 11 or a day of month < 1 is passed in. - * - * @param year - * the Gregorian year to validate. It will reject any year < 1. - * @param month - * the Gregorian month number to validate. It will enforce that the month is between 0 - 11 like a - * {@link GregorianCalendar}, where {@link Calendar#JANUARY} has a value of 0. - * @param dayOfMonth - * the day of the Gregorian month to validate. It will reject any value < 1, but will allow values > 31 - * since calling methods will simply set it to the maximum for that month. TODO: check calling methods to - * see if there is any reason that the class needs days > the maximum. - * @throws IllegalArgumentException - * if a year of < 1, a month < 0 or > 11 or a day of month < 1 is passed in - * @see #validateGregorianYear(int) - * @see #validateGregorianMonth(int) - * @see #validateGregorianDayOfMonth(int) - */ - private static void validateGregorianDate(int year, int month, int dayOfMonth) { - validateGregorianMonth(month); - validateGregorianDayOfMonth(dayOfMonth); - validateGregorianYear(year); - } - - /** - * Validates a Gregorian month for validity. - * - * @param month - * the Gregorian month number to validate. It will enforce that the month is between 0 - 11 like a - * {@link GregorianCalendar}, where {@link Calendar#JANUARY} has a value of 0. - */ - private static void validateGregorianMonth(int month) { - if (month > 11 || month < 0) { - throw new IllegalArgumentException("The Gregorian month has to be between 0 - 11. " + month - + " is invalid."); - } - } - - /** - * Validates a Gregorian day of month for validity. - * - * @param dayOfMonth - * the day of the Gregorian month to validate. It will reject any value < 1, but will allow values > 31 - * since calling methods will simply set it to the maximum for that month. TODO: check calling methods to - * see if there is any reason that the class needs days > the maximum. - */ - private static void validateGregorianDayOfMonth(int dayOfMonth) { - if (dayOfMonth <= 0) { - throw new IllegalArgumentException("The day of month can't be less than 1. " + dayOfMonth + " is invalid."); - } - } - - /** - * Validates a Gregorian year for validity. - * - * @param year - * the Gregorian year to validate. It will reject any year < 1. - */ - private static void validateGregorianYear(int year) { - if (year < 1) { - throw new IllegalArgumentException("Years < 1 can't be calculated. " + year + " is invalid."); - } - } - /** * Returns the number of days for a given Jewish year. ND+ER * @@ -806,9 +695,13 @@ public int getDaysInJewishMonth() { } /** - * Computes the Jewish date from the absolute date. + * Computes and sets the Jewish date fields based on the provided absolute (Gregorian) date. */ - private void absDateToJewishDate() { + private void setAbsDate(int gregorianAbsDate) { + if (gregorianAbsDate <= 0) { + throw new IllegalArgumentException("Dates in the BC era are not supported"); + } + this.gregorianAbsDate = gregorianAbsDate; // Approximation from below jewishYear = (gregorianAbsDate - JEWISH_EPOCH) / 366; // Search forward for year from the approximation @@ -826,6 +719,14 @@ private void absDateToJewishDate() { } // Calculate the day by subtraction jewishDay = gregorianAbsDate - jewishDateToAbsDate(jewishYear, jewishMonth, 1) + 1; + + // day of week (same calculation as original) + dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; + + // Set the molad fields to 0 + moladHours = 0; + moladMinutes = 0; + moladChalakim = 0; } /** @@ -861,7 +762,7 @@ private static int jewishDateToAbsDate(int year, int month, int dayOfMonth) { public JewishDate getMolad() { JewishDate moladDate = new JewishDate(getChalakimSinceMoladTohu()); if (moladDate.getMoladHours() >= 6) { - moladDate.forward(Calendar.DATE, 1); + moladDate.addDays(1); } moladDate.setMoladHours((moladDate.getMoladHours() + 18) % 24); return moladDate; @@ -887,24 +788,13 @@ private static int moladToAbsDate(long chalakim) { * @param molad the number of chalakim since the beginning of Sunday prior to BaHaRaD */ public JewishDate(long molad) { - absDateToDate(moladToAbsDate(molad)); + setAbsDate(moladToAbsDate(molad)); int conjunctionDay = (int) (molad / (long) CHALAKIM_PER_DAY); - int conjunctionParts = (int) (molad - conjunctionDay * (long) CHALAKIM_PER_DAY); - setMoladTime(conjunctionParts); - } - - /** - * Sets the molad time (hours minutes and chalakim) based on the number of chalakim since the start of the day. - * - * @param chalakim - * the number of chalakim since the start of the day. - */ - private void setMoladTime(int chalakim) { - int adjustedChalakim = chalakim; - setMoladHours(adjustedChalakim / CHALAKIM_PER_HOUR); - adjustedChalakim = adjustedChalakim - (getMoladHours() * CHALAKIM_PER_HOUR); - setMoladMinutes(adjustedChalakim / CHALAKIM_PER_MINUTE); - setMoladChalakim(adjustedChalakim - moladMinutes * CHALAKIM_PER_MINUTE); + int chalakim = (int) (molad - conjunctionDay * (long) CHALAKIM_PER_DAY); + setMoladHours(chalakim / CHALAKIM_PER_HOUR); + chalakim = chalakim - (getMoladHours() * CHALAKIM_PER_HOUR); + setMoladMinutes(chalakim / CHALAKIM_PER_MINUTE); + setMoladChalakim(chalakim - moladMinutes * CHALAKIM_PER_MINUTE); } /** @@ -1018,70 +908,10 @@ public void setGregorianDate(ZonedDateTime zonedDateTime) { * if the date would fall prior to the year 1 AD */ public void setGregorianDate(LocalDate localDate) { - if (localDate.getYear() <= 0) { - throw new IllegalArgumentException( - "Calendars with a BC era are not supported. The year " - + localDate.getYear() + " BC is invalid." - ); - } - - gregorianYear = localDate.getYear(); - gregorianMonth = localDate.getMonth().getValue(); // FIXME + 1;// 1 = January - gregorianDayOfMonth = localDate.getDayOfMonth(); - - // initialize absolute date - gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); + int absDate = gregorianDateToAbsDate(localDate.getYear(), localDate.getMonth().getValue(), localDate.getDayOfMonth()); // FIXME + 1;// 1 = January // convert to Jewish date - absDateToJewishDate(); - - // day of week (same calculation as original) - dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; - } - - /** - * Sets the Gregorian Date, and updates the Jewish date accordingly. Like the Java Calendar A value of 0 is expected - * for January. - * - * @param year - * the Gregorian year - * @param month - * the Gregorian month. Like the Java Calendar, this class expects 0 for January - * @param dayOfMonth - * the Gregorian day of month. If this is > the number of days in the month/year, the last valid date of - * the month will be set - * @throws IllegalArgumentException - * if a year of < 1, a month < 0 or > 11 or a day of month < 1 is passed in - */ - public void setGregorianDate(int year, int month, int dayOfMonth) { - validateGregorianDate(year, month, dayOfMonth); - setInternalGregorianDate(year, month + 1, dayOfMonth); - } - - /** - * Sets the hidden internal representation of the Gregorian date , and updates the Jewish date accordingly. While - * public getters and setters have 0 based months matching the Java Calendar classes, This class internally - * represents the Gregorian month starting at 1. When this is called it will not adjust the month to match the Java - * Calendar classes. - * - * @param year the year - * @param month the month - * @param dayOfMonth the day of month - */ - private void setInternalGregorianDate(int year, int month, int dayOfMonth) { - // make sure date is a valid date for the given month, if not, set to last day of month - if (dayOfMonth > getLastDayOfGregorianMonth(month, year)) { - dayOfMonth = getLastDayOfGregorianMonth(month, year); - } - // init month, date, year - gregorianMonth = month; - gregorianDayOfMonth = dayOfMonth; - gregorianYear = year; - - gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init date - absDateToJewishDate(); - - dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week + setAbsDate(absDate); } /** @@ -1132,13 +962,14 @@ public void setJewishDate(int year, int month, int dayOfMonth) { * chalakim per minutes, so it would be 44 minutes and 1 chelek in the case of 793 (TaShTzaG). */ public void setJewishDate(int year, int month, int dayOfMonth, int hours, int minutes, int chalakim) { - validateJewishDate(year, month, dayOfMonth, hours, minutes, chalakim); - // if 30 is passed for a month that only has 29 days (for example by rolling the month from a month that had 30 - // days to a month that only has 29) set the date to 29th - if (dayOfMonth > getDaysInJewishMonth(month, year)) { - dayOfMonth = getDaysInJewishMonth(month, year); - } + // if 30 is passed for a month that only has 29 days (for example by rolling the month from a month that had 30 + // days to a month that only has 29) set the date to 29th + if (dayOfMonth > getDaysInJewishMonth(month, year)) { + dayOfMonth = getDaysInJewishMonth(month, year); + } + + validateJewishDate(year, month, dayOfMonth, hours, minutes, chalakim); jewishMonth = month; jewishDay = dayOfMonth; @@ -1148,21 +979,10 @@ public void setJewishDate(int year, int month, int dayOfMonth, int hours, int mi moladChalakim = chalakim; gregorianAbsDate = jewishDateToAbsDate(jewishYear, jewishMonth, jewishDay); // reset Gregorian date - absDateToDate(gregorianAbsDate); dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // reset day of week } - /** - * Returns this object's date as a {@link java.util.Calendar} object. - * - * @return The {@link java.util.Calendar} - */ - public Calendar getGregorianCalendar() { - Calendar calendar = Calendar.getInstance(); - calendar.set(getGregorianYear(), getGregorianMonth(), getGregorianDayOfMonth()); - return calendar; - } /** * Returns this object's date as a {@link java.time.LocalDate} object. @@ -1170,7 +990,7 @@ public Calendar getGregorianCalendar() { * @return The {@link java.time.LocalDate} */ public LocalDate getLocalDate() { - return LocalDate.of(getGregorianYear(), getGregorianMonth() + 1, getGregorianDayOfMonth()); + return absDateToDate(getAbsDate()); } /** @@ -1181,6 +1001,52 @@ public void resetDate() { setGregorianDate(localDate); } + public void minusDays(int days){ + if (days < 1) { + throw new IllegalArgumentException("the amount of days to subtract has to be greater than zero."); + } + setAbsDate(getAbsDate() - days); + + } + public void addDays(int days){ + if (days < 1) { + throw new IllegalArgumentException("the amount of days to add has to be greater than zero."); + } + setAbsDate(getAbsDate() + days); + } + public void addMonths(int months){ + if (months < 1) { + throw new IllegalArgumentException("the amount of months to add has to be greater than zero."); + } + int year = getJewishYear(); + int month = getJewishMonth(); + for (int i = 0; i < months; i++) { + if (month == ELUL) { + month = TISHREI; + year++; + } else if ((! isJewishLeapYear(year) && month == ADAR) + || (isJewishLeapYear(year) && month == ADAR_II)){ + month = NISSAN; + } else { + month++; + } + } + int day = Math.min(getJewishDayOfMonth(), getDaysInJewishMonth(month,year)); + setJewishDate(year, month, day); + } + public void addYears(int years){ + if (years < 1) { + throw new IllegalArgumentException("the amount of years to add has to be greater than zero."); + } + int year = getJewishYear() + years; + // Clamp to ADAR + int month = Math.min(getJewishMonth(),getLastMonthOfJewishYear(year)); + // Clamp to final day of the month + int day = Math.min(getJewishDayOfMonth(), getDaysInJewishMonth(month,year)); + setJewishDate(year, month, day); + } + + /** * Returns a string containing the Jewish date in the form, "day Month, year" e.g. "21 Shevat, 5729". For more * complex formatting, use the formatter classes. @@ -1192,165 +1058,6 @@ public String toString() { return new HebrewDateFormatter().format(this); } - /** - * Rolls the date, month or year forward by the amount passed in. It modifies both the Gregorian and Jewish dates accordingly. - * If manipulation beyond the fields supported here is required, use the {@link Calendar} class {@link Calendar#add(int, int)} - * or {@link Calendar#roll(int, int)} methods in the following manner. - * - *
- *
- * Calendar cal = jewishDate.getTime(); // get a java.util.Calendar representation of the JewishDate
- * cal.add(Calendar.MONTH, 3); // add 3 Gregorian months
- * jewishDate.setDate(cal); // set the updated calendar back to this class
- *
- *
- *
- * @param field the calendar field to be forwarded. The must be {@link Calendar#DATE}, {@link Calendar#MONTH} or {@link Calendar#YEAR}
- * @param amount the positive amount to move forward
- * @throws IllegalArgumentException if the field is anything besides {@link Calendar#DATE}, {@link Calendar#MONTH} or {@link Calendar#YEAR}
- * or if the amount is less than 1
- *
- * @see #back()
- * @see Calendar#add(int, int)
- * @see Calendar#roll(int, int)
- */
- public void forward(int field, int amount) { //FIXME first param should be converted from the Calendar.DATE
- if (field != Calendar.DATE && field != Calendar.MONTH && field != Calendar.YEAR) {
- throw new IllegalArgumentException("Unsupported field was passed to Forward. Only Calendar.DATE, Calendar.MONTH or Calendar.YEAR are supported.");
- }
- if (amount < 1) {
- throw new IllegalArgumentException("JewishDate.forward() does not support amounts less than 1. See JewishDate.back()");
- }
- if (field == Calendar.DATE) {
- // Change Gregorian date
- for (int i = 0; i < amount; i++) {
- if (gregorianDayOfMonth == getLastDayOfGregorianMonth(gregorianMonth, gregorianYear)) {
- gregorianDayOfMonth = 1;
- // if last day of year
- if (gregorianMonth == 12) {
- gregorianYear++;
- gregorianMonth = 1;
- } else {
- gregorianMonth++;
- }
- } else { // if not last day of month
- gregorianDayOfMonth++;
- }
-
- // Change the Jewish Date
- if (jewishDay == getDaysInJewishMonth()) {
- // if it last day of elul (i.e. last day of Jewish year)
- if (jewishMonth == ELUL) {
- jewishYear++;
- jewishMonth++;
- jewishDay = 1;
- } else if (jewishMonth == getLastMonthOfJewishYear(jewishYear)) {
- // if it is the last day of Adar, or Adar II as case may be
- jewishMonth = NISSAN;
- jewishDay = 1;
- } else {
- jewishMonth++;
- jewishDay = 1;
- }
- } else { // if not last date of month
- jewishDay++;
- }
-
- if (dayOfWeek == 7) { // if last day of week, loop back to Sunday
- dayOfWeek = 1;
- } else {
- dayOfWeek++;
- }
-
- gregorianAbsDate++; // increment the absolute date
- }
- } else if (field == Calendar.MONTH) {
- forwardJewishMonth(amount);
- } else {
- setJewishYear(getJewishYear() + amount);
- }
- }
-
- /**
- * Forward the Jewish date by the number of months passed in.
- * 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
- * the way that the Java Calendar behaves (not that simple since there is a difference between add() or roll().
- *
- * @throws IllegalArgumentException if the amount is less than 1
- * @param amount the number of months to roll the month forward
- */
- private void forwardJewishMonth(int amount) {
- if (amount < 1) {
- throw new IllegalArgumentException("the amount of months to forward has to be greater than zero.");
- }
- for (int i = 0; i < amount; i++) {
- if (getJewishMonth() == ELUL) {
- setJewishMonth(TISHREI);
- setJewishYear(getJewishYear() + 1);
- } else if ((! isJewishLeapYear() && getJewishMonth() == ADAR)
- || (isJewishLeapYear() && getJewishMonth() == ADAR_II)){
- setJewishMonth(NISSAN);
- } else {
- setJewishMonth(getJewishMonth() + 1);
- }
- }
- }
-
- /**
- * Rolls the date back by 1 day. It modifies both the Gregorian and Jewish dates accordingly. The API does not
- * currently offer the ability to forward more than one day at a time, or to forward by month or year. If such
- * manipulation is required use the {@link Calendar} class {@link Calendar#add(int, int)} or
- * {@link Calendar#roll(int, int)} methods in the following manner.
- *
- *
- *
- * Calendar cal = jewishDate.getTime(); // get a java.util.Calendar representation of the JewishDate
- * cal.add(Calendar.MONTH, -3); // subtract 3 Gregorian months
- * jewishDate.setDate(cal); // set the updated calendar back to this class
- *
- *
- *
- * @see #back()
- * @see Calendar#add(int, int)
- * @see Calendar#roll(int, int)
- */
- public void back() {
- // Change Gregorian date
- if (gregorianDayOfMonth == 1) { // if first day of month
- if (gregorianMonth == 1) { // if first day of year
- gregorianMonth = 12;
- gregorianYear--;
- } else {
- gregorianMonth--;
- }
- // change to last day of previous month
- gregorianDayOfMonth = getLastDayOfGregorianMonth(gregorianMonth, gregorianYear);
- } else {
- gregorianDayOfMonth--;
- }
- // change Jewish date
- if (jewishDay == 1) { // if first day of the Jewish month
- if (jewishMonth == NISSAN) {
- jewishMonth = getLastMonthOfJewishYear(jewishYear);
- } else if (jewishMonth == TISHREI) { // if Rosh Hashana
- jewishYear--;
- jewishMonth--;
- } else {
- jewishMonth--;
- }
- jewishDay = getDaysInJewishMonth();
- } else {
- jewishDay--;
- }
-
- if (dayOfWeek == 1) { // if first day of week, loop back to Saturday
- dayOfWeek = 7;
- } else {
- dayOfWeek--;
- }
- gregorianAbsDate--; // change the absolute date
- }
-
/**
* Indicates whether some other object is "equal to" this one.
* @see Object#equals(Object)
@@ -1359,9 +1066,9 @@ public boolean equals(Object object) {
if (this == object) {
return true;
}
- if (!(object instanceof JewishDate)) {
- return false;
- }
+ if (object == null || getClass() != object.getClass()) {
+ return false;
+ }
JewishDate jewishDate = (JewishDate) object;
return gregorianAbsDate == jewishDate.getAbsDate();
}
@@ -1375,33 +1082,6 @@ public int compareTo(JewishDate jewishDate) {
return Integer.compare(gregorianAbsDate, jewishDate.getAbsDate());
}
- /**
- * Returns the Gregorian month (between 0-11).
- *
- * @return the Gregorian month (between 0-11). Like the java.util.Calendar, months are 0 based.
- */
- public int getGregorianMonth() {
- return gregorianMonth - 1; //FIXME
- }
-
- /**
- * Returns the Gregorian day of the month.
- *
- * @return the Gregorian day of the mont
- */
- public int getGregorianDayOfMonth() {
- return gregorianDayOfMonth;
- }
-
- /**
- * Returns the Gregorian year.
- *
- * @return the Gregorian year
- */
- public int getGregorianYear() {
- return gregorianYear;
- }
-
/**
* Returns the Jewish month 1-12 (or 13 years in a leap year). The month count starts with 1 for Nissan and goes to
* 13 for Adar II
@@ -1440,84 +1120,6 @@ public int getDayOfWeek() {
return dayOfWeek;
}
- /**
- * Sets the Gregorian month.
- *
- * @param month
- * the Gregorian month
- *
- * @throws IllegalArgumentException
- * if a month < 0 or > 11 is passed in
- */
- public void setGregorianMonth(int month) {
- validateGregorianMonth(month);
- setInternalGregorianDate(gregorianYear, month + 1, gregorianDayOfMonth); //FIXME
- }
-
- /**
- * sets the Gregorian year.
- *
- * @param year
- * the Gregorian year.
- * @throws IllegalArgumentException
- * if a year of < 1 is passed in
- */
- public void setGregorianYear(int year) {
- validateGregorianYear(year);
- setInternalGregorianDate(year, gregorianMonth, gregorianDayOfMonth);
- }
-
- /**
- * sets the Gregorian Day of month.
- *
- * @param dayOfMonth
- * the Gregorian Day of month.
- * @throws IllegalArgumentException
- * if the day of month of < 1 is passed in
- */
- public void setGregorianDayOfMonth(int dayOfMonth) {
- validateGregorianDayOfMonth(dayOfMonth);
- setInternalGregorianDate(gregorianYear, gregorianMonth, dayOfMonth);
- }
-
- /**
- * sets the Jewish month.
- *
- * @param month
- * the Jewish month from 1 to 12 (or 13 years in a leap year). The month count starts with 1 for Nissan
- * and goes to 13 for Adar II
- * @throws IllegalArgumentException
- * if a month < 1 or > 12 (or 13 on a leap year) is passed in
- */
- public void setJewishMonth(int month) {
- setJewishDate(jewishYear, month, jewishDay);
- }
-
- /**
- * sets the Jewish year.
- *
- * @param year
- * the Jewish year
- * @throws IllegalArgumentException
- * if a year of < 3761 is passed in. The same will happen if the year is 3761 and the month and day
- * previously set are < 18 Teves (prior to Jan 1, 1 AD)
- */
- public void setJewishYear(int year) {
- setJewishDate(year, jewishMonth, jewishDay);
- }
-
- /**
- * sets the Jewish day of month.
- *
- * @param dayOfMonth
- * the Jewish day of month
- * @throws IllegalArgumentException
- * if the day of month is < 1 or > 30 is passed in
- */
- public void setJewishDayOfMonth(int dayOfMonth) {
- setJewishDate(jewishYear, jewishMonth, dayOfMonth);
- }
-
/**
* A method that creates a deep copy of the object.
*
@@ -1531,7 +1133,7 @@ public Object clone() {
// Required by the compiler. Should never be reached since we implement clone()
}
if (clone != null) {
- clone.setInternalGregorianDate(gregorianYear, gregorianMonth, gregorianDayOfMonth);
+ clone.setAbsDate(getAbsDate());
}
return clone;
}
@@ -1541,9 +1143,6 @@ public Object clone() {
* @see Object#hashCode()
*/
public int hashCode() {
- int result = 17;
- result = 37 * result + getClass().hashCode(); // needed or this and subclasses will return identical hash
- result += 37 * result + gregorianAbsDate;
- return result;
+ return Integer.hashCode(gregorianAbsDate);
}
}