package com.example;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Date extends java.util.Date {
private static final String URL = "http://www.baidu.com";
public Date(){
super(getNetworkTime());
}
public static long getNetworkTime() {
URL url = null;
URLConnection uc = null;
try {
url = new URL(URL);
uc = url.openConnection();
uc.connect();
uc.getDate();
System.out.println(uc.getDate());
return uc.getDate();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return super.toString();
}
}
java 重写 new Date 使用网络时间
于 2023-08-06 13:24:56 首次发布