当前位置: 首页 > news >正文

7timer.info 免费天气预报对接记录

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.client.RestTemplate;import java.time.*;
import java.time.format.DateTimeFormatter;public class SevenTimerWeather {public static void main(String[] args) throws Exception {String url = "http://www.7timer.info/bin/astro.php"+ "?lon=114.54503&lat=38.138258&ac=0&lang=en&unit=metric&output=json&tzshift=0";RestTemplate restTemplate = new RestTemplate();String json = restTemplate.getForObject(url, String.class);ObjectMapper mapper = new ObjectMapper();JsonNode root = mapper.readTree(json);// 起报时间(UTC)String initStr = root.get("init").asText();LocalDateTime initUtc = LocalDateTime.parse(initStr, DateTimeFormatter.ofPattern("yyyyMMddHH"));ZonedDateTime initBj = initUtc.atZone(ZoneOffset.UTC).withZoneSameInstant(ZoneId.of("Asia/Shanghai"));// 当前北京时间ZonedDateTime nowBj = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));// 选择最接近当前时间的预报JsonNode dataseries = root.get("dataseries");JsonNode bestNode = null;long bestDiff = Long.MAX_VALUE;for (JsonNode node : dataseries) {int tp = node.get("timepoint").asInt();ZonedDateTime forecastTime = initBj.plusHours(tp);long diff = Math.abs(Duration.between(forecastTime, nowBj).toMinutes());if (diff < bestDiff) {bestDiff = diff;bestNode = node;}}if (bestNode != null) {int temp = bestNode.get("temp2m").asInt();                        // 室外温度int rh = bestNode.get("rh2m").asInt();                             // 湿度%String windDir = bestNode.get("wind10m").get("direction").asText();int windSpeed = bestNode.get("wind10m").get("speed").asInt();int transparency = bestNode.get("transparency").asInt();double feelsLike = calcFeelsLike(temp, rh, windSpeed);String windDirCn = windDirToChinese(windDir);String visibility = transparencyToKm(transparency);ZonedDateTime forecastTime = initBj.plusHours(bestNode.get("timepoint").asInt());System.out.println("预报时间:" + forecastTime);System.out.println("室外温度:" + temp + " ℃");System.out.println("体感温度:" + String.format("%.1f ℃", feelsLike));System.out.println("风向:" + windDirCn);System.out.println("风速:" + windSpeed + " m/s");System.out.println("能见度:" + visibility);String weatherText = getWeatherText(bestNode.get("cloudcover").asInt(),bestNode.get("prec_type").asText());System.out.println("天气:" + weatherText);}}// 根据 cloudcover 和 prec_type 推断天气文本private static String getWeatherText(int cloudcover, String precType) {if ("snow".equalsIgnoreCase(precType)) {if (cloudcover >= 7) return "大雪";else if (cloudcover >= 4) return "中雪";else return "小雪";} else if ("rain".equalsIgnoreCase(precType)) {if (cloudcover >= 7) return "大雨";else if (cloudcover >= 4) return "中雨";else return "小雨";} else { // 无降水if (cloudcover <= 2) return "晴";else if (cloudcover <= 5) return "少云/多云";else if (cloudcover <= 7) return "阴";else return "阴天";}}// 风向中文转换private static String windDirToChinese(String dir) {switch (dir) {case "N": return "北风";case "NE": return "东北风";case "E": return "东风";case "SE": return "东南风";case "S": return "南风";case "SW": return "西南风";case "W": return "西风";case "NW": return "西北风";default: return dir;}}// 透明度 -> 能见度公里数private static String transparencyToKm(int t) {switch (t) {case 1: return "≥20 km";case 2: return "16–20 km";case 3: return "12–16 km";case 4: return "8–12 km";case 5: return "4–8 km";case 6: return "2–4 km";case 7: return "1–2 km";case 8: return "0.5–1 km";case 9: return "<0.5 km";default: return "未知";}}// 简易体感温度计算private static double calcFeelsLike(double tempC, int humidity, double windMs) {if (tempC >= 27) {// 热指数公式 (近似)return -8.784695 + 1.61139411 * tempC + 2.338549 * humidity- 0.14611605 * tempC * humidity- 0.012308094 * tempC * tempC- 0.016424828 * humidity * humidity+ 0.002211732 * tempC * tempC * humidity+ 0.00072546 * tempC * humidity * humidity- 0.000003582 * tempC * tempC * humidity * humidity;} else if (tempC <= 10) {// 风寒指数公式double windKmh = windMs * 3.6;return 13.12 + 0.6215 * tempC - 11.37 * Math.pow(windKmh, 0.16)+ 0.3965 * tempC * Math.pow(windKmh, 0.16);} else {return tempC;}}
}
  • lat纬度 (Latitude)

    • 表示南北位置

    • 北纬为正,南纬为负

  • lon经度 (Longitude)

    • 表示东西位置

    • 东经为正,西经为负

坐标系使用WGS84格式,可以在谷歌地图拾取

http://www.zskr.cn/news/12144.html

相关文章:

  • 用标准版平板干翻上代Pro,小米又想学苹果了?
  • 记录一下第一次为Dify贡献插件的经历
  • Visual Prompt Builder-AI 提示词可视化工具 - 详解
  • STM32H743-ARM例程2-UART命令控制LED - 实践
  • 12_TCP和UDP实现服务端和客户端的通信
  • Day22super详解
  • 2025年值得选的文件摆渡系统品牌解析
  • 分布式专题——14 RabbitMQ之集群实战 - 指南
  • QT打包工具
  • QT与Spring Boot通信:实现HTTP请求的完整指南 - 教程
  • linux docker 配置外网拉镜像
  • 实用指南:【JavaEE初阶】多线程重点知识以及常考的面试题-多线程进阶(三)
  • 阿里云边缘安全加速ESA
  • 本土项目管理工具Gitee如何助力企业数字化转型
  • 基于OPC UA协议的SIMATIC PLC通信实现
  • Transformer模型/注意力机制/目标检测/语义分割/图神经网络/强化学习/生成式模型/自监督学习/物理信息神经网络等 - 指南
  • 实用指南:解析前端框架 Axios 的设计理念与源码
  • Gitee Wiki:AI赋能的下一代研发知识管理平台如何重塑软件行业协作范式
  • Autodesk Moldflow 2026下载地址与安装教程
  • 深入解析:Java SOA集成:从“混乱“到“有序“的3步蜕变之旅!
  • 程序员利用Python分析股票赚钱,开发了股票行情看板
  • 实用指南:C#上位机软件:1.7 熟悉VS并开启你的第一个C#程序
  • 界面控件DevExpress WinForms v25.1 - AI聊天控件功能持续增强
  • K8S Deployment 学习
  • 基于菲涅尔积分的角锥喇叭方向图计算
  • Flask的ORM工具SQLAlchemy
  • 构建复合AI系统以实现可扩展工作流
  • 实验任务1
  • 61.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--提取金额 - 实践
  • 使用 Ansible 部署 Elasticsearch 集群