/**
* 同步每天日出日落时间
* 日出+1小时 日落-1小时
*/
@Scheduled(cron = "0 0 4 * * ?")
public void syncSunriseSunset() {
log.info("开始同步日出日落时间");
String[] bjSunTimes = SunriseSunsetAPI.getBjSunTimes();
// 保存16个小时
redisTemplate.opsForValue().set(SUN_TIMES, bjSunTimes[0] + "," + bjSunTimes[1], 16, TimeUnit.HOURS);
log.info("结束同步日出日落时间");
}
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class SunriseSunsetAPI {
private static final String BASE_URL = "https://api.sunrise-sunset.org/json";
// 默认日出时间(早上9点)
private static final String DEFAULT_SUNRISE = "09:00";
// 默认日落时间(下午5点)
private static final String DEFAULT_SUNSET = "17:00";
// 时间格式
private static final SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
public static String[] getBjSunTimes() {
return getSunTimes(39.9042, 116.4074, "today");
}
public static String[] getSunTimes(double lat, double lng, String date) {
String sunriseTime = DEFAULT_SUNRISE;
String sunsetTime = DEFAULT_SUNSET;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = BASE_URL + "?lat=" + lat + "&lng=" + lng + "&date=" + date + "&formatted=0";
HttpGet request = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(request)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
JSONObject json = JSONObject.parseObject(result);
JSONObject results = json.getJSONObject("results");
// 解析日出时间并加1小时
Date sunriseDate = utcFormat.parse(results.getString("sunrise"));
Calendar sunriseCal = Calendar.getInstance();
sunriseCal.setTime(sunriseDate);
sunriseCal.add(Calendar.HOUR_OF_DAY, 1); // 日出时间加1小时
sunriseTime = timeFormat.format(sunriseCal.getTime());
// 解析日落时间并减1小时
Date sunsetDate = utcFormat.parse(results.getString("sunset"));
Calendar sunsetCal = Calendar.getInstance();
sunsetCal.setTime(sunsetDate);
sunsetCal.add(Calendar.HOUR_OF_DAY, -1); // 日落时间减1小时
sunsetTime = timeFormat.format(sunsetCal.getTime());
}
}
} catch (Exception e) {
System.err.println("获取日出日落时间失败,使用默认值");
e.printStackTrace();
}
System.out.println("调整后的日出时间: " + sunriseTime);
System.out.println("调整后的日落时间: " + sunsetTime);
return new String[]{sunriseTime, sunsetTime};
}
public static boolean compareTime(String[] sunTimes) {
// 判断当前时间是否在日出和日落之间
try {
Date now = new Date();
Date currentTime = timeFormat.parse(timeFormat.format(now));
Date sunrise = timeFormat.parse(sunTimes[0]);
Date sunset = timeFormat.parse(sunTimes[1]);
if (currentTime.after(sunrise) && currentTime.before(sunset)) {
return true;
}
} catch (Exception e) {
System.err.println("时间比较出错");
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
getSunTimes(39.9042, 116.4074, "today"); // 北京坐标
}
}
SpringBoot定时获取每天日出日落时间
最新推荐文章于 2025-08-05 18:46:53 发布