웹 페이지에서 다양한 형식의 릴리스 시간이 추출되고 릴리스 시간은 일반 "yyyy-mm-dd hh : mm : ss"형식으로 표현됩니다. 정확도 만 추구하려고 시도 할 수 있지만 네트워크 릴리스 시간의 형식은 매우 유연하기 때문에 100% 정확할 수 없습니다.
패키지 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; 수입 whu.utils.timeutil; /*** 2014 년 3 월 13 일 오후 2:49:05에 생성* @description 웹 페이지의 게시 시간을 얻으십시오*/public class fetchpubtime {/**는 URL에서 8 개의 연속 날짜를 나타냅니다 (예 : http://www.baidu.com/20140311/2356.html*/private string _whole =). "([-|/| _] {1} 20 // d {6})"; /**는 1 년, 월 및 날짜로-///로 분리 된 날짜를 나타냅니다 (예 : http://www.baidu.com/2014-3-11/2356.html*/private static string url_reg_sep_ymd = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; /**는 날짜가 - 또는/, 연도 및 월에 의해 분리 된 것을 나타냅니다. 예를 들어 http://www.baidu.com/2014-3/2356.html*/private static string url_reg_sep_ym = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; 개인 정적 캘린더 전류 = 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? 1-9]) | ([1-2] [0-9])))) | (// d {2} (([02468] [1235679]) | ([13579] [01345789 ]))) [//-////s]? (((((0? [13578])) | (1 [02])) [0-9] (3 [01])) | ((0? [469]) | (11)) [//-////s]? ) | (30)) | (0? 2 [//-/// s]? / ** * @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 ( "")) returtpextPagedate (urlContent); } return pubtime; } / ** URL에서 게시 시간을 추출하고 yyyy-mm-dd hh : mm : ss-formatted string * @param url * @return * / public static string getPubTimeFromUrl (String URL) {Pattern P_WHOLE = PATUCT.COMPILE (url_reg_whole); 매치 자 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 ()); // 각 단계는 (current.compareto (timeutil.strtocalendar (time, "yyyymmdd))> = 0) {return time.substring (0,4)+"-"-"+time.substring (4,6)+"-"+time.substring (6,8)+"+"+"00:00:00:00:00:0"; }} p_whole = null; m_whole = null; 패턴 p_sep = pattern.compile (url_reg_sep_ymd); 매치 자 m_sep = p_sep.matcher (url); if (m_sep.find (0) && m_sep.groupcount ()> 0) {String time = m_sep.group (0); time = time.substring (1, time.length ()); 문자열 [] 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; 패턴 p_sep_ym = pattern.compile (url_reg_sep_ym); 매치 자 M_SEP_YM = P_SEP_YM.MATCHER (URL); if (m_sep_ym.find (0) && m_sep_ym.groupCount ()> 0) {String time = m_sep_ym.group (0); time = time.substring (1, time.length ()); Calendar thetime = calendar.getInstance (); 문자열 [] 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;} /** 웹 페이지에서 출판 시간을 추출하십시오. 2013-12-19 15:48:33 또는 2013-12-19 또는 2012/3/05* @return Return Date* @Author : Oschina* @CreateTime*/Public STATIC STRINGPERTINGPADEDATE (string text) {boolean dateStrest = retplacecaml. " null; pattern p_etail = patternpile ( "(20 // d {2} [-/]/] [1,2} [-/] // d {1,2} // d {1,2} : // d {1,2} : // d {1,2}) | pattern.case_insensitive | pattern.multiline); // 연도, 월, 일, 날, 시간, 분, 두 번째라면 P = (20 // d {2} [-/] // d {1,2} [-/] // d {1, Pattern.case_insensitive | Pattern.multiline); // matcher matcher = p.matcher (datest); matcher matcher_detail = p_detail.matcher (datest); if (! containshms = true; } else matcher_detail = p_detail.matcher (dateSt); if (matcher_detail.find () && mayser_detail.groupCount ()> = 1) {matches = new ArrayList (); for (int i = 1; i <= mayser_detail.groupCount (); i ++) {String temp = mayser_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 ( "/", "-"). replare ( "Year", "-"). relgip ( "Month", "-"). replare ( "day", "-"); if (current.compareto (timeutil.strtocalendar (pubtime, "yyyy-mm-dd")> = 0) {if (contasshms) pubtime+= ""+"00:00:00"; if (pubtime.matches (righttimereg)) {return pubtime; }}}} else {return null; }} catch (예외 e) {return null; } return null; }}위의 내용은이 기사에 관한 모든 것입니다. 모든 사람들이 Java 프로그래밍을 배우는 것이 도움이되기를 바랍니다.