码迷,mamicode.com
首页 > 其他好文 > 详细

获取网络时间失败,求大神指点

时间:2017-06-08 21:24:25      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:onclick   format   ==   color   new t   catch   locale   nts   star   

private void GainTime() {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("http://www.taobao.com");
URLConnection uc=url.openConnection();
uc.connect();
long id=uc.getDate();
Date date=new Date(id);
SimpleDateFormat formats=new SimpleDateFormat("网络时间:"+"HHmmss", Locale.CHINA);
nowTime = formats.format(date);
Log.i("时间", date.getHours() + "" + date.getMinutes() + ""
+ date.getSeconds() + "" + "\n" + nowTime);

}catch (Exception e){
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}

}).start();

handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

time_texts.setText(nowTime);
if (nowTime==null||nowTime.equals("")){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
time_texts.setText(sdf.format(new Date()) + "(" + "系统时间" + ")");
}
}

};


}
public void onClick(View view){
GainTime();
}

获取网络时间失败,求大神指点

标签:onclick   format   ==   color   new t   catch   locale   nts   star   

原文地址:http://www.cnblogs.com/Dream0304/p/6964719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!