Skip to content

Instantly share code, notes, and snippets.

@jialinxie
Forked from junlincao/DayTime.java
Created August 5, 2017 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jialinxie/570021909e3fa049d93356f9393d10b6 to your computer and use it in GitHub Desktop.
Save jialinxie/570021909e3fa049d93356f9393d10b6 to your computer and use it in GitHub Desktop.
根据经纬度计算日出与日落时间
/**
* 根据经纬度计算日出日落时间
* @param lat 纬度
* @param lng 经度
* @param isSunRise true计算日出,false计算日落
* @return int[] 0位置为小时,1位置为分钟
*/
public static int[] getDayTime(double lat, double lng, boolean isSunRise){
lat = Math.toRadians(lat);
Calendar cNow = Calendar.getInstance();
Calendar cStart = Calendar.getInstance();
cStart.set(2000, 0, 1);
//2000年1月1日到今天天数
int dayCount = (int) ((cNow.getTimeInMillis() - cStart.getTimeInMillis()) / (1000 * 3600 * 24));
double ut0 = 180;
double utStart = 0;
final double h = Math.sin(Math.toRadians(-0.833));
for(; Math.abs(utStart - ut0) >= 0.1; ut0 = utStart){
double t = (dayCount + ut0 / 360d) / 36525; // 世纪数
double L = 280.460 + 36000.777 * t; // 太阳平均黄径
double G = Math.toRadians(357.528 + 35999.050 * t); // 太阳平近点角
double lamda = Math.toRadians(L + 1.915 * Math.sin(G) + 0.020 * Math.sin(2 * G)); // 太阳黄道经度
double epc = Math.toRadians(23.4393- 0.0130 * t); // 地球倾角
double sigam = Math.asin(Math.sin(epc) * Math.sin(lamda));// 太阳的偏差
// 格林威治时间太阳时间角
double gha = ut0 - 180 - 1.915 * Math.sin(G) - 0.020 * Math.sin(2 * G)
+ 2.466 * Math.sin(2 * lamda) - 0.053 * Math.sin(4 * lamda);
// 修正值e
double e = Math.toDegrees(Math.acos((h - Math.tan(lat) * Math.tan(sigam))));
utStart = ut0 - gha - lng + (isSunRise ? -e : e);
}
int zone = (int) (lng / 15 + (lng >= 0 ? 1 : -1));// 当前时区
return new int[]{
(int) (utStart / 15 + zone ),
(int) (60 * (utStart / 15 + zone - (int) (utStart / 15 + zone)))
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment