前言
目前我所負(fù)責(zé)的項(xiàng)目,是公司的路燈控制系統(tǒng)。路燈的開關(guān)燈時(shí)間是需要根據(jù)日出日落來參考設(shè)置的。而日出日落其實(shí)和經(jīng)緯度、日期相關(guān)的。
問題
使用java 計(jì)算出指定日期的日出日落
解決
package com.insigmaunited.lightai.util;
import javax.sql.DataSource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
public class SunTimesUtil {
static Double start = 0.0;
static Double end = 0.0;
static Double sRA = 0.0;
static Double sdec = 0.0;
static Double sr = 0.0;
static Double lon = 0.0;
public static void main(String[] args) throws ParseException {
//今天
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());
System.out.println(getSunTimeAtDate(calendar.getTime(),120.2722222,30.333333333 ));
}
public static HashMap<String,Object> getSunTimeAtDate(Date d,Double longitude, Double latitude){
long xcts =Days_since_2000_Jan_0(d);
HashMap<String,Object> hm = new HashMap<>(2);
try {
hm = GetSunTime(xcts,longitude, latitude);
} catch (ParseException e) {
e.printStackTrace();
}
return hm;
}
public static HashMap<String,Object> GetSunTime(long day, Double longitude, Double latitude) throws ParseException
{
SunRiset(day, longitude, latitude, -35.0 / 60.0, 1,start,end);
String sunrise = ToLocalTime(start);
String sunset = ToLocalTime(end);
HashMap<String,Object> hm = new HashMap<>(2);
hm.put("sunRise",sunrise);
hm.put("sunSet",sunset);
return hm;
}
private static String ToLocalTime(Double utTime)
{
int hour = (int) (Math.floor(utTime));
Double temp = utTime - hour;
hour += 8;
temp = temp * 60;
int minute = (int) (Math.floor(temp));
String minuteStr = minute+" ";
if(minute<10){
minuteStr = "0"+minute;
}
return hour+":"+minuteStr;
}
private static void Sunpos(Double d, Double lon, Double r)
{
Double M,//太陽(yáng)的平均近點(diǎn)角,從太陽(yáng)觀察到的地球(=從地球看到太陽(yáng)的)距近日點(diǎn)(近地點(diǎn))的角度。
w, //近日點(diǎn)的平均黃道經(jīng)度。
e, //地球橢圓公轉(zhuǎn)軌道離心率。
E, //太陽(yáng)的偏近點(diǎn)角。計(jì)算公式見下面。
x, y,
v; //真近點(diǎn)角,太陽(yáng)在任意時(shí)刻的真實(shí)近點(diǎn)角。
M = Revolution(356.0470 + 0.9856002585 * d);//自變量的組成:2000.0時(shí)刻太陽(yáng)黃經(jīng)為356.0470度,此后每天約推進(jìn)一度(360度/365天
w = 282.9404 + 4.70935E-5 * d;//近日點(diǎn)的平均黃經(jīng)。
e = 0.016709 - 1.151E-9 * d;//地球公轉(zhuǎn)橢圓軌道離心率的時(shí)間演化。以上公式和黃赤交角公式一樣,不必深究。
E = M + e * Radge * Sind(M) * (1.0 + e * Cosd(M));
x = Cosd(E) - e;
y = Math.sqrt(1.0 - e * e) * Sind(E);
setSr(Math.sqrt(x * x + y * y));
v = Atan2d(y, x);
lon = v + w;
setLon(lon);
if (lon >= 360.0)
{lon -= 360.0;
setLon(lon);}
}
private static void Sun_RA_dec(Double d, Double RA, Double dec, Double r)
{
Double obl_ecl, x, y, z;
Sunpos(d, lon, r);
//計(jì)算太陽(yáng)的黃道坐標(biāo)。
x = sr * Cosd(lon);
y = sr * Sind(lon);
//計(jì)算太陽(yáng)的直角坐標(biāo)。
obl_ecl = 23.4393 - 3.563E-7 * d;
//黃赤交角,同前。
z = y * Sind(obl_ecl);
y = y * Cosd(obl_ecl);
//把太陽(yáng)的黃道坐標(biāo)轉(zhuǎn)換成赤道坐標(biāo)(暫改用直角坐標(biāo))。
setsRA(Atan2d(y, x));
setSdec(Atan2d(z, Math.sqrt(x * x + y * y)));
//最后轉(zhuǎn)成赤道坐標(biāo)。顯然太陽(yáng)的位置是由黃道坐標(biāo)方便地直接確定的,但必須轉(zhuǎn)換到赤
//道坐標(biāo)里才能結(jié)合地球的自轉(zhuǎn)確定我們需要的白晝長(zhǎng)度。
}
private static int SunRiset(long day, Double longitude, Double lat, Double altit, int upper_limb, Double trise, Double tset)
{
Double d, /* Days since 2000 Jan 0.0 (negative before) */
//以歷元2000.0起算的日數(shù)。
sradius, /* Sun's apparent radius */
//太陽(yáng)視半徑,約16分(受日地距離、大氣折射等諸多影響)
t, /* Diurnal arc */
//周日弧,太陽(yáng)一天在天上走過的弧長(zhǎng)。
tsouth, /* Time when Sun is at south */
sidtime; /* Local sidereal time */
//當(dāng)?shù)睾阈菚r(shí),即地球的真實(shí)自轉(zhuǎn)周期。比平均太陽(yáng)日(日常時(shí)間)長(zhǎng)3分56秒。
int rc = 0; /* Return cde from function - usually 0 */
/* Compute d of 12h local mean solar time */
d = day/* Days_since_2000_Jan_0(date)*/ + 0.5 - longitude / 360.0;
//計(jì)算觀測(cè)地當(dāng)日中午時(shí)刻對(duì)應(yīng)2000.0起算的日數(shù)。
/* Compute local sideral time of this moment */
sidtime = Revolution(GMST0(d) + 180.0 + longitude);
//計(jì)算同時(shí)刻的當(dāng)?shù)睾阈菚r(shí)(以角度為單位)。以格林尼治為基準(zhǔn),用經(jīng)度差校正。
/* Compute Sun's RA + Decl at this moment */
Sun_RA_dec(d, sRA,sdec,sr);
//計(jì)算同時(shí)刻太陽(yáng)赤經(jīng)赤緯。
/* Compute time when Sun is at south - in hours UT */
tsouth = 12.0 - Rev180(sidtime - sRA) / 15.0;
//計(jì)算太陽(yáng)日的正午時(shí)刻,以世界時(shí)(格林尼治平太陽(yáng)時(shí))的小時(shí)計(jì)。
/* Compute the Sun's apparent radius, degrees */
sradius = 0.2666 / sr;
//太陽(yáng)視半徑。0.2666是一天文單位處的太陽(yáng)視半徑(角度)。
/* Do correction to upper limb, if necessary */
if (upper_limb != 0)
altit -= sradius;
//如果要用上邊緣,就要扣除一個(gè)視半徑。
/* Compute the diurnal arc that the Sun traverses to reach */
//計(jì)算周日弧。直接利用球面三角公式。如果碰到極晝極夜問題,同前處理。
/* the specified altitide altit: */
Double cost;
cost = (Sind(altit) - Sind(lat) * Sind(sdec)) /
(Cosd(lat) * Cosd(sdec));
if (cost >= 1.0)
{
rc = -1;
t = 0.0;
}
else
{
if (cost <= -1.0)
{
rc = +1;
t = 12.0; /* Sun always above altit */
}
else
t = Acosd(cost) / 15.0; /* The diurnal arc, hours */
}
/* Store rise and set times - in hours UT */
setStart(tsouth - t);
setEnd(tsouth + t);
return rc;
}
private static long Days_since_2000_Jan_0(Date date)
{
String d2000 = "2000-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Long ll = 0L;
try {
ll = date.getTime()-sdf.parse(d2000).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
long day = ll/1000/60/60/24;
return day;
}
private static Double Revolution(Double x)
{
return (x - 360.0 * Math.floor(x * Inv360));
}
private static Double Rev180(Double x)
{
return (x - 360.0 * Math.floor(x * Inv360 + 0.5));
}
private static Double GMST0(Double d)
{
Double sidtim0;
sidtim0 = Revolution((180.0 + 356.0470 + 282.9404) +
(0.9856002585 + 4.70935E-5) * d);
return sidtim0;
}
private static Double Inv360 = 1.0 / 360.0;
private static Double Sind(Double x)
{
return Math.sin(x * Degrad);
}
private static Double Cosd(Double x)
{
return Math.cos(x * Degrad);
}
/* private static Double Tand(Double x)
{
return Math.tan(x * Degrad);
}
private static Double Atand(Double x)
{
return Radge * Math.atan(x);
}
private static Double Asind(Double x)
{
return Radge * Math.asin(x);
}*/
private static Double Acosd(Double x)
{
return Radge * Math.acos(x);
}
private static Double Atan2d(Double y, Double x)
{
return Radge * Math.atan2(y, x);
}
private static Double Radge = 180.0 / Math.PI;
private static Double Degrad = Math.PI / 180.0;
public static Double getStart() {
return start;
}
public static void setStart(Double start) {
SunTimesUtil.start = start;
}
public static Double getsRA() {
return sRA;
}
public static void setsRA(Double sRA) {
SunTimesUtil.sRA = sRA;
}
public static Double getSdec() {
return sdec;
}
public static void setSdec(Double sdec) {
SunTimesUtil.sdec = sdec;
}
public static Double getSr() {
return sr;
}
public static void setSr(Double sr) {
SunTimesUtil.sr = sr;
}
public static Double getLon() {
return lon;
}
public static void setLon(Double lon) {
SunTimesUtil.lon = lon;
}
public static Double getEnd() {
return end;
}
public static void setEnd(Double end) {
SunTimesUtil.end = end;
}
}
驗(yàn)證
上面的main運(yùn)行結(jié)果是
Mon Feb 05 14:05:00 CST 2018
{sunRise=6:47 , sunSet=17:37 }
去web站點(diǎn)查詢下

image.png
基本吻合。
參考
本代碼下載于http://download.csdn.net/download/ban1225ban1225/7617613
感謝作者的貢獻(xiàn)。