//获取网络时间,子线程调用
private String getNetTime() {
URL url = null;//取得资源对象
try {
url = new URL("http://www.baidu.com");
//url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
//url = new URL("http://www.bjtime.cn");
URLConnection uc = url.openConnection();
uc.connect(); //发出连接
long ld = uc.getDate(); //取得网站日期时间
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(ld);
final String format = formatter.format(calendar.getTime());
return format;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String getLocalTime() {
URL url = null;//取得资源对象
try {
DateFormat formatter = new SimpleDateFo
Android 获取网络时间、系统时间
最新推荐文章于 2024-04-14 13:33:56 发布