獲取網絡時間,注意加上時間差 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