美文网首页
获取淘宝服务器时间,用于淘宝、天猫秒杀

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

作者: cain07 | 来源:发表于2020-07-07 18:44 被阅读0次
/**
 * 获取淘宝服务器时间,用于淘宝、天猫秒杀
 */
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));
        }
    }
}

https://blog.csdn.net/qq_32029605/java/article/details/106805359

相关文章

网友评论

      本文标题:获取淘宝服务器时间,用于淘宝、天猫秒杀

      本文链接:https://www.haomeiwen.com/subject/rgyzqktx.html