
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:
String locationName = "Jerusalem";
double latitude = 31.778; // Har Habayis
double longitude = 35.2354; // Har Habayis
double elevation = 0;
ZoneId zoneId = ZoneId.of("Asia/Jerusalem");
GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, zoneId);
ZmanimCalendar zc = new ZmanimCalendar(location);
zc.setLocalDate(LocalDate.of(2011, Month.FEBRUARY, 8));
System.out.println("Sunrise: " + zc.getSunrise());
System.out.println("Sunset: " + zc.getSunset());While you would expect a sunrise of 6:27:37 AM and sunset of 5:19:14 PM, running this code would generate the following times that appear to be 2 hours early:
Sunrise: 2011-02-08T04:27:37.030110380Z
Sunset: 2011-02-08T15:19:14.914959235ZThe 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.
DateTimeFormatter zmanimFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy").withZone(location.getZoneId());
System.out.println("Sunrise: " + zmanimFormat.format(zc.getSunrise()));
System.out.println("Sunset: " + zmanimFormat.format(zc.getSunset()));This will output the expected:
Sunrise: Tue Feb 08 06:27:37 IST 2011
Sunset: Tue Feb 08 17:19:14 IST 2011Below 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()));
}
}
The technical notes on the original 

Until this point the