http请求gmt时间_获取网络时间,注意加上时间差 GMT+8

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);

}


版权声明:本文为weixin_42133753原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。