从指定的网站、网页上上获取时间,mysql时间精确到毫秒秒或者毫秒

使用正则表达式,从网站上获取指定数据 - 博客频道 - CSDN.NET
崔成龙 . 勇往直前
拼搏人生,以成败论英雄!-廊坊师范学院信息技术提高班第八期
分类:【java基础】
& & & &最近做的一个项目中,其中有这样一个需求:用户要求我们实时在地图上显示某些指定景点的人数,但是却没有给我们数据的接口。不过可以从网页上获取到最新的数据,每小时更新一次。所以经理安排我做一个实时从网页上抓取数据的功能。
& & & &既然是网页,那么无用的数据肯定是非常多的,所以就需要用正则表达式来过滤出自己所需要的数据。
& & & &不得不说,正则表达式比substring好用多了,而且效率也很不错。下面来分享一下我的这段代码吧:
* 从网站获取日期信息
* @Title: getDate
上午09:42:26
private String getDate() {
// 从网站抓取数据
String table = catchData();
String date = &&;
// 使用正则表达式,获取对应的数据
Pattern places = pile(&(&p align=\&center\&&)([^\\s]*)&);
Matcher matcher = places.matcher(table);
while (matcher.find()) {
System.out.println(matcher.group(2));
date = matcher.group(2);
* 从网站抓取数据(未经处理)
* @Title: getData
上午09:34:30
@SuppressWarnings(&unchecked&)
private String catchData() {
String table = &&;
Map map = new HashMap();
map.put(&a&, &1&);// 莫删,否则报错
table = AsyncRequestUtil.getJsonResult(map, &.cn/flow.php&);
} catch (Exception e) {
e.printStackTrace();
【AsyncRequestUtil.java】
package com.zhjy.zydc.
import java.util.M
* 异步请求数据
: Cuichenglong
* @Version
上午09:54:20
public class AsyncRequestUtil {
* 异步请求数据
* @Title: getJsonResult
* @param map
* @param strURL
public static String getJsonResult(Map&String, Object& map, String strURL)throws Exception {
/** 跨域登录,获取返回结果
String result =
result = UrlUtil.getDataFromURL(strURL, map);
if (result!=null && result.startsWith(&null{&)) {
result = result.substring(&null&.length());
【UrlUtil .java】
package com.zhjy.zydc.
import java.io.BufferedR
import java.io.InputStreamR
import java.io.OutputStreamW
import java.io.UnsupportedEncodingE
import java.net.URL;
import java.net.URLC
import java.net.URLD
import java.net.URLE
import java.util.ArrayL
import java.util.E
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import java.util.S
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpS
* url跨域获取数据
: Cuichenglong
: Zhong Hai Ji Yuan
* @Version
下午04:14:26
public final class UrlUtil {
* 根据URL跨域获取输出结果
* @Title: getDataFromURL
* @param strURL 要访问的URL地址
* @param param 参数
* @return 结果字符串
* @throws Exception
public static String getDataFromURL(String strURL, Map&String, Object& param) throws Exception{
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(5000); //允许5秒钟的延迟:连接主机的超时时间(单位:毫秒)
conn.setReadTimeout(5000); //允许5秒钟的延迟 :从主机读取数据的超时时间(单位:毫秒)
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
final StringBuilder sb = new StringBuilder(param.size() && 4); // 4次方
final Set&String& keys = param.keySet();
for (final String key : keys) {
Object value = param.get(key);
sb.append(key); // 不能包含特殊字符
sb.append('=');
//如果格式为String类型,则进行2次解码、2次编码操作
if (value instanceof String) // String
value = (URLDecoder.decode(URLDecoder.decode((String)value, &utf-8&),
&utf-8&));
value = (Object)(URLEncoder.encode(URLEncoder.encode((String)value, &utf-8&),
&utf-8&));
value = URLEncoder.encode((String)value, &utf-8&);
sb.append(value);
sb.append('&');
// 将最后的 '&' 去掉
sb.deleteCharAt(sb.length() - 1);
// writer.write(&email=&password=123&);
writer.write(sb.toString());
writer.flush();
writer.close();
InputStreamReader reder = new InputStreamReader(conn.getInputStream(), &utf-8&);
BufferedReader breader = new BufferedReader(reder);
// BufferedWriter w = new BufferedWriter(new FileWriter(&d:/1.txt&));
String content =
String result =
while ((content = breader.readLine()) != null) {
& & & &这是一段很简单的代码。是从.cn/flow.php网址中抓取日期的代码。
& & & &其实只有2步,第一步是通过java.net.URL后台访问指定网址,并且拿到页面的html源码。第二步从html源码中通过正则表达式,获取日期。这里说一下正则表达式:
Pattern places = pile(&(&p align=\&center\&&)([^\\s]*)&);
& & & &其中^表示屏蔽,\s表示空格,所以^\\s表示屏蔽空格,这里第一个\是转移字符。*表示匹配多个字符。
& & & &通过matcher,获取匹配的值。在一次matcher.find()获取每一次匹配的数据,一个括号对应一个group。如果取matcher.group(1),则会取到&p align=&center&&。matcher.group(2)可以取到匹配的值。
& & & 了解了这些,我们就可以随意的从某些网址上抓取数据了。有了数据,还有什么可以挡住我们前进的脚步!!!
xiaoxian8023
排名:第473名
(29)(26)(19)(3)(1)(12)(8)(16)(9)(4)flash 中如何得到精确到毫秒的计时器?
[问题点数:120分]
flash 中如何得到精确到毫秒的计时器?
[问题点数:120分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2011年5月 C/C++大版内专家分月排行榜第二2010年9月 C/C++大版内专家分月排行榜第二
2009年11月 C/C++大版内专家分月排行榜第三
2009年1月 VC/MFC大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。所以这里提醒做非常精确的时间统计的朋友,谨慎使用System.currentTimeMillis() 。
在Java中可以通过System.currentTimeMillis()或者System.nanoTime() (JDK&=5.0) 方法获得当前的时间的精确值。但是通过阅读Javadoc,我们发现这两个方法并不一定保证得到你所期望的精度。先来看 System.currentTimeMillis():
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
JDK5.0加入的System.nanoTime()方法。Javadoc对该方法的描述如下:
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
它返回系统能够提供的最为精确的计时,以纳秒(10亿分之一秒)为单位,但并不保证纳秒级精度。
如果你的Java程序需要高精度的计时,如1毫秒或者更小,使用System.nanoTime()方法,它完全可以满足你的需求。
阅读(...) 评论()在unix上如何取精确的时间啊精确到毫秒 - Linux/Unix当前位置:& &&&在unix上如何取精确的时间啊精确到毫秒在unix上如何取精确的时间啊精确到毫秒&&网友分享于:&&浏览:34次在unix下怎么取精确的时间啊,精确到毫秒我维护的一个unix下的程序,每收到一包数据后,按照内部协议对数据进行封装,需要为数据包加上时间标签,我现在用的取本地时间的函数(localtime/ftime)可以精确到毫秒,但是感觉取值不是很准,当收到连续的数据包离的很近的时候,取到的本地时间是一样的,连毫秒也是一样的,而且我现在取到的时间,毫秒的个位都是0,比如510ms\820ms\910ms,感觉50毫秒之内的包基本都是一样时间标签,想请问一下,如何才能取到精确的时间呢,精确到毫秒,------解决方案--------------------us&比&ms&的精度高,只要ms,可以丢弃高精度的部分
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有按键精灵时间精确到毫秒执行_百度知道

我要回帖

更多关于 获取指定时间的毫秒数 的文章

 

随机推荐