Home  >  Q&A  >  body text

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

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();处报异常,此方法是不是不能直接用?
PHP中文网PHP中文网2741 days ago253

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:33:14

    URLConnection getDate method returns the time when the original server message was sent, not the standard network time. The getDate method itself does not throw an exception. What exception did you encounter?

    reply
    0
  • Cancelreply