Waktu rilis berbagai format di halaman web diekstraksi, dan waktu rilis diekspresikan dalam format "yyyy-mm-dd hh: mm: ss" "yyyy-mm-dd: mm: ss". Anda hanya dapat mencoba untuk mengejar keakuratan, tetapi karena format waktu rilis jaringan sangat fleksibel, tidak dapat 100% benar.
paket whu.extract.pubtime.core; Impor java.util.arraylist; impor java.util.calendar; impor java.util.collections; impor java.util.list; import java.util.regex.matcher; impor java.util.regex.pattern; impor whu.utils.timeutil; /*** Dibuat pada 13 Maret 2014 jam 2:49:05 pm* @description Dapatkan waktu penerbitan halaman web*/kelas publik fetchpubtime {/** mewakili 8 tanggal berturut -turut dalam url, misalnya http://www.baidu.com/20140311/2356.htp:htmol =/waidu.com/20140311/2356.HTMLE = "([-|/| _] {1} 20 // d {6})"; /** mewakili tanggal yang dipisahkan oleh-atau/dengan tahun, bulan dan tanggal, misalnya http://www.baidu.com/2014-3-11/2356.html*/string statis privat URL_REG_SEP_YMD = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; /** menunjukkan bahwa tanggal dipisahkan oleh - atau/, hanya tahun dan bulan, misalnya, http://www.baidu.com/2014-3/2356.html*/string statis privat URL_REG_SEP_YM = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; Kalender statis pribadi saat ini = calendar.getInstance (); / ** Ekspresi waktu reguler yang diformat dengan benar*/ string statis privat 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])))))))))))))))? $ "; / ** * @param url * @param urlcontent * @return */ public static string getPubTimevarious (string url, string urlContent) {string pubtime = getPubTimefromUrl (url); // tidak di tautan, mencocokkan if (pubtime == null) {if (urlcontent! = Null &&! Urlcontent.trim (). Equals ("")) return extractPageDate (urlContent); } return pubtime; } / ** Ekstrak waktu penerbitan dari URL dan kembalikan yyyy-mm-dd hh: mm: string yang diformat SS * @param url * @return * / string statis public getPubTimefromUrl (string url) {pola p_whole = pola.compile (url_reg_whole); Pencocokan 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 ()); // Setiap langkah tidak dapat melebihi waktu saat ini jika (Current.Compareto (TimeUtil.strtocalendar (waktu, "yyyymmdd"))> = 0) {return time.substring (0,4)+"-"+time.substring (4,6)+"-"+time.substring (6,8)+"+"+""; }} p_whole = null; m_whole = null; Pola p_sep = pola.compile (url_reg_sep_ymd); Pencocokan 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}"); Kalender 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.ComePareto (thetime)> = 0) {return Seg [0]+"-"+Seg [1]+"-"+seg [2]+""+"00:00:00"; }} p_sep = null; m_sep = null; Pola p_sep_ym = pola.compile (url_reg_sep_ym); Pencocokan 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 ()); Kalender 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;} /** Ekstrak waktu publikasi dari WebPage Code Sumber* Ekspresi Ekspresi Tanggal dalam Kode Implementasi dalam Tanggal dalam Tanggal dalam Kode Implementasi Dalam JAV DALAM JAV DALAM JAV DALAM JAV DALAM JAV DALAM JAVE DALAM JAVE DALAM PENGEMBALIAN DALAM JAV DALAM JAV DALAM JAV DALAM PENGEMBALIAN DALAM JAV DALAM PEREFFACE 2, 2013-12-19 15:48:33 atau 2013-12-19 atau 2012/3/05* @param string teks yang akan diekstraksi* @Return Tanggal pengembalian* @Author: oschina* @createTime: 21 Januari, "TEKPRET;" = null; pola p_detail = pola.compile ("(20 // d {2} [-/] // d {1,2} [-/] // d {1,2} Pola.case_insensitive | pola.multiline); // Jika itu hanya tahun, bulan, hari, hari, jam, menit, kedua, kedua, kemudian ikuti pola p = pola.2pile (20 // bulan, 2, bulan, ", bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan), bulan, Pola.case_insensitive | pola.multiline); // pencocokan pencocokan = p.matcher (dateStr); Matcher matcher_detail = p_detail.matcher (dateStr); if (! (matcher_detail.find (0) && matcher_detail.groupCount ()> = 1)) {matcher_detail = p.matcher (dateStr); contilShms = true; } else matcher_detail = p_detail.matcher (dateStr); if (matcher_detail.find () && matcher_detail.groupCount ()> = 1) {matches = new arraylist (); untuk (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 (); // Keluarkan nilai pertama pubtime = pubtime.replace ("/", "-"). Ganti ("tahun", "-"). Ganti ("bulan", "-"). Ganti ("hari", "-"); 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; }}Di atas adalah semua tentang artikel ini, saya harap akan sangat membantu bagi semua orang untuk belajar pemrograman Java.