获取网络时间,注意加上时间差 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);

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM