使用正则表达式抽取新闻/BBS网页发表时间(修改版)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 分析时间戳
 * 
 * @author xum
 * 
 */
public class PublishTimeExtract {
 
private static final String TIME_REGEX = "((:|>|\\s)?20[0-9]{2}(-|/|\\.|\\u5e74)\\d{1,2}(-|/|\\.|\\u6708)\\d{1,2}(\\u65e5)?\\s?\\d{1,2}(:|\\u65f6)\\d{2}((:|\\u5206)\\d{2})?|(:|>|\\s)?[0-9]{2}\\u5e74\\d{1,2}\\u6708\\d{1,2}(\\u65e5)?\\s?\\d{1,2}(:|\\u65f6)\\d{2}((:|\\u5206)\\d{2})?)";
private static final String TIME_REGEX_1 = "((:|>|\\s)?20[0-9]{2}(-|/|\\.|\\u5e74)\\d{1,2}(-|/|\\.|\\u6708)\\d{1,2}(\\u65e5)?(\\s|&nbsp|<)+|(:|>|\\s)?[0-9]{2}\\u5e74\\d{1,2}\\u6708\\d{1,2}(\\u65e5)?(\\s|&nbsp|<)+)";
private static Pattern pattern = Pattern.compile(TIME_REGEX);
private static Pattern pattern1 = Pattern.compile(TIME_REGEX_1);
private static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static final String BBS_URL = "(http://bbs/\\..*|http://www\\.tianya\\.cn/[a-zA-Z]*forum/content/.*)";
 
/**
 * @param content
 * @param url
 * @return
 */
public static String extractDate(String content, String url) {
 
String con = content
.replaceAll("<!--.*?-->", "");
 
Matcher m = pattern.matcher(con);
Date now = new Date();
 
//
if (url.matches(BBS_URL)) {
 
String dateStr = null;
 
Date date = null;
 
while (m.find()) {
 
dateStr = m.group();
 
if (dateStr == null)
continue;
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
 
dateStr = dateStr.replaceAll("\\u65f6|\\u5206", ":");
 
Date tempDate;
 
try {
tempDate = sdf.parse(changeString2Date(dateStr));
 
if (tempDate.after(now)) {
continue;
}
 
} catch (ParseException e) {
continue;
}
 
if (date == null) {
date = tempDate;
} else if (tempDate.after(date)) {
date = tempDate;
}
}
 
if (date != null) {
 
return date.getTime() + "";
}
 
} else {
 
String dateStr = null;
 
if (m.find()) {
dateStr = m.group();
}
 
if (dateStr != null) {
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
;
dateStr = dateStr.replaceAll("\\u65f6|\\u5206", ":");
 
try {
 
return (sdf.parse(changeString2Date(dateStr)).getTime())
+ "";
 
} catch (ParseException e) {
System.out
.println("=======================>parsedate error: "
+ e.getMessage());
return (new Date()).getTime() + "";
}
}
}
 
m = pattern1.matcher(con);
 
if (url.matches(BBS_URL)) {
 
String dateStr = null;
 
Date date = null;
 
while (m.find()) {
 
dateStr = m.group();
 
if (dateStr == null)
continue;
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
 
dateStr = dateStr.replaceAll("(&nbsp|<)", "");
 
Date tempDate;
 
try {
tempDate = sdf.parse(changeString2Date(dateStr));
 
if (tempDate.after(now)) {
continue;
}
 
} catch (ParseException e) {
continue;
}
 
if (date == null) {
date = tempDate;
} else if (tempDate.after(date)) {
date = tempDate;
}
}
 
if (date != null) {
 
return date.getTime() + "";
}
 
} else {
 
String dateStr = null;
 
if (m.find()) {
dateStr = m.group();
}
 
if (dateStr != null) {
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
;
dateStr = dateStr.replaceAll("(&nbsp|<)", "");
 
try {
 
return (sdf.parse(changeString2Date(dateStr)).getTime())
+ "";
 
} catch (ParseException e) {
System.out
.println("=======================>parsedate error: "
+ e.getMessage());
return (new Date()).getTime() + "";
}
}
}
 
return (new Date()).getTime() + "";
}
 
private static String changeString2Date(String dateStr) {
 
System.out.println("=======================>Pre Date: " + dateStr);
 
String date = "";
 
String[] tempDateStr = dateStr.trim().replaceAll("\\s+", "@")
.replaceAll("(-|:)", "@").split("@");
 
if (tempDateStr.length > 0) {
 
String year = tempDateStr[0];
 
if (year.length() == 2) {
year = "20" + year;
}
 
date = date + year + "-";
}
 
if (tempDateStr.length > 1) {
date = date
+ (tempDateStr[1].length() > 1 ? tempDateStr[1] : "0"
+ tempDateStr[1]) + "-";
}
 
if (tempDateStr.length > 2) {
date = date
+ (tempDateStr[2].length() > 1 ? tempDateStr[2] : "0"
+ tempDateStr[2]) + " ";
}
 
if (tempDateStr.length > 3) {
date = date
+ (tempDateStr[3].length() > 1 ? tempDateStr[3] : "0"
+ tempDateStr[3]) + ":";
} else {
date = date + "00:";
}
 
if (tempDateStr.length > 4) {
date = date
+ (tempDateStr[4].length() > 1 ? tempDateStr[4] : "0"
+ tempDateStr[4]) + ":";
} else {
date = date + "00:";
}
 
if (tempDateStr.length > 5) {
date = date
+ (tempDateStr[5].length() > 1 ? tempDateStr[5] : "0"
+ tempDateStr[5]);
} else {
date = date + "00";
}
 
System.out.println("=======================> Date: " + date);
 
return date;
}
 
public static void main(String[] args) {
 
extractDate(
" <td align=\"center\">http://www.rednet.cn &nbsp;<!--时间开始-->2012/3/2 9:14:36<!--时间结束-->&nbsp;&nbsp;",
"");
 
}

本文转自william_xu 51CTO博客,原文链接:http://blog.51cto.com/williamx/806457,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值