private void GainTime() {
Timer timer;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
URL url = new URL("http://www.baidu.com");
URLConnection connection = url.openConnection();
connection.connect();
//获取网络时间搓
long id = connection.getDate();
//转为时间格式
Date date = new Date(id);
//格式化时间
SimpleDateFormat format = new SimpleDateFormat("网络时间" + "HH:mm:ss", Locale.CHINA);
//时差加GMT+8
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
now = format.format(date);
timeText.setText(now);
Log.i("时间", date.getHours() + "时" + date.getMinutes() + "分"
+ date.getSeconds() + "秒" + "\n" + now);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000);
}