根據(jù)經(jīng)緯度計算某一天的日落日出-java版本

前言

目前我所負責的項目,是公司的路燈控制系統(tǒng)。路燈的開關燈時間是需要根據(jù)日出日落來參考設置的。而日出日落其實和經(jīng)緯度、日期相關的。

問題

使用java 計算出指定日期的日出日落

解決

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,//太陽的平均近點角,從太陽觀察到的地球(=從地球看到太陽的)距近日點(近地點)的角度。
                w, //近日點的平均黃道經(jīng)度。
                e, //地球橢圓公轉(zhuǎn)軌道離心率。
                E, //太陽的偏近點角。計算公式見下面。

                x, y,
                v;  //真近點角,太陽在任意時刻的真實近點角。


        M = Revolution(356.0470 + 0.9856002585 * d);//自變量的組成:2000.0時刻太陽黃經(jīng)為356.0470度,此后每天約推進一度(360度/365天
        w = 282.9404 + 4.70935E-5 * d;//近日點的平均黃經(jīng)。

        e = 0.016709 - 1.151E-9 * d;//地球公轉(zhuǎn)橢圓軌道離心率的時間演化。以上公式和黃赤交角公式一樣,不必深究。

        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);
        //計算太陽的黃道坐標。
        x = sr * Cosd(lon);
        y = sr * Sind(lon);
        //計算太陽的直角坐標。
        obl_ecl = 23.4393 - 3.563E-7 * d;
        //黃赤交角,同前。
        z = y * Sind(obl_ecl);
        y = y * Cosd(obl_ecl);
        //把太陽的黃道坐標轉(zhuǎn)換成赤道坐標(暫改用直角坐標)。

        setsRA(Atan2d(y, x));
        setSdec(Atan2d(z, Math.sqrt(x * x + y * y)));
        //最后轉(zhuǎn)成赤道坐標。顯然太陽的位置是由黃道坐標方便地直接確定的,但必須轉(zhuǎn)換到赤
        //道坐標里才能結合地球的自轉(zhuǎn)確定我們需要的白晝長度。

    }
    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 */
                //太陽視半徑,約16分(受日地距離、大氣折射等諸多影響)

                t,          /* Diurnal arc */
                //周日弧,太陽一天在天上走過的弧長。

                tsouth,     /* Time when Sun is at south */
                sidtime;    /* Local sidereal time */
        //當?shù)睾阈菚r,即地球的真實自轉(zhuǎn)周期。比平均太陽日(日常時間)長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;
        //計算觀測地當日中午時刻對應2000.0起算的日數(shù)。

            /* Compute local sideral time of this moment */
        sidtime = Revolution(GMST0(d) + 180.0 + longitude);
        //計算同時刻的當?shù)睾阈菚r(以角度為單位)。以格林尼治為基準,用經(jīng)度差校正。

            /* Compute Sun's RA + Decl at this moment */
        Sun_RA_dec(d, sRA,sdec,sr);
        //計算同時刻太陽赤經(jīng)赤緯。

            /* Compute time when Sun is at south - in hours UT */
        tsouth = 12.0 - Rev180(sidtime - sRA) / 15.0;
        //計算太陽日的正午時刻,以世界時(格林尼治平太陽時)的小時計。

            /* Compute the Sun's apparent radius, degrees */
        sradius = 0.2666 / sr;
        //太陽視半徑。0.2666是一天文單位處的太陽視半徑(角度)。

            /* Do correction to upper limb, if necessary */
        if (upper_limb != 0)
            altit -= sradius;
        //如果要用上邊緣,就要扣除一個視半徑。

            /* Compute the diurnal arc that the Sun traverses to reach */
        //計算周日弧。直接利用球面三角公式。如果碰到極晝極夜問題,同前處理。
            /* 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;
    }

}

驗證

上面的main運行結果是

Mon Feb 05 14:05:00 CST 2018
{sunRise=6:47 , sunSet=17:37 }

web站點查詢下

image.png

基本吻合。

參考

本代碼下載于http://download.csdn.net/download/ban1225ban1225/7617613
感謝作者的貢獻。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 這些經(jīng)緯線是怎樣定出來的呢?地球是在不停地繞地軸旋轉(zhuǎn)(地軸是一根通過地球南北兩極和地球中心的假想線),在地球中腰畫...
    Akitas閱讀 1,385評論 0 2
  • 知識科普 這些經(jīng)緯線是怎樣定出來的呢?地球是在不停地繞地軸旋轉(zhuǎn)(地軸是一根通過地球南北兩極和地球中心的 假想線),...
    Yumazhiyao閱讀 3,057評論 1 3
  • 北師大楚江亭 “龍生龍,鳳生鳳,老鼠的兒子會打洞”,這是我對講座內(nèi)容的通俗理解,其實是指家庭文化背景對孩子一生的影...
    李靜_2bd9閱讀 2,117評論 0 1
  • 彼時,一個是追風少年,迎著風奔跑,追尋那陽光、雨露;一個是花季少女,托著腮凝視,仰望那星群、明月;星云流轉(zhuǎn),日月升...
    一滴水de困惑閱讀 466評論 1 6
  • “哎,聽說你有喜歡的人了,是誰啊?”芊君看向自己肩上顯得小鳥依人的蘇映綿,問道。“易時。”芊君有些震驚:“他?”蘇...
    千軍大人閱讀 615評論 0 0

友情鏈接更多精彩內(nèi)容