java 用java计算日出和日落
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4935960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating sunrise and sunset with java
提问by lony
I search a way to calculate sunrise and -set for a given timezone aka location. I would like something similar to Zend_Date.
我搜索了一种计算给定时区(又名位置)的日出和设置的方法。我想要类似于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
干杯和感谢,
-lony
采纳答案by dogbane
Take a look at sunrisesunsetlib-javawhich calculates the sunrise/sunset times from GPS coordinates and a date.
查看sunrisesunsetlib-java,它根据GPS 坐标和日期计算日出/日落时间。
回答by simon04
You might want to use commons-suncalc, a dependency-free library for Java ?7:
你可能想使用commons-suncalc,一个 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());
回答by Meno Hochschild
Just now, I have released Time4J-v4.29which also supports sunrise/sunset-calculations. Example:
刚刚,我发布了Time4J-v4.29,它也支持sunrise/sunset-calculations。例子:
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:
Java-8 兼容性:
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()
.
类SolarTime
(类型Moment
or PlainTimestamp
)的结果可以很容易地转换为 Java-8 类型,如Instant
或LocalDateTime
,通常通过调用方法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.
主要算法与NOAA使用的算法相同(主要区别在于 Time4J 也考虑了 delta-T 计算)。NOAA 算法(实际上与 Jean Meeus 开发的算法相同)比@dogbane 的回答中提到的图书馆 sunsetsunsetlib 使用的 Williams 算法更精确,尤其是在极地附近或极地地区。但是,Time4J 还通过指定计算器名称来支持威廉姆斯算法。这个简单的算法在正常的地理位置仍然可用,并产生 1-2 分钟的精度。
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.
还支持一些噱头,如蓝色小时、各种暮光定义、日照长度或确定极夜/午夜太阳或观察者海拔的影响,请参阅 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.
回答by James D
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:
我们还为此提供了一个 Web API,Web API 和 Java 库都可以在以下位置找到:
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
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:
该库和 Web API 是商业性的,但我们向符合条件的非营利组织、研究人员和开源项目提供免费访问 Web API。对于商业用户,定价信息位于:
The library that gives sunrise and sunset (among other things) is the "Astronomy" one in the list.
提供日出和日落(除其他外)的图书馆是列表中的“天文学”。
回答by Jose Florido
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.
它非常容易使用。它确实以格林威治标准时间响应时间,因此如果需要,您需要进行纬度/经度到时区的转换。