标签:分析 String sep url matcher Zookeeper 源码 Pattern time
download:Zookeeper源码分析
Zookeeper作为阿里系与Dubbo完美搭配的注册中心,其江湖地位毋容置疑。本课程以最开始图解Zookeeper框架的架构和组件作为课程总起,进而“分而治之”各个击破Zookeeper框架的各个组件源码,如:序列化,数据模型,持久化,Client和Server架构、选举流程等,最后通过总结快速梳理所得。
适合人群
有代码洁癖的中高级Java工程师
从事分布式系统设计和开发的极客
认可“源码驱动式”学习方式的学生党
技术储备要求
Java基础扎实,对IO/NIO网络通信等知识有一定了解
了解序列化、持久化、多线程
了解zookeeper的一般使用命令
package whu.extract.pubtime.core;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import whu.utils.TimeUtil;
/**
- Created On 2014年3月13日 下午2:49:05
-
@description 獲取網頁的發佈時間
/
public class FetchPubTime {
/ 表示url中連續的8位日期,
private static String url_regwhole= "([-|/|]{1}20\d{6})";
/ 表示 用-或者/隔開的日期,有年月日的
private static String url_reg_sepymd = "([-|/|]{1}20\d{2}[-|/|]{1}\d{1,2}[-|/|]{1}\d{1,2})";
/ 表示 用-或者/隔開的日期,只要年和月份的
private static String url_reg_sepym = "([-|/|]{1}20\d{2}[-|/|_]{1}\d{1,2})";
private static Calendar current = Calendar.getInstance();
/ 格式正確的時間正則表達式/
private static String rightTimeReg = "^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$";/**
- @param url
- @param urlContent
-
@return
*/
public static String getPubTimeVarious(String url,String urlContent) {String pubTime = getPubTimeFromUrl(url);
//链接里面沒有,匹配文本中的
if(pubTime == null)
{
if(urlContent!=null&&!urlContent.trim().equals(""))
return extractPageDate(urlContent);
}return pubTime;
}
/**從url里面抽取動身佈時間,返回YYYY-MM-DD HH:mm:ss格式的字符串
- @param url
-
@return
*/
public static String getPubTimeFromUrl(String url)
{
Pattern p_whole = Pattern.compile(url_reg_whole);
Matcher m_whole = p_whole.matcher(url);
if(m_whole.find(0)&&m_whole.groupCount()>0)
{
String time = m_whole.group(0);
time = time.substring(1,time.length());
//每一步都不可以超出當前時間
if(current.compareTo(TimeUtil.strToCalendar(time, "yyyyMMdd"))>=0)
{
return time.substring(0,4)+"-"+time.substring(4,6)+"-"+
time.substring(6,8)+" "+"00:00:00";
}
}p_whole = null;
m_whole = null;
Pattern p_sep = Pattern.compile(url_reg_sep_ymd);
Matcher m_sep = p_sep.matcher(url);
if(m_sep.find(0)&&m_sep.groupCount()>0)
{
String time = msep.group(0);
time = time.substring(1,time.length());
String[] seg = time.split("[-|/|]{1}");
Calendar theTime = Calendar.getInstance();
theTime.set(Calendar.YEAR,Integer.parseInt(seg[0]));
theTime.set(Calendar.MONTH, Integer.parseInt(seg[1]));
theTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(seg[2]));
if(current.compareTo(theTime)>=0)
{return seg[0]+"-"+seg[1]+"-"+seg[2]+" "+"00:00:00"; }
}
p_sep = null;
m_sep = null;
Pattern p_sep_ym = Pattern.compile(url_reg_sep_ym);
Matcher m_sep_ym = p_sep_ym.matcher(url);
if(m_sep_ym.find(0)&&m_sep_ym.groupCount()>0)
{
String time = m_sepym.group(0);
time = time.substring(1,time.length());
Calendar theTime = Calendar.getInstance();
String[] seg = time.split("[-|/|]{1}");
theTime.set(Calendar.YEAR,Integer.parseInt(seg[0]));
theTime.set(Calendar.MONTH, Integer.parseInt(seg[1]));
theTime.set(Calendar.DAY_OF_MONTH, 1);
if(current.compareTo(theTime)>=0)
{return seg[0]+"-"+seg[1]+"-"+"01"+" "+"00:00:00"; }
}
return null;
}
/** 從網頁源码中取動身佈時間
- java中正則表達式提取字符串中日期完成代码
- 2013年12月19日15:58:42
- 讀取出2013-12-19 15:48:33或者2013-12-19或者2012/3/05方式的時間
- @param text 待提取的字符串
- @return 返回日期
- @author: oschina
-
@Createtime: Jan 21, 2013
*/
public static String extractPageDate(String text) {
boolean containsHMS =false;
String dateStr = text.replaceAll("r?n", " ");
try {
List matches = null;
Pattern p_detail = Pattern.compile("(20\d{2}[-/]\d{1,2}[-/]\d{1,2} \d{1,2}:\d{1,2}:\d{1,2})|(20\d{2}年\d{1,2}月\d{1,2}日)", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
//假如是僅僅抽取年月日,則依照上面的,假如是抽取年月日-時分秒,則依照下面的
Pattern p = Pattern.compile("(20\d{2}[-/]\d{1,2}[-/]\d{1,2})|(20\d{2}年\d{1,2}月\d{1,2}日)", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
//Matcher matcher = p.matcher(dateStr);
Matcher matcher_detail = p_detail.matcher(dateStr);if(!(matcher_detail.find(0) && matcher_detail.groupCount() >= 1)) { matcher_detail = p.matcher(dateStr); containsHMS = true; }else matcher_detail = p_detail.matcher(dateStr); if (matcher_detail.find() && matcher_detail.groupCount() >= 1) { matches = new ArrayList(); for (int i = 1; i <= matcher_detail.groupCount(); i++) { String temp = matcher_detail.group(i); matches.add(temp); } } else { matches = Collections.EMPTY_LIST; } if (matches.size() > 0) { for(int i=0;i<matches.size();i++) { String pubTime = matches.get(i).toString().trim(); //取出第一個值 pubTime = pubTime.replace("/", "-").replace("年", "-").replace("月", "-").replace("日", "-"); if(current.compareTo(TimeUtil.strToCalendar(pubTime, "yyyy-MM-dd"))>=0) { if(containsHMS) pubTime+=" "+"00:00:00"; if(pubTime.matches(rightTimeReg)) { return pubTime; } } } } else { return null; }
} catch (Exception e) {
return null;
}
return null;
}
}
标签:分析,String,sep,url,matcher,Zookeeper,源码,Pattern,time 来源: https://blog.51cto.com/u_15161311/2703051
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。