java timezone解决夏令时、冬令时问题 |
您所在的位置:网站首页 › 什么时候从冬令时变成夏令时 › java timezone解决夏令时、冬令时问题 |
1. 方式一:通过IP查询API,获取当地时间
API说明
https://ipinfo.io/{ip}?token={token} { "ip": "91.80.22.206", "city": "Rome", "region": "Lazio", "country": "IT", "loc": "41.8626,12.5390", "org": "AS30722 Vodafone Italia S.p.A.", "postal": "00178", "timezone": "Europe/Rome" } https://ipinfo.io/103.38.30.114 ?token= fe65864213261b 网址:https://ipinfo.io/signup 注册账号获取token, 限额每月 50000次查询 后端根据 timezone 获取 当地时间 2.方式二:使用Timezone 时区ID,动态获取当地时间 TimeZone tz = TimeZone.getTimeZone("Europe/Berlin"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); df.setTimeZone(tz ); String strDate = df.format(new Date()); System.out.println("柏林当前时间:"+strDate); int rawOffset = tz.getRawOffset(); // 不带令时的与GMT的时差 int offset = tz.getOffset(TimeUtil.GetGreenDate().getTime());// 带时令的与GMT的时差可通过 offset与rawOffset对比,看当前时区是否属于夏令时、冬令时 boolean b = tz.inDaylightTime(new Date());// 判断当前时间是否处于夏令时 public static Date GetGreenDate(){ TimeZone gmtTz = TimeZone.getTimeZone("GMT"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); df.setTimeZone(gmtTz); String strDate = df.format(new Date()); df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parse = null; try { parse = df.parse(strDate); } catch (ParseException e) { } return parse; } 3.模拟测试:断网条件下,java计算"America/New_York" 时区,与UTC时间时差。 服务器时间2022-01-22,时差:-18000000 服务器时间2022-04-22(进入夏令时),时差:-14400000
结论: 1.通过更改服务器时间,能模拟时区是否进入夏令时。 2.java8 timezone内有计算时区是否处于夏令时、冬令时的算法。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |