Este artículo comparte una colección completa de la fecha de Java y la conversión de marca de tiempo para su referencia. El contenido específico es el siguiente
paquete com.crm.util; import java.math.bigdecimal; import java.text.DecimalFormat; import java.text.parseException; import java.text.simpledateFormat; import java.util.calendar; import java.util.date; / ** * @author dingjiacheng * */ public class dateFormatUtil {/ ** * TimeStamp hasta la fecha * @param ms * @return */ public static date transfordate (integer ms) {if (ms == null) {ms = 0; } long msl = (largo) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss"); Fecha temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (ParseException e) {E.PrintStackTrace (); }} return temp; } / ** * Obtenga la marca de tiempo a las 9:30 pm * * @return * / public static int cal.add (calendario.date, día); cal.set (calendario.hour_of_day, hora); cal.set (calendario. Segundo, 0); cal.set (calendario. Minute, minuto); cal.set (calendario.milliseCond, 0); return (int) (cal.gettimeinmillis () / 1000); } / ** * Obtenga el tiempo completo en la hora actual * * @return * / public static int getintegaltime () {calendar cal = calendar.getInstance (); cal.add (calendario.hour_of_day, 1); cal.set (calendario. Segundo, 0); cal.set (calendario. Minute, 0); cal.set (calendario.milliseCond, 0); return (int) (cal.gettimeinmillis () / 1000); } public static int getintegaltimeend () {calendar cal = calendar.getInstance (); cal.set (calendario.hour_of_day, 24); cal.set (calendario. Segundo, 0); cal.set (calendario. Minute, 0); cal.set (calendario.milliseCond, 0); return (int) (cal.gettimeinmillis () / 1000); } / ** * TimeStamp hasta la fecha * @param ms * @return * / public static date transfordate3 (integer ms) {if (ms == null) {ms = 0; } long msl = (largo) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm"); Fecha temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (ParseException e) {E.PrintStackTrace (); }} return temp; } / ** * TimeStamp hasta la fecha * @param ms * @return * / public static date transfordate (long ms) {if (ms == null) {ms = (long) 0; } long msl = (largo) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss"); Fecha temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (ParseException e) {E.PrintStackTrace (); }} return temp; } public static string transfordate1 (entero ms) {string str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }}} return str; } public static string transfordate2 (entero ms) {string str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }}} return str; } public static string transfordate4 (entero ms) {string str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy.mm.dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }} return str; } public static string transfordate5 (entero ms) {string str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/mm/dd hh: mm: ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }} return str; } public static String transfordateInchinese (Integer MS) {String str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat ("yyyyy año mm mes dd fecha hh: mm: ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }} return str; } / ** * fecha a la marca de tiempo * @param fecha * @return * / public static entero transformilliseCond (fecha de fecha) {if (date == null) return null; return (int) (date.gettime ()/1000); }/*** Obtenga la marca de tiempo actual* @return*/public static integer currentTimestamp () {return (int) (System.CurrentTimemillis ()/1000); } / ** * Cadena de fecha en TimEmestamp * @param datestr * @return * / public static entero transformilliseCond (string datSSTER) {date date = dateFormatUtil.FormatDate (datestr); Fecha de retorno == nulo? nulo: dateFormatUtil.TransformilliseCond (fecha); } / ** * Fecha String a Timestamp * @param datestr * @return * / public static integer transformilliseCond (string datstr, string format) {date date = dateFormatUtil.FormatDate (datestr, format); Fecha de retorno == nulo? nulo: dateFormatUtil.TransformilliseCond (fecha); } / ** * Fecha Cadena a la marca de tiempo * @param datestr * @param format como "yyyyy-mm-dd" * @return * / public static integer transformilliseCondbytim (string datestr, string tim) {simpledateFormat sdf = new SimpleDateFormat (Tim); Fecha fecha = nulo; intente {date = sdf.parse (datestr); } catch (ParseException e) {E.PrintStackTrace (); } fecha de retorno == nulo? nulo: dateFormatUtil.TransformilliseCond (fecha); } / ** * cadena hasta la fecha, el formato es: "aaa yyy-mm-dd hh: mm: ss" * @param datestr * @return * / public static date formatDate (String datestr) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyyy-mm-dd hh: mm: ss");; Resultado de fecha = nulo; intente {result = sdf.parse (datestr); } catch (ParseException e) {E.PrintStackTrace (); } resultado de retorno; } / ** * cadena hasta la fecha, formato: "aaa yyyy-mm-dd hh: mm: ss" * @param datstr * @return * / public static date formatDate (string datestr, string format) {SimpleDateFormat sdf = new SimpleDateFormat (format); Resultado de fecha = nulo; intente {result = sdf.parse (datestr); } catch (ParseException e) {E.PrintStackTrace (); } resultado de retorno; } / ** * fecha a cadena * @param fecha * @return * / public static string formatDate (fecha fecha) {simpledateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss"); Resultado de cadena = nulo; resultado = sdf.format (fecha); resultado de retorno; } / ** * fecha a la cadena * @param fecha * @return * / public static string formatDate (fecha de fecha, formato de cadena) {SimpleDateFormat sdf = new SimpleDateFormat (format); Resultado de cadena = nulo; resultado = sdf.format (fecha); resultado de retorno; } / ** * salida de formato de marca de tiempo (para plantilla httpl) * * @param ms timestamp * @param formato formato formato * @return * / public static string transfordate (Integer MS, string format) {string str = "" "; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat sdf = new SimpleDateFormat (formato); if (! ms.equals (0)) {try {str = sdf.format (msl); } catch (Exception e) {E.PrintStackTrace (); }}} return str; } / ** * Tome la parte entera o decimal del número de tipo BigDecimal (para la plantilla HTTPL) * * @param B value * @param Modo Modo 0 Rondas 1 Parte decimal * @return * / public static String SplitBigDecimal (BigDecimal B, int Mode) {decimalformat df = new decimalformat ("0.00"); Cadena s = df.format (b); if (mode == 0) {return s.split ("//.") [0]; } else {return "."+S.Split ("//.") [1]; }} / ** * Calcule el número de días que la diferencia entre dos fechas (para plantillas http) * * @param ts1 timestamp 1 * @param ts2 timestamp 2 * @return * / public static int caculate2days (Integer TS1, Integer TS2) {Date FirstDate = DateFormal.transfordate (TS1); Date SECTDATE = dateFormatUtil.Transfordate (TS2); Calendario calendario = calendario.getInstance (); calendario. Settime (First Date); int daynum1 = calendar.get (calendar.day_of_year); calendario. Settime (Seconddate); int daynum2 = calendar.get (calendar.day_of_year); return math.abs (DayNum1 - DayNum2); }/** * Agregue un asterisco a los cuatro dígitos intermedios del teléfono móvil * * @param mobile * @return */public string mobileserect (String mobile) {if (! mobile = mobile.substring (0, entre 2)+"****"+mobile.substring (entre+2, mobile.length ()); } return mobile; } / ** * Agregue un asterisco a la dirección de correo electrónico * * @param Correo electrónico * @return * / public String Correo electrónicoSerect (Correo electrónico de cadena) {if (! Correo electrónico = correo electrónico. } devolver correo electrónico; } / ** * Agregue datos del tipo BigDecimal * * @param bigDecimal Source * @param bigDecimal Target * @return * / public bigDecimal sumbigdicimal (bigDecimal Source, bigDecimal Target) {Source = Source.Add (Target); fuente de retorno; } / ** * Adición de datos de tipo BigDecimal * * @param bigDecimal Source * @param bigDecimal Target * @return * / public bigDecimal sumbigdicimalandDouble (fuente bigDecimal, doble objetivo) {bigDecimal new_Target = new BigDecimal (Target); fuente = fuente.Add (new_target); fuente de retorno; } / ** * resta de datos de tipo bigDecimal * * @param bigDecimal Source * @param bigDecimal objetivo * @return * / public bigDecimal subbigdicimal (fuente bigDecimal, bigDecimal target) {fuente = fuente.subtract (target); fuente de retorno; } / *** Obtenga la diferencia de tiempo entre el tiempo entrante y la hora actual* @return* / public static long gettimediff (int timestamp) {date d1 = dateFormatUtil.transfordate (timestamp); Fecha hoy = nueva fecha (); if (d1.gettime () <Today.gettime ()) {return null; } return (d1.gettime ()-Today.gettime ())/1000; } / *** Obtenga la fecha del primer día de una determinada semana* @param Semana 0 esta semana 1 semana anterior 1 la próxima semana* @return* / public static string weekfirstday (int week) {calendar c1 = calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(semana-1) -5); Cadena d1 = new SimpledateFormat ("aaa yyy-mm-dd"). Format (c1.gettime ()); regresar D1+"00:00:00"; } / *** Tiempo actual más un año* / public static String addYear (int starttime) {date firstDate = dateFormatUtil.Transfordate (starttime); Calendario calendario = calendario.getInstance (); calendario. Settime (First Date); calendario.add (calendario. años, 1); String d1 = new SimpleDateFormat ("yyyyy-mm-dd hh: mm: ss"). Format (calendar.gettime ()); regresar D1; } / ** * Obtenga la fecha del último día de una determinada semana * @param Week * @return * / public static string weeklastday (int week) {calendar c1 = calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(semana-1) +1); Cadena d1 = new SimpledateFormat ("aaa yyy-mm-dd"). Format (c1.gettime ()); regresar D1+"23:59:59"; } / *** Comparación con la hora actual* @return* / public static boolean greatthannow (int timestamp) {date d1 = dateFormatUtil.Transfordate (timestamp); Fecha hoy = nueva fecha (); if (d1.gettime ()> = thay.gettime ()) {return true; } return false; } / ** * HH: mm: el tiempo de formato SS se convierte en una marca de tiempo de 1970-01-01 días (es decir, si solo hay tiempo y no hay fecha, se requiere usar una marca de tiempo para representar la hora) * @author dingjiAng * * / public static intsfromFrom (tiempo de cadena) {return TransformillIsEd ("1970-01-01" "Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy-mmmm (yyyyyyyyymm HH: MM: SS "); } / ** * Tiempo de tiempo convertido a HH: MM: SS Format Time (extracción de fecha) * @author dingjiacheng * * / public static string transtotime (int tiempo) {cadena s = nueva cadena (transfordate1 (tiempo)); Cadena ss [] = s.split (""); return ss [1]; } public static int Transtochuo (String DataString) {SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("yyyy-mm-dd"); int res = 0; intente {date date = SimpleDateFormat .Parse (DataString); res = (int) date.gettime (); } catch (ParseException e) {E.PrintStackTrace (); } return Res; } public static void main (string [] args) {//system.out.println (getIngalTimeend ()); System.out.println (Transfordate2 (TransformilliseCond ("2015-02-25 00:00:00")))); //System.out.println(transformilliseCond("2016-01-25","yyyyy-mm-dd ")); //System.out.println(transfordate1(TransformilliseCond("1970-01-01 00:00:00 "," aaa yyy-mm-dd hh: mm: ss "))); //System.out.println (currentTimestamp ()); //System.out.println (transfordate (currentTimestamp ())); //System.out.println(new date ()); //System.out.println (dateUtils.getDate ()); System.out.println (transfromtime ("00:00:01")); System.out.println (Transtotime (transfromtime ("15:01:13"))); }} Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.