作为java的初学者,我正在尝试一些简单的案例。从this SO post和2nd link到那里给出的pdf文件,我已经开发了这段代码:
import java.time.LocalTime;
import java.util.Calendar;
import static java.lang.Math.acos;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.tan;
/**
* Created by rudra on 17/02/18.
*/
public class PathOfSun {
static public double getTime() {
LocalTime loacltime = LocalTime.now();//.getHour();
int tzone = 5; //TODO: placeholder, will be calculated
// int localhour = time.getHour();
int hour = loacltime.getHour();
int min = loacltime.getMinute();
int sec = loacltime.getSecond();
double lat = latlang.Lat;
double lang = latlang.Lang;
Calendar calendar = Calendar.getInstance();
int dayofyear = calendar.get(Calendar.DAY_OF_YEAR);
int daysthisyear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
double pi = Math.PI;
double gamma = (2 * pi / daysthisyear) * (dayofyear - 1 + (hour - 12) / 24);
double eqtime = 229.18 * (0.000075 + 0.001868 * cos(gamma) - 0.032077 * sin(gamma) -
0.014615 * cos(2 * gamma) - 0.04849 * sin(2 * gamma));
double decl = 0.006918 - 0.399912 * cos(gamma) + 0.070257 * sin(gamma) - 0.006758 * cos(2 * gamma)
+ 0.000907 * sin(2 * gamma) - 0.002697 * cos(3 * gamma) + 0.00148 * sin(3 * gamma);
double toffset = eqtime + 4 * lang + 60 * tzone;
double tst = 60 * hour + min + sec / 60 + toffset;
double ha = tst / 4 - 180;
// Zenith
double phi = acos(sin(lat) * sin(decl) + cos(lat) * cos(decl) * cos(ha));
// Azimuth
double theta = 180 - acos((sin(lat) * cos(phi) - sin(decl)) / (cos(lat) * sin(phi)));
// Calculating the hourAngle as in second page
// At sunrise
double ha2 = acos(cos(90.8*180/pi)/cos(lat)*cos(decl)-tan(lat)*tan(decl));
double stime = 720 - 4 * (lang - ha2) - eqtime;
System.out.println("Time of Sun "+ stime);
return stime;
}
当被称为:
时 PathOfSun.getTime();
给出了错误的时间,意味着日落和日出(+/- ha2)的极度值几乎相同。
我的代码中有错误吗? 请帮助我做到这一点。
答案 0 :(得分:0)
我的建议,
查看您在帖子中提供的NOAA给定链接中的日出和日落特殊情况。
由于太阳和地球大气层的折射,日出和日落时间是特殊情况。因此,可能需要考虑这些特殊情况,以便日出和日落的返回时间是正确的。
您可能想要考虑的另一个特殊情况是中午时间。程序是否按此计算,并且返回的时间在NOAA clock的给定时间内是否正确?该链接显示了不同时区的时间。
对于包括我自己在内的大多数程序员来说,谚语和一句名言都是熟悉和挣扎,而魔鬼就是细节"。在这种情况下,问题的特殊情况或边缘情况。
任何问题和/或澄清,发表评论并尽力而为。