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("网络时间:"+"HH:mm:ss", 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();}
时间: 2024-12-17 11:36:43