How do I calculate the בעל התניא Baal Hatanya’s zman for shkiah as 4 minutes after sunset using the KosherJava Zmanim API?
Answer:
I was recently asked how to use the Zmanim API to calculate the Baal Hatanya’s shkiah, based on the opinion that shkiah (halachic sunset) is 4 minutes after civil sunset. The assumption that the Baal Hatanya’s shkiah is a fixed 4 minutes after sunset is not that simple and will require a separate post to clarify. This zman should not be used lehalacha without consulting a rov. This post shows how to use the API assuming that it is a fixed 4 minutes after sunset. The technique to calculate this with the API is the same as the way getTzais72Minutes() is calculated. The source of that method is
public Instant getTzais72Minutes() {
return getTimeOffset(getSunsetBasedOnElevationSetting(), Duration.ofMinutes(72));
}
public static Instant getTimeOffset(Instant time, Duration offset) {
if (time == null || offset == null) {
return null;
}
return time.plus(offset);
}
The getTimeOffset method simply adds the offset to the time of the zman and returns the result. While using the API itself is not needed for such a simple calculation, here is how it would be used:
Adding it to the API itself would be even simpler:
public Instant getShkiahBaalHatanya() {
return getTimeOffset(getSeaLevelSunset(), Duration.ofMinutes(4));
}
At some point in the future I may (doubtful) add this time to the API itself. The zman is not commonly used, and the Chabad calendars that I have seen all use regular sunset.
Update on July 5, 2015 – י״ח תמוז תשע״ה: This article was updated to clarify that the Baal Hatanya’s opinion may not be a fixed 4 minutes, but that the post was showing how to use the API to calculate it based on the questioner’s assumption that it was a 4-minute zman. Update on December 14, 2018 – ו׳ טבת תשע״ט: This article was superseded with the more recent and corrected article Baal Hatanya’s Zmanim Added to KosherJava Zmanim Library.
The recently released Zmanim API 1.3.0 added Jewish calendar support to the API. Previously, the API had zmanim support, but no Jewish calendar support. While the calendar code is in beta mode and is subject to API changes, below are some simple code examples using the current 1.3.0 release. These examples of the use of the JewishCalendar and HebrewDateFormatter classes do not fully cover the functionality available in the Zmanim API. Please read the Javadocs and experiment. Please let me know if there are any calendar-related items that you feel should change or be added to the API.
Note: The code samples and their output below have been updated for the KosherJava Zmanim 3.x API.
Setting and outputting formatted dates
JewishCalendar jd = new JewishCalendar(5773, JewishDate.NISSAN, 23); // 23 Nissan, 5773
HebrewDateFormatter hdf = new HebrewDateFormatter();
System.out.println(jd); // prints the Hebrew date in English characters - 23 Nissan, 5773
hdf.setHebrewFormat(true); // change formatting to Hebrew
System.out.println(hdf.format(jd)); // date formatted in Hebrew
jd.setJewishDate(5729, JewishDate.SHEVAT, 21); // set the date to 21 Shevat, 5729
System.out.println(hdf.format(jd)); // date formatted in Hebrew
jd.setJewishDate(5772, JewishDate.NISSAN, 18); // set the date to the fourth day of Pesach
System.out.println(hdf.format(jd));
System.out.println(hdf.formatYomTov(jd)); // output Chol Hamoed Pesach in Hebrew
hdf.setHebrewFormat(false); // change formatting to the default
System.out.println(hdf.format(jd)); // prints the Hebrew date in English characters - 18 Nissan, 5772
System.out.println(hdf.formatYomTov(jd)); // output Chol Hamoed Pesach
Please note that the parsha will only be output if the date is a Shabbos. To get the parsha of the coming Shabbos on a weekday, use getUpcomingParshah(), which was added to the API after this article was written.
HebrewDateFormatter hdf = new HebrewDateFormatter();
JewishCalendar jd = new JewishCalendar(5773, JewishDate.NISSAN, 12);
System.out.println(hdf.formatParshah(jd));
hdf.setHebrewFormat(true);
System.out.println(hdf.formatParshah(jd));
jd.minusDays(1);
System.out.println("Parsha on Friday [" + hdf.formatParshah(jd) + "]"); // no parsha output on a weekday
System.out.println("Upcoming parsha [" + hdf.formatParshah(jd.getUpcomingParshah()) + "]"); // the coming Shabbos's parsha
jd.setJewishDate(5773, JewishDate.TAMMUZ, 28); // double parsha
System.out.println(hdf.formatParshah(jd));
Output:
Tzav
צו
Parsha on Friday []
Upcoming parsha [צו]
מטות מסעי
Rosh Chodesh
HebrewDateFormatter hdf = new HebrewDateFormatter();
JewishCalendar jd = new JewishCalendar(5773, JewishDate.NISSAN, 1);
if (jd.isRoshChodesh()) { // not necessary for the formatter
System.out.println(hdf.formatRoshChodesh(jd));
}
hdf.setHebrewFormat(true);
System.out.println(hdf.formatRoshChodesh(jd));
jd.plusDays(1); // roll the calendar to the second day of Nissan
System.out.println("output[" + hdf.formatRoshChodesh(jd) + "]"); // no Rosh Chodesh output
Output:
Rosh Chodesh Nissan
ראש חודש ניסן
output[]
Daf Yomi
JewishCalendar jd = new JewishCalendar(5773, JewishDate.NISSAN, 13);
HebrewDateFormatter hdf = new HebrewDateFormatter();
System.out.println(hdf.format(jd)); // output the formatted date "13 Nissan, 5773"
Daf daf = jd.getDafYomiBavli(); // get the daf for the date
System.out.println(daf.getMasechtaTransliterated()); // output the transliterated masechta name "Eruvin"
System.out.println(daf.getDaf()); // output the daf (page) number "16"
System.out.println(hdf.formatDafYomiBavli(daf)); // output the formatted daf "Eruvin 16"
hdf.setHebrewFormat(true); // set the formatter to Hebrew
System.out.println(hdf.format(jd)); // output the date in Hebrew "י״ג ניסן תשע״ג"
System.out.println(daf.getMasechta()); // output the masechta name in Hebrew "עירובין"
System.out.println(hdf.formatHebrewNumber(daf.getDaf())); // output the daf number formatted in Hebrew "ט״ז"
System.out.println(hdf.formatDafYomiBavli(daf)); // output the daf and masechta in Hebrew "עירובין ט״ז"
JewishCalendar jd = new JewishCalendar(5772, JewishDate.KISLEV, 25); // set the date to the first day of Chanukah
HebrewDateFormatter hdf = new HebrewDateFormatter();
System.out.println(jd); // output the transliterated date
System.out.println(jd.getDayOfChanukah()); // output 1
System.out.println(hdf.formatYomTov(jd)); // output Chanukah 1
hdf.setHebrewFormat(true); // set the format to Hebrew
System.out.println(hdf.formatHebrewNumber(jd.getDayOfChanukah())); // output 1 in Hebrew
System.out.println(hdf.formatYomTov(jd)); // output Chanukah 1 in Hebrew
Output:
25 Kislev, 5772
1
Chanukah 1
א׳
א׳ חנוכה
Note that there is no simple way at this point to output just Chanukah. This will likely change in the future to match Chanukah to the behavior of other Yomim Tovim.
Loop and output an entire year
JewishCalendar jc = new JewishCalendar(5773, JewishDate.TISHREI, 1);
jc.setInIsrael(false); // false is the default and is set here only for clarity. Set to true for Israel
HebrewDateFormatter hdf = new HebrewDateFormatter();
hdf.setHebrewFormat(true);
HebrewDateFormatter hdfTransliterated = new HebrewDateFormatter();
while (jc.getJewishYear() == 5773) {
String hebrewOutput = hdf.format(jc);
String transliteratedOutput = hdfTransliterated.format(jc);
if (jc.getDayOfWeek() == 7 && jc.getParshah() != JewishCalendar.Parshah.NONE) { // Shabbos with a parsha
hebrewOutput += " - " + hdf.formatParshah(jc);
transliteratedOutput += " - " + hdfTransliterated.formatParshah(jc);
}
if (jc.isYomTov() || jc.isTaanis()) { // includes Chanukah
hebrewOutput += ", " + hdf.formatYomTov(jc);
transliteratedOutput += ", " + hdfTransliterated.formatYomTov(jc);
}
if (jc.isRoshChodesh()) {
hebrewOutput += ", " + hdf.formatRoshChodesh(jc);
transliteratedOutput += ", " + hdfTransliterated.formatRoshChodesh(jc);
}
if (jc.getDayOfOmer() > 0) {
hebrewOutput += ", " + hdf.formatOmer(jc);
transliteratedOutput += ", " + hdfTransliterated.formatOmer(jc);
}
System.out.println(transliteratedOutput + " - " + hebrewOutput);
jc.plusDays(1);
}
The Zmanim API version 1.3.0 was released on March 4th, 2013 – כ״א אדר תשע״ג. Various changes in the new release vs. the previous version 1.2.1 that was released in May 2010 can be seen below. This release includes beta support for Jewish Calendar calculations as well as a number of updated zmanim and refactored code. The Jewish Calendar support in the Zmanim API is based on Avrom Finkelstein’s HebrewDate project released in 2002. Unlike the Zmanim code, the Jewish calendar interfaces may change significantly in the future (see Jay Gindin’s various changes that may make it into this API) and should therefore be considered beta.
Changes in the Zmanim API 1.3.0 release
Refactoring of zmanim calculations to allow more flexibility in generating custom zmanim. New generic methods that take an arbitrary start and end of day for many zmanim such as getSofZmanShma and getPlagHamincha etc. This makes calculating atypical zmanim that I had not anticipated when creating the API much easier. I receive such requests from time to time, and the changes greatly simplify these types of calculations. Examples of recent requests are:
Zman krias shema calculated from alos as 72 minutes before netz and tzais as 7.083 degrees
Plag hamincha from alos 7.083° to tzais 7.083°
Many rounds of general code refactoring to simplify and stabilize the code and make it easier to port. Thank you Jay Gindin for his many suggestions.
Deprecation of the getSofZmanShmaKolEliyahu() method. Thank you to Rabbi Yisroel Boruch Twerski for pointing out that the Yisroel Vehazmanim was incorrect in including this zman. This is something he discussed with Rabbi Harfenes.
Uniformly return zmanim based on sea level sunrise and sunset. Most zmanim had this fix a long time ago, but there were a few zmanim such as candle lighting time that were corrected.
In the past I posted about Android Zmanim, Jay Gindin’s open source Android zmanim project that uses the KosherJava Zmanim API. The fact that Android development uses Java natively means that it is very simple for Android developers to include zmanim in their apps. Since that time there have been a number of other applications (including iPhone apps) using the code. Moshe Berman’s KosherCocoa Objective-C port of the KosherJava Zmanim API will probably result in additional iPhone apps that use the API. Yitzchok’s older Zmanim .NETport of the KosherJava Zmanim API also opened the door for Windows Phonezmanim applications (Ari Polski’s Zmanim app already uses it, and there are others in the works). I may post additional details about some of them in the future, including an updated post about Jay’s latest Android Zmanim release. I am aware of the following mobile applications that use the KosherJava Zmanim API.
There are also a few secular apps that are not zmanim-specific that use the API for sunrise and sunset calculations. One that I know of is Jeffrey Blattman’sDawn to Dusk Widget, and I know that there are some photography apps that use it for sunrise/sunset calculations as well. There are likely more that I have missed, and a few that are in active development but unreleased. If you are aware of any missed apps, please let me know.
Note: This FAQ has been updated for the KosherJava Zmanim 3.x API, which returns zmanim as a Java Instant. In versions prior to 3.0, zmanim were returned as a Date, and printing one directly formatted it in the computer’s default time zone, so in Lakewood the sunrise below appeared as Mon Feb 07 23:27:41 EST 2011, 7 hours early. The fix (formatting with the location’s time zone) is the same in both versions.
Question:
Why does the output of zmanim for a different time zone appear incorrect?
Answer:
One of the common issues encountered by developers using the API is that zmanim generated for a different time zone than the user’s time zone may return output that appears incorrect. For example, a user in Lakewood, NJ trying to calculate sunrise for Yerushalayim may attempt to use the following code:
The issue is simple, and the sunrise and sunset returned above are actually accurate. Zmanim are returned by the Zmanim API as a Java Instant that represents a moment in time (stored internally as the number of seconds and nanoseconds since January 1, 1970 UTC, the Unix epoch). Sunrise in Yerushalayim on February 8th actually happens at 4:27:37 AM UTC. Java is simply outputting the Instant as a String in the ISO 8601 format, which is always in UTC (the trailing Z), regardless of the time zone of the location or the computer. The user probably intends to output the time in IST – Israel Standard Time (“Asia/Jerusalem” in the IANA time zone database). To do this, you have to output the zmanim using a formatter set to use the “Asia/Jerusalem” time zone.
Sunrise: Tue Feb 08 06:27:37 IST 2011
Sunset: Tue Feb 08 17:19:14 IST 2011
Below is the full code example.
import com.kosherjava.zmanim.*;
import com.kosherjava.zmanim.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class FormatZmanim {
public static void main(String[] args) {
String locationName = "Jerusalem";
double latitude = 31.778; // latitude of Har Habayis
double longitude = 35.2354; // longitude of Har Habayis
double elevation = 0; // optional elevation
ZoneId zoneId = ZoneId.of("Asia/Jerusalem"); // a valid IANA time zone ID, see ZoneId.getAvailableZoneIds()
GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, zoneId); // create the location object
ZmanimCalendar zc = new ZmanimCalendar(location); // create the ZmanimCalendar
zc.setLocalDate(LocalDate.of(2011, Month.FEBRUARY, 8)); // set the date
DateTimeFormatter zmanimFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy").withZone(location.getZoneId()); // create the formatter using the location's time zone
System.out.println("Sunrise: " + zmanimFormat.format(zc.getSunrise()));
System.out.println("Sunset: " + zmanimFormat.format(zc.getSunset()));
}
}