/**
* 获取淘宝服务器时间,用于淘宝、天猫秒杀
*/
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
网友评论