Class SunTimesCalculator
java.lang.Object
com.kosherjava.zmanim.util.AstronomicalCalculator
com.kosherjava.zmanim.util.SunTimesCalculator
- All Implemented Interfaces:
Cloneable
Implementation of sunrise and sunset methods to calculate astronomical times. This calculator uses the Java algorithm
written by Kevin
Boone that is based on the US Naval Observatory'sAstronomical Almanac and used with his permission. Added to Kevin's
code is adjustment of the zenith to account for elevation. This algorithm returns the same time every year and does not
account for leap years. It is not as accurate as the Jean Meeus based
NOAACalculator that is the default calculator
use by the KosherJava zmanim library. It also does not have an implementation of some solar calculation methods.- Author:
- © Eliyahu Hershfeld 2004 - 2026, © Kevin Boone 2000
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final doubleThe number of degrees of longitude that corresponds to one hour of time difference. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static doubleacosDeg(double x) Return the arc cosine in degrees.private static doubleasinDeg(double x) Return the arc sine in degrees.private static doublecosDeg(double deg) Calculate cosine of the angle in degreesprivate static doublegetApproxTimeDays(int dayOfYear, double hoursFromMeridian, boolean isSunrise) Calculate the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events.Returns the name of the algorithm.private static doublegetCosLocalHourAngle(double sunTrueLongitude, double latitude, double zenith) Calculate the cosine of the Sun's local hour angleprivate static doublegetHoursFromMeridian(double longitude) Get time difference between location's longitude and the Meridian, in hours.private static doublegetLocalMeanTime(double localHour, double sunRightAscensionHours, double approxTimeDays) Calculate local mean time of rising or setting.private static doublegetMeanAnomaly(int dayOfYear, double longitude, boolean isSunrise) Calculate the Sun's mean anomaly in degrees, at sunrise or sunset, given the longitude in degreesdoublegetSolarAzimuth(ZonedDateTime zdt, GeoLocation geoLocation) This calculator class does not implement the getSolarAzimuth method, and throws aUnsupportedOperationException.doublegetSolarElevation(ZonedDateTime zdt, GeoLocation geoLocation) This calculator class does not implement the getSolarElevation method, and throws aUnsupportedOperationException.private static doublegetSunRightAscensionHours(double sunTrueLongitude) Calculates the Sun's right ascension in hours.private static doublegetSunTrueLongitude(double sunMeanAnomaly) Returns the Sun's true longitude in degrees.doublegetTimeAtAzimuth(LocalDate localDate, GeoLocation geoLocation, double azimuth) This calculator class does not implement the getTimeAtAzimuth method, and throws aUnsupportedOperationException.private static doublegetTimeUTC(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean isSunrise) Get sunrise or sunset time in UTC, according to flag.doublegetUTCMidnight(LocalDate localDate, GeoLocation geoLocation) Return the Universal Coordinated Time (UTC) of solar midnight (UTC) for the given day at the given location.doublegetUTCNoon(LocalDate localDate, GeoLocation geoLocation) Return the Universal Coordinated Time (UTC) of solar noon (UTC) for the given day at the given location.doublegetUTCSunrise(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean adjustForElevation) A method that calculates UTC sunrise as well as any time based on an angle above or below sunrise.doublegetUTCSunset(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean adjustForElevation) A method that calculates UTC sunset as well as any time based on an angle above or below sunset.private static doublesinDeg(double deg) The sine in degrees.private static doubletanDeg(double deg) Return the tangent in degrees.Methods inherited from class AstronomicalCalculator
adjustZenith, clone, equals, getDefault, getEarthRadius, getElevationAdjustment, getRefraction, getSolarRadius, hashCode, setEarthRadius, setRefraction, setSolarRadiusModifier and TypeMethodDescription(package private) doubleadjustZenith(double zenith, double elevation) Adjusts the zenith of astronomical sunrise and sunset to account for solar refraction, solar radius and elevation.clone()booleanstatic AstronomicalCalculatorReturns the default class for calculating sunrise and sunset.doubleA method that returns the earth radius in KM.(package private) doublegetElevationAdjustment(double elevation) Method to return the adjustment to the zenith required to account for the elevation.doubleMethod to get the refraction value to be used when calculating sunrise and sunset.doubleMethod to get the sun's radius.inthashCode()voidsetEarthRadius(double earthRadius) A method that allows setting the earth's radius.voidsetRefraction(double refraction) A method to allow overriding the default refraction of the calculator.voidsetSolarRadius(double solarRadius) Method to set the sun's radius.
-
Field Details
-
DEG_PER_HOUR
The number of degrees of longitude that corresponds to one hour of time difference.- See Also:
-
-
Constructor Details
-
SunTimesCalculator
public SunTimesCalculator()Default constructor of the SunTimesCalculator.
-
-
Method Details
-
getCalculatorName
Description copied from class:AstronomicalCalculatorReturns the name of the algorithm.- Specified by:
getCalculatorNamein classAstronomicalCalculator- Returns:
- the descriptive name of the algorithm.
-
getUTCSunrise
public double getUTCSunrise(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean adjustForElevation) Description copied from class:AstronomicalCalculatorA method that calculates UTC sunrise as well as any time based on an angle above or below sunrise. This abstract method is implemented by the classes that extend this class.- Specified by:
getUTCSunrisein classAstronomicalCalculator- Parameters:
localDate- TheLocalDaterepresenting the date to calculate sunrise for.geoLocation- The location information used for astronomical calculation of solar times.zenith- the azimuth below the vertical zenith of 90 degrees. for sunrise typically thezenithused for the calculation uses geometric zenith of 90° andadjuststhis slightly to account for solar refraction and the sun's radius. Another example would beAstronomicalCalendar.getBeginNauticalTwilight()that passesAstronomicalCalendar.NAUTICAL_ZENITHto this method.adjustForElevation- Should the time be adjusted for elevation- Returns:
- The UTC time of sunrise in 24-hour format. 5:45:00 AM will return 5.75. If an error was encountered in the
calculation (expected behavior for some locations such as near the poles,
Double.NaNwill be returned. - See Also:
-
getUTCSunset
public double getUTCSunset(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean adjustForElevation) Description copied from class:AstronomicalCalculatorA method that calculates UTC sunset as well as any time based on an angle above or below sunset. This abstract method is implemented by the classes that extend this class.- Specified by:
getUTCSunsetin classAstronomicalCalculator- Parameters:
localDate- TheLocalDaterepresenting the date to calculate sunset for.geoLocation- The location information used for astronomical calculation of solar times.zenith- the azimuth below the vertical zenith of 90°. For sunset typically thezenithused for the calculation uses geometric zenith of 90° andadjuststhis slightly to account for solar refraction and the sun's radius. Another example would beAstronomicalCalendar.getEndNauticalTwilight()that passesAstronomicalCalendar.NAUTICAL_ZENITHto this method.adjustForElevation- Should the time be adjusted for elevation- Returns:
- The UTC time of sunset in 24-hour format. 5:45:00 AM will return 5.75. If an error was encountered in the
calculation (expected behavior for some locations such as near the poles,
Double.NaNwill be returned. - See Also:
-
sinDeg
The sine in degrees.- Parameters:
deg- the degrees- Returns:
- sin of the angle in degrees
-
acosDeg
Return the arc cosine in degrees.- Parameters:
x- angle- Returns:
- acos of the angle in degrees
-
asinDeg
Return the arc sine in degrees.- Parameters:
x- angle- Returns:
- asin of the angle in degrees
-
tanDeg
Return the tangent in degrees.- Parameters:
deg- degrees- Returns:
- tan of the angle in degrees
-
cosDeg
Calculate cosine of the angle in degrees- Parameters:
deg- degrees- Returns:
- cosine of the angle in degrees
-
getHoursFromMeridian
Get time difference between location's longitude and the Meridian, in hours.- Parameters:
longitude- the longitude- Returns:
- time difference between the location's longitude and the Meridian, in hours. West of Meridian has a negative time difference
-
getApproxTimeDays
Calculate the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events. We need this figure to derive the Sun's mean anomaly.- Parameters:
dayOfYear- the day of yearhoursFromMeridian- hours from the meridianisSunrise- true for sunrise and false for sunset- Returns:
- the approximate time of sunset or sunrise in days since midnight Jan 1st, assuming 6am and 6pm events. We need this figure to derive the Sun's mean anomaly.
-
getMeanAnomaly
Calculate the Sun's mean anomaly in degrees, at sunrise or sunset, given the longitude in degrees- Parameters:
dayOfYear- the day of the yearlongitude- longitudeisSunrise- true for sunrise and false for sunset- Returns:
- the Sun's mean anomaly in degrees
-
getSunTrueLongitude
Returns the Sun's true longitude in degrees.- Parameters:
sunMeanAnomaly- the Sun's mean anomaly in degrees- Returns:
- the Sun's true longitude in degrees. The result is an angle >= 0 and <= 360.
-
getSunRightAscensionHours
Calculates the Sun's right ascension in hours.- Parameters:
sunTrueLongitude- the Sun's true longitude in degrees > 0 and < 360.- Returns:
- the Sun's right ascension in hours in angles > 0 and < 360.
-
getCosLocalHourAngle
Calculate the cosine of the Sun's local hour angle- Parameters:
sunTrueLongitude- the sun's true longitudelatitude- the latitudezenith- the zenith- Returns:
- the cosine of the Sun's local hour angle
-
getLocalMeanTime
private static double getLocalMeanTime(double localHour, double sunRightAscensionHours, double approxTimeDays) Calculate local mean time of rising or setting. By 'local' is meant the exact time at the location, assuming that there were no time zone. That is, the time difference between the location and the Meridian depended entirely on the longitude. We can't do anything with this time directly; we must convert it to UTC and then to a local time.- Parameters:
localHour- the local hoursunRightAscensionHours- the sun's right ascension in hoursapproxTimeDays- approximate time days- Returns:
- the fractional number of hours since midnight as a double
-
getTimeUTC
private static double getTimeUTC(LocalDate localDate, GeoLocation geoLocation, double zenith, boolean isSunrise) Get sunrise or sunset time in UTC, according to flag. This time is returned as a double and is not adjusted for time-zone.- Parameters:
localDate- theLocalDateobject to extract the day of year for calculationgeoLocation- The location information used for astronomical calculation of solar times.zenith- Sun's zenith in degreesisSunrise- True for sunrise and false for sunset.- Returns:
- the time as a double. If an error was encountered in the calculation (expected behavior for some locations such as
near the poles,
Double.NaNwill be returned.
-
getUTCNoon
Description copied from class:AstronomicalCalculatorReturn the Universal Coordinated Time (UTC) of solar noon (UTC) for the given day at the given location. TheNOAACalculator, the default calculator implementation calculates true solar noon, something that can be calculated even in the Arctic / Antarctic where there may be no sunrise or sunset, while theSunTimesCalculatorapproximates it, calculating the time as halfway between sunrise and sunset, something that can't be calculated in Polar regions where there is no sunrise or sunset for part of the year. See The Definition of Chatzos for details on solar noon / midnight calculations.- Specified by:
getUTCNoonin classAstronomicalCalculator- Parameters:
localDate- TheLocalDaterepresenting the date to calculate noon for.geoLocation- The location information used for astronomical calculation of solar times.- Returns:
- The UTC time of solar noon in 24-hour format. 1:45:00 PM will return 13.75. If an error was encountered in the
the calculation (expected behavior for some locations such as near the poles,
Double.NaNwill be returned. - See Also:
-
getUTCMidnight
Description copied from class:AstronomicalCalculatorReturn the Universal Coordinated Time (UTC) of solar midnight (UTC) for the given day at the given location. TheNOAACalculator, the default calculator implementation calculates true solar midnight, something that can be calculated even in the Arctic / Antarctic where there may be no sunrise or sunset, while theSunTimesCalculatorapproximates it, calculating the time as 12 hours after halfway between sunrise and sunset, something that can't be calculated in Polar regions where there is no sunrise or sunset for part of the year. See The Definition of Chatzos for details on solar noon / midnight calculations.- Specified by:
getUTCMidnightin classAstronomicalCalculator- Parameters:
localDate- TheLocalDaterepresenting the date to calculate midnight for. The calculation will be for midnight at the end of the day passed in.geoLocation- The location information used for astronomical calculation of solar times.- Returns:
- The UTC time of solar midnight in in a 24-hour
doubleformat. 1:45:00 AM will return 1.75. If an error was encountered in the calculation (expected behavior for theSunTimesCalculatorat times of the year in Polar regions),Double.NaNwill be returned. - See Also:
-
getSolarAzimuth
This calculator class does not implement the getSolarAzimuth method, and throws aUnsupportedOperationException. Use theNOAACalculatorif this method is required.
Return the Solar Azimuth for the horizontal coordinate system at the given location at the given time. Not corrected for altitude. True south is 180 degrees.- Specified by:
getSolarAzimuthin classAstronomicalCalculator- Parameters:
zdt- TheZonedDateTimeto calculate the azimuth for.acgeoLocation- The location information used for astronomical calculation of solar times.- Returns:
- the solar azimuth in degrees. Astronomical midday would be 180 in the norther hemosphere and 0 in the southern hemosphere. Depending on the location and time of year, sunrise will have an azimuth of about 90° and sunset about 270°.
- Throws:
UnsupportedOperationException- This calculator class does not implement the getSolarAzimuth method. Use theNOAACalculatorinstead.
-
getSolarElevation
This calculator class does not implement the getSolarElevation method, and throws aUnsupportedOperationException. Use theNOAACalculatorif this method is required.
Return the Solar Elevation for the horizontal coordinate system at the given location at the given time. Can be negative if the sun is below the horizon. Not corrected for altitude.- Specified by:
getSolarElevationin classAstronomicalCalculator- Parameters:
zdt- TheZonedDateTimeto calculate the elevation for.geoLocation- The location information used for astronomical calculation of solar times.- Returns:
- solar elevation in degrees. The horizon (calculated in a vacuum using the solar radius as the point) is 090°, civil twilight is -690° etc. This means that sunrise and sunset that do use refraction and are calculated from the upper limb of the sun will return about 0.83390°.
- Throws:
UnsupportedOperationException- This calculator class does not implement the getSolarElevation method. Use theNOAACalculatorinstead.
-
getTimeAtAzimuth
This calculator class does not implement the getTimeAtAzimuth method, and throws aUnsupportedOperationException. Use theNOAACalculatorif this method is required.
Returns the time that the azimuth will occur for the date and location passed to this method. As an example, to know when the sun will be directly west, pass in an azimuth of 270°, and for directly east, pass in an azimith of 90°.- Specified by:
getTimeAtAzimuthin classAstronomicalCalculator- Parameters:
localDate- TheLocalDaterepresenting the date to calculate the time of the azimuth for.geoLocation- The location information used for astronomical calculation of solar times.azimuth- the azimuth to run the calculation for- Returns:
- The UTC time that the azimuth will be reached in a 24-hour
doubleformat. 5:45:00 PM will return 17.75. If an error was encountered in the calculation (expected behavior for some locations such as near the poles, aDouble.NaNwill be returned. - Throws:
UnsupportedOperationException- This calculator class does not implement the getTimeAtAzimuth method. Use theNOAACalculatorinstead.
-