获取当天日出和日落的时间java不使用第三方库
时间: 2023-12-24 07:04:26 浏览: 194
可以使用Java内置的Calendar类来获取当天的日出和日落时间。一般来说,我们可以使用一些简单的算法来计算这些时间。以下是一个基于经纬度和当前日期时间的简单算法:
```java
import java.util.Calendar;
import java.util.TimeZone;
public class SunriseSunsetCalculator {
private static final double ZENITH = 90.8333; // 太阳的真实高度,以度为单位
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
double longitude = 116.4074; // 经度
double latitude = 39.9042; // 纬度
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
double sunriseTime = getSunriseTime(year, month, day, longitude, latitude, tz);
double sunsetTime = getSunsetTime(year, month, day, longitude, latitude, tz);
System.out.println("日出时间:" + formatTime(sunriseTime));
System.out.println("日落时间:" + formatTime(sunsetTime));
}
private static double getSunriseTime(int year, int month, int day, double longitude, double latitude, TimeZone tz) {
double noonTime = getNoonTime(year, month, day, longitude, tz);
double timeDiff = getEquationOfTime(year, month, day) / 60;
double sunriseTime = noonTime - (4 * (longitude + 7.5) / 60) - timeDiff;
return sunriseTime;
}
private static double getSunsetTime(int year, int month, int day, double longitude, double latitude, TimeZone tz) {
double noonTime = getNoonTime(year, month, day, longitude, tz);
double timeDiff = getEquationOfTime(year, month, day) / 60;
double sunsetTime = noonTime + (4 * (longitude - 7.5) / 60) - timeDiff;
return sunsetTime;
}
private static double getNoonTime(int year, int month, int day, double longitude, TimeZone tz) {
Calendar cal = Calendar.getInstance(tz);
cal.set(year, month - 1, day, 12, 0, 0);
double jd = getJulianDay(cal);
double noonTime = getSolarNoon(jd, longitude);
return noonTime;
}
private static double getSolarNoon(double jd, double longitude) {
double t = calcTimeJulianCent(jd);
double eqTime = calcEquationOfTime(t);
double solarNoonOffset = 720 - (longitude * 4) - eqTime;
double solarNoonTime = jd + (solarNoonOffset / 1440);
return solarNoonTime;
}
private static double getJulianDay(Calendar cal) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
if (month <= 2) {
year -= 1;
month += 12;
}
int A = year / 100;
int B = 2 - A + (A / 4);
double JD = Math.floor(365.25 * (year + 4716)) + Math.floor(30.6001 * (month + 1)) + day + B - 1524.5;
double fraction = hour / 24.0 + minute / 1440.0 + second / 86400.0;
return JD + fraction;
}
private static double calcTimeJulianCent(double jd) {
return (jd - 2451545.0) / 36525.0;
}
private static double calcGeomMeanLongSun(double t) {
double L0 = 280.46646 + t * (36000.76983 + t * 0.0003032);
while (L0 > 360.0) {
L0 -= 360.0;
}
while (L0 < 0.0) {
L0 += 360.0;
}
return L0; // in degrees
}
private static double calcGeomMeanAnomalySun(double t) {
return 357.52911 + t * (35999.05029 - 0.0001537 * t);
}
private static double calcEquationOfTime(double t) {
double epsilon = calcObliquityCorrection(t);
double l0 = calcGeomMeanLongSun(t);
double e = calcEccentricityEarthOrbit(t);
double m = calcGeomMeanAnomalySun(t);
double y = Math.tan(Math.toRadians(epsilon) / 2.0);
y *= y;
double sin2l0 = Math.sin(2.0 * Math.toRadians(l0));
double sinm = Math.sin(Math.toRadians(m));
double cos2l0 = Math.cos(2.0 * Math.toRadians(l0));
double sin4l0 = Math.sin(4.0 * Math.toRadians(l0));
double sin2m = Math.sin(2.0 * Math.toRadians(m));
double Etime = y * sin2l0 - 2.0 * e * sinm + 4.0 * e * y * sinm * cos2l0
- 0.5 * y * y * sin4l0 - 1.25 * e * e * sin2m;
return Math.toDegrees(Etime) * 4.0; // in minutes of time
}
private static double calcObliquityCorrection(double t) {
double e0 = calcMeanObliquityOfEcliptic(t);
double omega = 125.04 - 1934.136 * t;
double e = e0 + 0.00256 * Math.cos(Math.toRadians(omega));
return e; // in degrees
}
private static double calcMeanObliquityOfEcliptic(double t) {
double seconds = 21.448 - t
* (46.8150 + t * (0.00059 - t * (0.001813)));
double e0 = 23.0 + (26.0 + (seconds / 60.0)) / 60.0;
return e0; // in degrees
}
private static double calcEccentricityEarthOrbit(double t) {
return 0.016708634 - t * (0.000042037 + 0.0000001267 * t);
}
private static String formatTime(double time) {
int hour = (int) time;
int minute = (int) ((time - hour) * 60);
return String.format("%02d:%02d", hour, minute);
}
}
```
这个算法可以计算日出和日落的时间,需要提供经纬度和当前日期。在这个示例中,我们使用北京市的经纬度(经度为116.4074度,纬度为39.9042度)来计算当天的日出和日落时间。
阅读全文