android获取网络时间,不需要后台返回服务器时间字段

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34581102/article/details/82857013
    //获取网络时间
    private void getNetTime() {
        LogUtil.i("wjw","getNetTime打印了.....");
        new Thread(){
            @Override
            public void run() {
                URL url = null;//取得资源对象
                try {
                    LogUtil.i("wjw","run打印了.....");
                    url = new URL("http://www.baidu.com");
                    //url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
//                    url = new URL("http://www.bjtime.cn");
                    URLConnection uc = url.openConnection();//生成连接对象
                    uc.connect(); //发出连接
                    //取得网站日期时间(时间戳)
                    mLd = uc.getDate();
//                    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    Calendar calendar = Calendar.getInstance();
//                    calendar.setTimeInMillis(ld);
//                    final String format = formatter.format(calendar.getTime());//年月日时分秒
                    LogUtil.i("wjw","当前网络时间为"+ mLd);//时间戳
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

猜你喜欢

转载自blog.csdn.net/qq_34581102/article/details/82857013