Skip to content

Commit 84fa545

Browse files
authored
Start using java.time classes
Breaking changes
1 parent 5ac3d2a commit 84fa545

1 file changed

Lines changed: 48 additions & 39 deletions

File tree

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

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Zmanim Java API
3-
* Copyright (C) 2011 - 2024 Eliyahu Hershfeld
3+
* Copyright (C) 2011 - 2026 Eliyahu Hershfeld
44
* Copyright (C) September 2002 Avrom Finkelstien
55
*
66
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
@@ -17,7 +17,9 @@
1717
package com.kosherjava.zmanim.hebrewcalendar;
1818

1919
import java.time.LocalDate;
20-
import java.util.Date;
20+
import java.time.ZoneId;
21+
import java.time.ZonedDateTime;
22+
import java.time.Instant;
2123
import java.util.Calendar;
2224
import java.util.GregorianCalendar;
2325

@@ -52,7 +54,7 @@
5254
* @see java.util.Date
5355
* @see java.util.Calendar
5456
* @author © Avrom Finkelstien 2002
55-
* @author © Eliyahu Hershfeld 2011 - 2024
57+
* @author © Eliyahu Hershfeld 2011 - 2026
5658
*/
5759
public class JewishDate implements Comparable<JewishDate>, Cloneable {
5860
/**
@@ -976,25 +978,25 @@ public JewishDate() {
976978
/**
977979
* A constructor that initializes the date to the {@link java.util.Date Date} parameter.
978980
*
979-
* @param date
980-
* the <code>Date</code> to set the calendar to
981+
* @param instant
982+
* the <code>Instant</code> to set the calendar to
981983
* @throws IllegalArgumentException
982984
* if the date would fall prior to the January 1, 1 AD
983985
*/
984-
public JewishDate(Date date) {
985-
setDate(date);
986+
public JewishDate(Instant instant) {
987+
setDate(instant);
986988
}
987989

988990
/**
989991
* A constructor that initializes the date to the {@link java.util.Calendar Calendar} parameter.
990992
*
991-
* @param calendar
992-
* the <code>Calendar</code> to set the calendar to
993+
* @param zonedDateTime
994+
* the <code>ZonedDateTime</code> to set the calendar to
993995
* @throws IllegalArgumentException
994996
* if the {@link Calendar#ERA} is {@link GregorianCalendar#BC}
995997
*/
996-
public JewishDate(Calendar calendar) {
997-
setDate(calendar);
998+
public JewishDate(ZonedDateTime zonedDateTime) {
999+
setDate(zonedDateTime);
9981000
}
9991001

10001002
/**
@@ -1012,37 +1014,45 @@ public JewishDate(LocalDate localDate) {
10121014
/**
10131015
* Sets the date based on a {@link java.util.Calendar Calendar} object. Modifies the Jewish date as well.
10141016
*
1015-
* @param calendar
1016-
* the <code>Calendar</code> to set the calendar to
1017+
* @param zonedDateTime
1018+
* the <code>ZonedDateTime</code> to set the calendar to
10171019
* @throws IllegalArgumentException
10181020
* if the {@link Calendar#ERA} is {@link GregorianCalendar#BC}
10191021
*/
1020-
public void setDate(Calendar calendar) {
1021-
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
1022-
throw new IllegalArgumentException("Calendars with a BC era are not supported. The year "
1023-
+ calendar.get(Calendar.YEAR) + " BC is invalid.");
1024-
}
1025-
gregorianMonth = calendar.get(Calendar.MONTH) + 1;
1026-
gregorianDayOfMonth = calendar.get(Calendar.DATE);
1027-
gregorianYear = calendar.get(Calendar.YEAR);
1028-
gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth); // init the date
1029-
absDateToJewishDate();
1022+
public void setDate(ZonedDateTime zonedDateTime) {
1023+
int year = zonedDateTime.getYear();
10301024

1031-
dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1; // set day of week
1025+
if (year <= 0) {
1026+
throw new IllegalArgumentException(
1027+
"Calendars with a BC era are not supported. The year "
1028+
+ year + " BC is invalid."
1029+
);
1030+
}
1031+
1032+
gregorianYear = year;
1033+
gregorianMonth = zonedDateTime.getMonthValue(); // 1 = January
1034+
gregorianDayOfMonth = zonedDateTime.getDayOfMonth();
1035+
1036+
// initialize absolute date
1037+
gregorianAbsDate = gregorianDateToAbsDate(gregorianYear, gregorianMonth, gregorianDayOfMonth);
1038+
1039+
// convert to Jewish date
1040+
absDateToJewishDate();
1041+
1042+
// day of week (same calculation as original)
1043+
dayOfWeek = Math.abs(gregorianAbsDate % 7) + 1;
10321044
}
10331045

10341046
/**
1035-
* Sets the date based on a {@link java.util.Date Date} object. Modifies the Jewish date as well.
1047+
* Sets the date based on a {@link java.time.Instant Instant} object. Modifies the Jewish date as well.
10361048
*
1037-
* @param date
1038-
* the <code>Date</code> to set the calendar to
1049+
* @param instant
1050+
* the <code>Instant</code> to set the calendar to
10391051
* @throws IllegalArgumentException
10401052
* if the date would fall prior to the year 1 AD
10411053
*/
1042-
public void setDate(Date date) {
1043-
Calendar cal = Calendar.getInstance();
1044-
cal.setTime(date);
1045-
setDate(cal);
1054+
public void setDate(Instant instant) {
1055+
setDate(instant.atZone(ZoneId.systemDefault()));
10461056
}
10471057

10481058
/**
@@ -1054,9 +1064,8 @@ public void setDate(Date date) {
10541064
* if the date would fall prior to the year 1 AD
10551065
*/
10561066
public void setDate(LocalDate localDate) {
1057-
Calendar cal = Calendar.getInstance();
1058-
cal.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth());
1059-
setDate(cal);
1067+
ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.systemDefault());
1068+
setDate(zdt);
10601069
}
10611070

10621071
/**
@@ -1197,8 +1206,8 @@ public LocalDate getLocalDate() {
11971206
* Resets this date to the current system date.
11981207
*/
11991208
public void resetDate() {
1200-
Calendar calendar = Calendar.getInstance();
1201-
setDate(calendar);
1209+
ZonedDateTime zdt = ZonedDateTime.now();
1210+
setDate(zdt);
12021211
}
12031212

12041213
/**
@@ -1234,7 +1243,7 @@ public String toString() {
12341243
* @see Calendar#add(int, int)
12351244
* @see Calendar#roll(int, int)
12361245
*/
1237-
public void forward(int field, int amount) {
1246+
public void forward(int field, int amount) { //FIXME first param should be converted from the Calendar.DATE
12381247
if (field != Calendar.DATE && field != Calendar.MONTH && field != Calendar.YEAR) {
12391248
throw new IllegalArgumentException("Unsupported field was passed to Forward. Only Calendar.DATE, Calendar.MONTH or Calendar.YEAR are supported.");
12401249
}
@@ -1401,7 +1410,7 @@ public int compareTo(JewishDate jewishDate) {
14011410
* @return the Gregorian month (between 0-11). Like the java.util.Calendar, months are 0 based.
14021411
*/
14031412
public int getGregorianMonth() {
1404-
return gregorianMonth - 1;
1413+
return gregorianMonth - 1; //FIXME
14051414
}
14061415

14071416
/**
@@ -1471,7 +1480,7 @@ public int getDayOfWeek() {
14711480
*/
14721481
public void setGregorianMonth(int month) {
14731482
validateGregorianMonth(month);
1474-
setInternalGregorianDate(gregorianYear, month + 1, gregorianDayOfMonth);
1483+
setInternalGregorianDate(gregorianYear, month + 1, gregorianDayOfMonth); //FIXME
14751484
}
14761485

14771486
/**

0 commit comments

Comments
 (0)