简体   繁体   中英

Calculating sunrise and sunset with java

I search a way to calculate sunrise and -set for a given timezone aka location. I would like something similar to Zend_Date .

The idea is to have an application where you can select a location and based on that you get the actual time and also sunrise, -set time.

Cheers and thanks,
-lony

看看sunrisesunsetlib-java ,它根据GPS坐标和日期计算日出/日落时间。

Computing sunrise and sunset is actually a very difficult problem to solve and most libraries that claim to do it use overly simple models of the solar system that don't actually produce accurate results.

The orbits of the Earth about the Sun and the Moon about the Earth are irregular, the spin axis of the Earth wobbles in two different ways (precession and nutation), and the time scales used by astronomers don't agree with coordinated universal time (UTC). On top of that, atmospheric refraction distorts the perceived position of celestial bodies (especially near the horizon); the rising and setting of the Sun and Moon are typically defined by the first appearance of the disk above the horizon, not the passing of the center of the disk; and the perceived size of the disk varies over time according to its distance from the observer.

I needed an accurate computation of this for a project I was working on and wrote a library to do it that incorporates all of the state-of-the-art models.

We also offer a web API for this and both the Web API and the Java Library can be found at:

http://askgeo.com/

That site also offers a lot of other information that depends on latitude and longitude. You can read about the details of the Library here:

http://askgeo.com/database/Astronomy

The library and web API are commercial but we give free access to the Web API to qualifying non-profits, researchers, and open source projects. For commercial users, the pricing information is at:

http://askgeo.com/#pricing

The library that gives sunrise and sunset (among other things) is the "Astronomy" one in the list.

Just now, I have released Time4J-v4.29 which also supports sunrise/sunset-calculations . Example:

 SolarTime hamburg = SolarTime.ofLocation(53.55, 10.0);
 Optional<Moment> result = PlainDate.nowInSystemTime().get(hamburg.sunrise());
 System.out.println(result.get().toZonalTimestamp(() -> "Europe/Berlin"));

Java-8-compatibility:

The results of class SolarTime (of type Moment or PlainTimestamp ) can easily be converted to Java-8-types like Instant or LocalDateTime , usually by calling the method toTemporalAccessor() .

About the algorithm:

The main algorithm is the same as used by NOAA (with the main difference that delta-T-calculations are also taken into account by Time4J). The NOAA-algorithm (which is practically the same as developed by Jean Meeus) is more precise than the Williams-algorithm used by the library sunrisesunsetlib mentioned in the answer of @dogbane, especially near or in the polar regions. However, Time4J also supports the Williams-algorithm by specifying the calculator name. This simple algorithm is still usable in normal geographic locations and yields 1-2-minute-precision.

Other features:

Some gimmicks like blue hour, various twilight definitions , length of sunshine or determining polar night / midnight sun or the impact of observers altitude are also supported, see the API.

Note about precision:

What so ever, we should always keep in mind that topologic facts like mountains or special weather conditions have strong impact on the real times of sunrise/sunset and cannot be modelled in any library supporting sunrise/sunset-calculations.

Android:

For the Android-platform, please use Time4A (the sister of Time4J) with optimized resource access.

You might want to use commons-suncalc , a dependency-free library for Java ⩾7:

Date date = // date of calculation
double lat, lng = // geolocation
SunTimes times = SunTimes.compute()
            .on(date)       // set a date
            .at(lat, lng)   // set a location
            .execute();     // get the results
System.out.println("Sunrise: " + times.getRise());
System.out.println("Sunset: " + times.getSet());

You could use this free webservice to get sunsrise and sunset times

It's very easy to use. It does response the times in GMT, so you need to do the lat/lon to timezone conversion if you need that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM