SpringBoot定时获取每天日出日落时间

    /**
     * 同步每天日出日落时间
     * 日出+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"); // 北京坐标
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值