Class AstronomicalCalendar

  • All Implemented Interfaces:
    Cloneable
    Direct Known Subclasses:
    ZmanimCalendar

    public class AstronomicalCalendar
    extends Object
    implements Cloneable
    A Java calendar that calculates astronomical times such as sunrise, sunset and twilight times. This class contains a Calendar and can therefore use the standard Calendar functionality to change dates etc... The calculation engine used to calculate the astronomical times can be changed to a different implementation by implementing the abstract AstronomicalCalculator and setting it with the setAstronomicalCalculator(AstronomicalCalculator). A number of different calculation engine implementations are included in the util package. Note: There are times when the algorithms can't calculate proper values for sunrise, sunset and twilight. This is usually caused by trying to calculate times for areas either very far North or South, where sunrise / sunset never happen on that date. This is common when calculating twilight with a deep dip below the horizon for locations as far south of the North Pole as London, in the northern hemisphere. The sun never reaches this dip at certain times of the year. When the calculations encounter this condition a null will be returned when a Date is expected and Long.MIN_VALUE when a long is expected. The reason that Exceptions are not thrown in these cases is because the lack of a rise/set or twilight is not an exception, but an expected condition in many parts of the world. Here is a simple example of how to use the API to calculate sunrise. First create the Calendar for the location you would like to calculate sunrise or sunset times for:
     String locationName = "Lakewood, NJ";
     double latitude = 40.0828; // Lakewood, NJ
     double longitude = -74.2094; // Lakewood, NJ
     double elevation = 20; // optional elevation correction in Meters
     // the String parameter in getTimeZone() has to be a valid timezone listed in
     // TimeZone.getAvailableIDs()
     TimeZone timeZone = TimeZone.getTimeZone("America/New_York");
     GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
     AstronomicalCalendar ac = new AstronomicalCalendar(location);
     
    To get the time of sunrise, first set the date you want (if not set, the date will default to today):
     ac.getCalendar().set(Calendar.MONTH, Calendar.FEBRUARY);
     ac.getCalendar().set(Calendar.DAY_OF_MONTH, 8);
     Date sunrise = ac.getSunrise();
     
    Author:
    © Eliyahu Hershfeld 2004 - 2018
    • Method Detail

      • getSeaLevelSunrise

        public Date getSeaLevelSunrise()
        A method that returns the sunrise without elevation adjustment. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light, something that is not affected by elevation. This method returns sunrise calculated at sea level. This forms the base for dawn calculations that are calculated as a dip below the horizon before sunrise.
        Returns:
        the Date representing the exact sea-level sunrise time. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the page.
        See Also:
        getSunrise(), getUTCSeaLevelSunrise(double), getSeaLevelSunset()
      • getBeginCivilTwilight

        public Date getBeginCivilTwilight()
        A method that returns the beginning of civil twilight (dawn) using a zenith of 96°.
        Returns:
        The Date of the beginning of civil twilight using a zenith of 96°. If the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
        See Also:
        CIVIL_ZENITH
      • getBeginNauticalTwilight

        public Date getBeginNauticalTwilight()
        A method that returns the beginning of nautical twilight using a zenith of 102°.
        Returns:
        The Date of the beginning of nautical twilight using a zenith of 102°. If the calculation can't be computed null will be returned. See detailed explanation on top of the page.
        See Also:
        NAUTICAL_ZENITH
      • getBeginAstronomicalTwilight

        public Date getBeginAstronomicalTwilight()
        A method that returns the beginning of astronomical twilight using a zenith of 108°.
        Returns:
        The Date of the beginning of astronomical twilight using a zenith of 108°. If the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
        See Also:
        ASTRONOMICAL_ZENITH
      • getSunset

        public Date getSunset()
        The getSunset method Returns a Date representing the elevation adjusted sunset time. The zenith used for the calculation uses geometric zenith of 90° plus AstronomicalCalculator.getElevationAdjustment(double). This is adjusted by the AstronomicalCalculator to add approximately 50/60 of a degree to account for 34 archminutes of refraction and 16 archminutes for the sun's radius for a total of 90.83333°. See documentation for the specific implementation of the AstronomicalCalculator that you are using. Note: In certain cases the calculates sunset will occur before sunrise. This will typically happen when a timezone other than the local timezone is used (calculating Los Angeles sunset using a GMT timezone for example). In this case the sunset date will be incremented to the following date.
        Returns:
        the Date representing the exact sunset time. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the page.
        See Also:
        AstronomicalCalculator.adjustZenith(double, double), getSeaLevelSunset(), getUTCSunset(double)
      • getSeaLevelSunset

        public Date getSeaLevelSunset()
        A method that returns the sunset without elevation adjustment. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light, something that is not affected by elevation. This method returns sunset calculated at sea level. This forms the base for dusk calculations that are calculated as a dip below the horizon after sunset.
        Returns:
        the Date representing the exact sea-level sunset time. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the page.
        See Also:
        getSunset(), 2see
      • getEndCivilTwilight

        public Date getEndCivilTwilight()
        A method that returns the end of civil twilight using a zenith of 96°.
        Returns:
        The Date of the end of civil twilight using a zenith of 96°. If the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
        See Also:
        CIVIL_ZENITH
      • getEndNauticalTwilight

        public Date getEndNauticalTwilight()
        A method that returns the end of nautical twilight using a zenith of 102°.
        Returns:
        The Date of the end of nautical twilight using a zenith of 102° . If the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
        See Also:
        NAUTICAL_ZENITH
      • getEndAstronomicalTwilight

        public Date getEndAstronomicalTwilight()
        A method that returns the end of astronomical twilight using a zenith of 108°.
        Returns:
        the Date of the end of astronomical twilight using a zenith of 108°. If the calculation can't be computed, null will be returned. See detailed explanation on top of the page.
        See Also:
        ASTRONOMICAL_ZENITH
      • getTimeOffset

        public Date getTimeOffset​(Date time,
                                  double offset)
        A utility method that returns a date offset by the offset time passed in as a parameter. This method casts the offset as a long and calls getTimeOffset(Date, long).
        Parameters:
        time - the start time
        offset - the offset in milliseconds to add to the time
        Returns:
        the Datewith the offset added to it
      • getTimeOffset

        public Date getTimeOffset​(Date time,
                                  long offset)
        A utility method that returns a date offset by the offset time passed in. Please note that the level of light during twilight is not affected by elevation, so if this is being used to calculate an offset before sunrise or after sunset with the intent of getting a rough "level of light" calculation, the sunrise or sunset time passed to this method should be sea level sunrise and sunset.
        Parameters:
        time - the start time
        offset - the offset in milliseconds to add to the time.
        Returns:
        the Date with the offset in milliseconds added to it
      • getSunriseOffsetByDegrees

        public Date getSunriseOffsetByDegrees​(double offsetZenith)
        A utility method that returns the time of an offset by degrees below or above the horizon of sunrise. Note that the degree offset is from the vertical, so for a calculation of 14° before sunrise, an offset of 14 + GEOMETRIC_ZENITH = 104 would have to be passed as a parameter.
        Parameters:
        offsetZenith - the degrees before getSunrise() to use in the calculation. For time after sunrise use negative numbers. Note that the degree offset is from the vertical, so for a calculation of 14° before sunrise, an offset of 14 + GEOMETRIC_ZENITH = 104 would have to be passed as a parameter.
        Returns:
        The Date of the offset after (or before) getSunrise(). If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the page.
      • getSunsetOffsetByDegrees

        public Date getSunsetOffsetByDegrees​(double offsetZenith)
        A utility method that returns the time of an offset by degrees below or above the horizon of sunset. Note that the degree offset is from the vertical, so for a calculation of 14° after sunset, an offset of 14 + GEOMETRIC_ZENITH = 104 would have to be passed as a parameter.
        Parameters:
        offsetZenith - the degrees after getSunset() to use in the calculation. For time before sunset use negative numbers. Note that the degree offset is from the vertical, so for a calculation of 14° after sunset, an offset of 14 + GEOMETRIC_ZENITH = 104 would have to be passed as a parameter.
        Returns:
        The Dateof the offset after (or before) getSunset(). If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, a null will be returned. See detailed explanation on top of the page.
      • getUTCSunrise

        public double getUTCSunrise​(double zenith)
        A method that returns the sunrise in UTC time without correction for time zone offset from GMT and without using daylight savings time.
        Parameters:
        zenith - the degrees below the horizon. For time after sunrise use negative numbers.
        Returns:
        The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, Double.NaN will be returned. See detailed explanation on top of the page.
      • getUTCSeaLevelSunrise

        public double getUTCSeaLevelSunrise​(double zenith)
        A method that returns the sunrise in UTC time without correction for time zone offset from GMT and without using daylight savings time. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light, something that is not affected by elevation. This method returns UTC sunrise calculated at sea level. This forms the base for dawn calculations that are calculated as a dip below the horizon before sunrise.
        Parameters:
        zenith - the degrees below the horizon. For time after sunrise use negative numbers.
        Returns:
        The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, Double.NaN will be returned. See detailed explanation on top of the page.
        See Also:
        getUTCSunrise(double), getUTCSeaLevelSunset(double)
      • getUTCSunset

        public double getUTCSunset​(double zenith)
        A method that returns the sunset in UTC time without correction for time zone offset from GMT and without using daylight savings time.
        Parameters:
        zenith - the degrees below the horizon. For time after sunset use negative numbers.
        Returns:
        The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, Double.NaN will be returned. See detailed explanation on top of the page.
        See Also:
        getUTCSeaLevelSunset(double)
      • getUTCSeaLevelSunset

        public double getUTCSeaLevelSunset​(double zenith)
        A method that returns the sunset in UTC time without correction for elevation, time zone offset from GMT and without using daylight savings time. Non-sunrise and sunset calculations such as dawn and dusk, depend on the amount of visible light, something that is not affected by elevation. This method returns UTC sunset calculated at sea level. This forms the base for dusk calculations that are calculated as a dip below the horizon after sunset.
        Parameters:
        zenith - the degrees below the horizon. For time before sunset use negative numbers.
        Returns:
        The time in the format: 18.75 for 18:45:00 UTC/GMT. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, Double.NaN will be returned. See detailed explanation on top of the page.
        See Also:
        getUTCSunset(double), getUTCSeaLevelSunrise(double)
      • getTemporalHour

        public long getTemporalHour​(Date startOfday,
                                    Date endOfDay)
        A utility method that will allow the calculation of a temporal (solar) hour based on the sunrise and sunset passed as parameters to this method. An example of the use of this method would be the calculation of a non-elevation adjusted temporal hour by passing in sea level sunrise and sea level sunset as parameters.
        Parameters:
        startOfday - The start of the day.
        endOfDay - The end of the day.
        Returns:
        the long millisecond length of the temporal hour. If the calculation can't be computed a Long.MIN_VALUE will be returned. See detailed explanation on top of the page.
        See Also:
        getTemporalHour()
      • getSunTransit

        public Date getSunTransit()
        A method that returns sundial or solar noon. It occurs when the Sun is transitting the celestial meridian. In this class it is calculated as halfway between sea level sunrise and sea level sunset, which can be slightly off the real transit time due to changes in declination (the lengthening or shortening day).
        Returns:
        the Date representing Sun's transit. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, null will be returned. See detailed explanation on top of the page.
        See Also:
        getSunTransit(Date, Date), getTemporalHour()
      • getSunTransit

        public Date getSunTransit​(Date startOfDay,
                                  Date endOfDay)
        A method that returns sundial or solar noon. It occurs when the Sun is transitting the celestial meridian. In this class it is calculated as halfway between the sunrise and sunset passed to this method. This time can be slightly off the real transit time due to changes in declination (the lengthening or shortening day).
        Parameters:
        startOfDay - the start of day for calculating the sun's transit. This can be sea level sunrise, visual sunrise (or any arbitrary start of day) passed to this method.
        endOfDay - the end of day for calculating the sun's transit. This can be sea level sunset, visual sunset (or any arbitrary end of day) passed to this method.
        Returns:
        the Date representing Sun's transit. If the calculation can't be computed such as in the Arctic Circle where there is at least one day a year where the sun does not rise, and one where it does not set, null will be returned. See detailed explanation on top of the page.
      • getDateFromTime

        protected Date getDateFromTime​(double time,
                                       boolean isSunrise)
        A method that returns a Date from the time passed in as a parameter.
        Parameters:
        time - The time to be set as the time for the Date. The time expected is in the format: 18.75 for 6:45:00 PM.time is sunrise and false if it is sunset
        isSunrise - true if the
        Returns:
        The Date.
      • getSunriseSolarDipFromOffset

        public double getSunriseSolarDipFromOffset​(double minutes)
        Returns the dip below the horizon before sunrise that matches the offset minutes on passed in as a parameter. For example passing in 72 minutes for a calendar set to the equinox in Jerusalem returns a value close to 16.1° Please note that this method is very slow and inefficient and should NEVER be used in a loop. TODO: Improve efficiency.
        Parameters:
        minutes - offset
        Returns:
        the degrees below the horizon before sunrise that match the offset in minutes passed it as a parameter.
        See Also:
        getSunsetSolarDipFromOffset(double)
      • getSunsetSolarDipFromOffset

        public double getSunsetSolarDipFromOffset​(double minutes)
        Returns the dip below the horizon after sunset that matches the offset minutes on passed in as a parameter. For example passing in 72 minutes for a calendar set to the equinox in Jerusalem returns a value close to 16.1° Please note that this method is very slow and inefficient and should NEVER be used in a loop. TODO: Improve efficiency.
        Parameters:
        minutes - offset
        Returns:
        the degrees below the horizon after sunset that match the offset in minutes passed it as a parameter.
        See Also:
        getSunriseSolarDipFromOffset(double)
      • getGeoLocation

        public GeoLocation getGeoLocation()
        A method that returns the currently set GeoLocation which contains location information used for the astronomical calculations.
        Returns:
        Returns the geoLocation.
      • setGeoLocation

        public void setGeoLocation​(GeoLocation geoLocation)
        Sets the GeoLocation Object to be used for astronomical calculations.
        Parameters:
        geoLocation - The geoLocation to set.
      • getCalendar

        public Calendar getCalendar()
        returns the Calendar object encapsulated in this class.
        Returns:
        Returns the calendar.
      • setCalendar

        public void setCalendar​(Calendar calendar)
        Parameters:
        calendar - The calendar to set.