Se extrae el tiempo de lanzamiento de varios formatos en la página web, y el tiempo de lanzamiento se expresa en el formato regular "Yyyy-MM-DD HH: MM: SS". Solo puede intentar seguir la precisión, pero debido a que el formato del tiempo de liberación de la red es muy flexible, no puede ser 100% correcto.
paquete 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; importar whu.utils.timilutil; /*** creado el 13 de marzo de 2014 a las 2:49:05 pm* @Description Obtenga la hora de publicación de la página web*/public class fetchPubtime {/** representa 8 fechas consecutivas en la url, por ejemplo, http://www.baidu.com/20140311/2356.html*/privado static string string striz_rege = "(([-|/| _] {1} 20 // d {6})"; /** Representa una fecha separada por-o/con un año, mes y fecha, por ejemplo http://www.baidu.com/2014-3-11/2356.html*/String estático privado url_reg_sep_ymd = "(([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; /** Indica que las fechas separadas por - o/, solo año y mes, por ejemplo, http://www.baidu.com/2014-3/2356.html*/string static string url_reg_sep_ym = "(([-|/| _] {1} 20 // d {2} [-|/| _] {1} // d {1,2})"; calendario estático privado corriente = calendario.getInstance (); / ** Tiempo formateado correcto Expresión regular*/ String static privado Righttimeg = "^((// 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? [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); // no en el enlace, coincidir if (pubTime == null) {if (urlContent! = Null &&! UrlContent.trim (). Equals ("")) return ExtractPagedate (urlContent); } return PubTime; } / ** Extraiga el tiempo de publicación de la URL y devuelva a Yyyyy-Mm-Dd HH: 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 ()); // Cada paso no puede exceder la hora actual 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 = nulo; Patrón p_sep = patrón.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}"); Calendario thetime = calendar.getInstance (); thetime.set (calendar.year, integer.parseint (seg [0])); thetime.set (calendario.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 = nulo; Patrón p_sep_ym = patrón.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 ()); Calendario thetime = calendar.getInstance (); String [] seg = time.split ("[-|/| _] {1}"); thetime.set (calendar.year, integer.parseint (seg [0])); thetime.set (calendario.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;} /** Extraiga la hora de publicación del código fuente de la web* Expresión regular Extrae la fecha de implementación de la fecha* Diciembre 19 de diciembre, 2013 15:58:5 82: 2013-12-19 15:48:33 o 2013-12-19 o 2012/3/05* @param Cadena de texto para extraer* @return return fecha* @author: Oschina* @CreatEtime: 21 de enero, 2013*/public string string extray = nulo; Pattern.case_insensitive | Pattern.multiline); 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); contenerSHMS = verdadero; } else Matcher_detail = P_Detail.Matcher (Datestr); if (matcher_detail.find () && matcher_detail.groupCount ()> = 1) {coincidentes = 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 (coincidente.size ()> 0) {for (int i = 0; i <matches.size (); i ++) {string pubtime = coincidente.get (i) .ToString (). Trim (); // Saque el primer valor PUBTime = PUBTime.replace ("/", "-"). Reemplazar ("año", "-"). Reemplazar ("mes", "-"). Reemplazar ("Día", "-"); if (current.compareto (timeUtil.strtocalendar (PubTime, "aaa yyy-mm-dd"))> = 0) {if (contenedShms) PubTime+= ""+"00:00:00"; if (PUBTime.matches (RightTimerEg)) {return PubTime; }}}} else {return null; }} capt (excepción e) {return null; } return null; }}Lo anterior se trata de este artículo, espero que sea útil para todos aprender la programación de Java.