เวลาวางจำหน่ายของรูปแบบต่าง ๆ ในหน้าเว็บจะถูกแยกออกมาและเวลาที่วางจำหน่ายจะแสดงในรูปแบบ "YYYY-MM-DD HH: MM: SS" ปกติ คุณสามารถลองใช้ความแม่นยำได้ แต่เนื่องจากรูปแบบของเวลาการเปิดตัวเครือข่ายมีความยืดหยุ่นมากจึงไม่สามารถถูกต้องได้ 100%
แพ็คเกจ whu.extract.pubtime.core; นำเข้า java.util.arraylist; นำเข้า java.util.calendar; นำเข้า java.util.collections; นำเข้า java.util.list; นำเข้า java.util.regex.matcher; นำเข้า java.util.regex.pattern; นำเข้า whu.utils.timeutil; /*** สร้างขึ้นเมื่อวันที่ 13 มีนาคม 2014 เวลา 2:49:05 น.* @description รับเวลาเผยแพร่ของหน้าเว็บ*/ชั้นเรียนสาธารณะ fetchpubtime {/** หมายถึง 8 วันที่ต่อเนื่องใน URL ตัวอย่างเช่น http://www.baidu.com/20140311/2356.html "([-|/| _] {1} 20 // d {6})"; /** หมายถึงวันที่คั่นด้วย-หรือ/กับปีเดือนและวันที่เช่น http://www.baidu.com/2014-3-11/2356.html*/สตริงคงที่ส่วนตัว url_reg_sep_ymd = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; /** ระบุว่าวันที่คั่นด้วย - หรือ//, เฉพาะปีและเดือนตัวอย่างเช่น http://www.baidu.com/2014-3/2356.html*/สตริงคงที่ส่วนตัว url_reg_sep_ym = "([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; ปฏิทินคงที่ส่วนตัวปัจจุบัน = ปฏิทิน GetInstance (); / ** การจัดรูปแบบเวลาที่ถูกต้องนิพจน์ปกติ*/ สตริงคงแบบคงที่ส่วนตัว 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 */ สตริงคงที่สาธารณะ getPubTimeVarious (url สตริง, urlcontent สตริง) {string pubtime = getPubtimeFromurl (url); // ไม่ได้อยู่ในลิงค์การจับคู่ถ้า (pubtime == null) {ถ้า (urlcontent! = null &&! urlcontent.trim (). เท่ากับ ("")) return extractPagedate (urlcontent); } return pubtime; } / ** แยกเวลาการเผยแพร่ออกจาก URL และส่งคืน yyyy-mm-dd hh: mm: สตริง SS-formatted * @param url * @return * / สตริงคงที่สาธารณะ getPubtimeFromurl matcher m_whole = p_whole.matcher (url); if (m_whole.find (0) && m_whole.groupcount ()> 0) {เวลาสตริง = m_whole.group (0); time = time.substring (1, time.length ()); // แต่ละขั้นตอนไม่เกินเวลาปัจจุบันถ้า (current.Compareto (timeutil.strtocalendar (เวลา, "yyyymmdd"))> = 0) {ส่งคืนเวลา. }} 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) {เวลาสตริง = 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) {เวลาสตริง = 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); ถ้า (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* @param สตริงข้อความที่จะสกัด* @return วันที่คืน* @author: oschina* @createtime: 21 มกราคม 2013* ลอง {รายการจับคู่ = NULL; P_Detail = pattern.compile ("(20 // d {2} [-/] // d {1,2} [-/] // d {1,2} // d {1,2}: // d {1,2}: // เดือน // d {1,2} วัน) ", pattern.case_insensitive | pattern.multiline); // ถ้าเป็นเพียงปีเดือน, วัน, วัน, ชั่วโมง, นาที, วินาที, วินาที, จากนั้นทำตามรูปแบบ p = ต่อไปนี้ pattern.compile ("(20 // d {2} [-/] // d {1,2} [-/] // d {1,2}) | (20 // d {2} ปีเดือนเดือนเดือนเดือนเดือนที่สอง) // 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); CONTERHSHMS = TRUE; } else matcher_detail = p_detail.matcher (datestr); if (matcher_detail.find () && matcher_detail.groupcount ()> = 1) {matches = new ArrayList (); สำหรับ (int i = 1; i <= matcher_detail.groupcount (); i ++) {สตริง temp = matcher_detail.group (i); Matches.add (Temp); }} else {matches = collections.empty_list; } if (matches.size ()> 0) {สำหรับ (int i = 0; i <matches.size (); i ++) {string pubtime = matches.get (i) .toString (). trim (); // นำค่าแรกออก pubtime = pubtime.replace ("/", "-") แทนที่ ("ปี", "-") แทนที่ ("เดือน", "-") แทนที่ ("วัน", "-"); if (current.compareto (timeutil.strtocalendar (pubtime, "yyyy-mm-dd"))> = 0) {ถ้า (contenthms) pubtime+= ""+"00:00:00"; if (pubtime.matches (righttimereg)) {return pubtime; }}}} else {return null; }} catch (exception e) {return null; } return null; -ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์สำหรับทุกคนในการเรียนรู้การเขียนโปรแกรม Java