利用正则解析国家授时中心页面上的时间
来源:互联网 发布:经典版聚宝盆软件下载 编辑:程序博客网 时间:2024/05/01 08:40
对于程序开发来说,尤其是游戏程序开发,光光客户端的时间系统是满足不了需求的。下面利用正则解析国家授时中心网络服务器:
国家授时中心网络服务器网址为:http://www.time.ac.cn/stime.asp 。
代码如下:
/** * @author long * @param args */public static void main(String[] args) {String url = "http://www.time.ac.cn/stime.asp";String content = download(url);Pattern p1 = Pattern.compile("hrs\\s+=\\s+\\d{1,2}");Pattern p2 = Pattern.compile("min\\s+=\\s+\\d{1,2}");Pattern p3 = Pattern.compile("sec\\s+=\\s+\\d{1,2}");Matcher m1 = p1.matcher(content);Matcher m2 = p2.matcher(content);Matcher m3 = p3.matcher(content);if (m1.find()) {System.out.println("the hour is---->"+ m1.group().replace("hrs = ", ""));}if (m2.find()) {System.out.println("the hour is---->"+ m2.group().replace("min = ", ""));}if (m3.find()) {System.out.println("the hour is---->"+ m3.group().replace("sec = ", ""));}
/** * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容 1.创建一个URL对象 * 2.通过URL对象,创建一个HttpURLConnection对象 3.得到InputStram 4.从InputStream当中读取数据 * @author long * @param urlStr * @return */public static String download(String urlStr) {StringBuffer sb = new StringBuffer();String line = null;BufferedReader buffer = null;try {// 创建一个URL对象URL url = new URL(urlStr);// 创建一个Http连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 使用IO流读取数据buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));while ((line = buffer.readLine()) != null) {sb.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {buffer.close();} catch (Exception e) {e.printStackTrace();}}return sb.toString();}这里没有截取年月日,但是道理一样。
- 利用正则解析国家授时中心页面上的时间
- 中国国家授时中心的时间服务器IP地…
- Windows国家授时中心服务器时间同步
- [转]取标准时间 // 中国国家授时中心的IP地址是:210.72.145.44
- 标准时间源——中国国家授时中心
- internet时间同步服务器地址(中国国家授时中心)
- linux 同步授时中心的时间
- 国家授时中心服务器IP地址
- 国家授时中心NTP服务器IP地址
- GSM模块通过国家网络授时中心获取网络时间同步
- 从授时服务器上获得时间
- 从授时服务器上获得时间
- 国家授时中心:2015年6月30日全世界钟表将拨慢一秒钟
- 卫星定位授时相关的时间算法
- android配置时间服务器+亚洲主要的授时服务器
- android配置时间服务器+亚洲主要的授时服务器
- 页面上处理时间的正则表达式的写法!(笔记)
- setTimeout用法:利用setTimeout在页面上不停的显示当前日期时间
- 【朴素贝叶斯】实战朴素贝叶斯_代码实现_训练算法
- 无符号数unsigned shor %u 取反输出--你值得拥有
- C++笔试笔记
- 前端代码标准最佳实践:javascript篇
- 关于数据库的连接,以及对数据增删该查的操作的封装
- 利用正则解析国家授时中心页面上的时间
- Easy Squeezy, lemon peasy
- 获取某月当天的时间(凌晨和夜晚)
- windows xp下: oracle服务重建
- forbid user to shrink winform or exit using message correlation mechanism
- 循环语句的效率
- 前端代码标准最佳实践:CSS
- Web应用程序开发方法研究
- 兼容Firefox和IE的onpropertychange事件oninput