首页 > 安卓中如何获取网络时间?

安卓中如何获取网络时间?

URL urlTime=new URL("http://www.bjtime.cn");//取得资源对象
URLConnection uc=urlTime.openConnection();//生成连接对象
uc.connect(); //发出连接
long ld=uc.getDate(); //取得网站日期时间
Date date=new Date(ld); //转换为标准时间对象

在uc.getDate();处报异常,此方法是不是不能直接用?

URLConnection getDate方法返回的是原始服务器消息发出的时间,并非标准的网络时间。getDate方法本身不存在抛出异常。你遇到了什么异常?

【热门文章】
【热门文章】