首页 > 编程知识 正文

js获取淘宝服务器时间,手机获得淘宝服务器时间

时间:2023-05-06 15:13:11 阅读:215320 作者:307

获取淘宝服务器时间,用于淘宝、天猫秒杀

/** * 获取淘宝服务器时间,用于淘宝、天猫秒杀 */public class TaobaoTime { public static void main(String[] args) throws Exception { final String url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp"; Response response = new OkHttpClient.Builder() .build() .newCall( new Request.Builder() .url(url) .get() .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36") .build()) .execute(); final JSONObject result = JSONObject.parseObject(response.body().byteStream(), StandardCharsets.UTF_8, JSONObject.class); final long epochMilli = result.getJSONObject("data").getLong("t"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.of("Asia/Shanghai")); while (true) { Thread.sleep(1000L); now = now.plusSeconds(1L); System.out.println(formatter.format(now)); } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。