Webページのさまざまな形式のリリース時間が抽出され、リリース時間は通常の「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; import whu.utils.timeutil; /*** 2014年3月13日午後2時49分5分に作成* @description Webページの公開時間を取得*/public class fetchpubtime {/**は、http://www.baidu.com/20140311/2356.html*/private static url_eg_eg_leg_ reg url_eg_ reg url_eg_wholの8つの連続した日付を表します。 "([ - |/| _] {1} 20 // d {6})"; /**は、http://www.baidu.com/2014-3-11/2356.html*/private static string url_reg_sep_yp_yp_ymd =など、1年、月、日付で区切られた日付を表します。 "([ - |/| _] {1} 20 // d {2} [ - |/| _] {1} // d {1,2})"; /**日付は、または/月と月のみで区切られていることを示します。 "([ - |/| _] {1} 20 // d {2} [ - |/| _] {1} // d {1,2})";プライベート静的カレンダー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])))))))|(( 11))[//- //// s]?((0?[1-9])|([1-2] [0-9])|(30)))|(0?2 [//- //// s]?((0?」 1-9])|([1-2] [0-9])))))))))))))) ])[//- //// s]?(((((0?[13578]))|(1 [02]))[//- ///// s]?(((0?[1-9])|([1-2]) [0-9])|(3 [01]))))))))))|(11)[//- ///// s]?((0?[1-9]))|([1-2] [0-9]] )|(30))|(0?2 [//- /// s]?((0?[1-9])|(1 [0-9])|(2 [0-8])))))))))))))); $ "; / ** * @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); } pubtimeを返します。 } / ** URLから公開時間を抽出し、yyyy-mm-dd hh:mm:mm:ss-formatted string * @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()); //各ステップは現在の時刻を超えることができません(current.compareTo(tamtil.strtocalendar(time、 "yyyymmdd"))> = 0){return time.substring(0,4)+" - "+time.substring(4,6)+" - "+time.Substring(6,8) }} p_whole = null; m_whole = null;パターン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 = m_sep.group(0); time = time.substring(1、time.length()); string [] seg = time.split( "[ - |/| _] {1}");カレンダー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); matcher 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());カレンダー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 ";} /** null;} /** Webページソースコードから出版時間を抽出します。 2013-12-19 15:48:33または2013-12-19または2012/3/05* @return return Date* @author:@createTime:2013年1月21日リストマッチ= null;パターンp_detail = pattern.compile( "(20 // d {1,2} [ - /] // d {1,2} // d {1,2}:// d {1,2}:// d {1,2} day) "、pattern.case_insensitive | pattern.multiline); //それが年、月、日、時間、分、秒、次の場合、次のパターンに従ってからp = pattern.compile("(20/] // d {1,2} [ - - /] // d {1,2})|(1,2})月、2番目) "、pattern.case_insensive | 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); cantaplshms = 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()。トリム(); //最初の値を取り出しますpubtime = pubtime.replace( "/"、 " - ")。置き換え( "year"、 " - ")。 if(current.comPareto(tamutil.strtocalendar(pubtime、 "yyyy-mm-dd")> = 0){if(contatrshms)pubtime+= ""+"00:00:00"; if(pubtime.matches(righttimereg)){pubtimeを返します。 }}}} else {return null; }} catch(例外e){return null; } nullを返します。 }}上記はこの記事に関するものです。誰もがJavaプログラミングを学ぶことが役立つことを願っています。