Este artículo comparte el código específico de la clase de herramienta Java DateUtils para su referencia. El contenido específico es el siguiente
import java.text.parseException; import java.text.simpledateFormat; import java.util.calendar; import java.util.date;/*** Descripción: clase de herramienta de fecha pública*/clase pública dateutils {public static string date_format = "yyyy-mm-dd"; public static string date_time_format = "yyyy-mm-dd hh: mm: ss"; cadena estática pública date_format_chinese = "yyyyy año m mes d día"; / ** * Obtenga la fecha actual * * * @return * */ public static string getCurrentDate () {String datStr = null; SimpleDateFormat df = new SimpleDateFormat (dateUtils.date_format); datestr = df.format (new Date ()); devolver datestr; } / ** * Obtenga la fecha y hora actuales * * * @return * * / public static string getCurrentDateTime () {String datStr = null; SimpleDateFormat df = new SimpleDateFormat (dateUtils.date_time_format); datestr = df.format (new Date ()); devolver datestr; } / ** * Obtenga la fecha y hora actuales * * * @return * * / public static string getCurrentDateTime (String dateFormat) {String datErSt = null; SimpleDateFormat df = new SimpleDateFormat (dateFormat); datestr = df.format (new Date ()); devolver datestr; } public static string dateToDateTime (fecha de fecha) {string datStr = null; SimpleDateFormat df = new SimpleDateFormat (dateUtils.date_time_format); datstr = df.format (fecha); devolver datestr; } / ** * Convertir el formato de la fecha de la fecha * * * @param datestr * @return * * / public static date stringTodate (String DatStros } Fecha fecha = nueva fecha (); SimpleDateFormat df = new SimpleDateFormat (dateUtils.date_format); intente {date = df.parse (datestr); } catch (parseException e) {date = dateUtils.stringtodate (datestr, "yyyymmdd"); } fecha de retorno; } / ** * Convertir el formato de fecha de la fecha * Formato personalizado * * @param datestr * @return * * / public static date stringTodate (String datestr, string dateFormat) {date date = new date (); SimpleDateFormat df = new SimpleDateFormat (dateFormat); intente {date = df.parse (datestr); } catch (ParseException e) {E.PrintStackTrace (); } fecha de retorno; } / ** * Convertir Fecha de formato Fecha en Formato de cadena * * * @param fecha * @return * * / public static string dataToToString (fecha de fecha) {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (dateUtils.date_format); datstr = df.format (fecha); devolver datestr; } / ** * Convertir Fecha de formato Fecha en Formato de cadena Formato personalizado * * @param fecha * @param dateFormat * @return * / public static string dataToToString (fecha fecha, cadena dateFormat) {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (dateFormat); datstr = df.format (fecha); devolver datestr; } / ** * Obtenga el valor del día de la fecha * * * * @param fecha * Ingrese la fecha * @return * * / public static int getdayofdate (fecha de fecha) {int d = 0; Calendario CD = calendar.getInstance (); CD.SetTime (fecha); d = cd.get (calendar.day_of_month); regresar D; } / ** * Obtenga el valor mes de la fecha * * * * @param fecha * Ingrese la fecha * @return * * / public static int getMonthofdate (fecha de fecha) {int m = 0; Calendario CD = calendar.getInstance (); CD.SetTime (fecha); m = CD.get (calendario.month) + 1; regresar m; } / ** * Obtenga el valor de año de la fecha * * * * @param fecha * Ingrese la fecha * @return * * / public static int getyearofdate (fecha de fecha) {int y = 0; Calendario CD = calendar.getInstance (); CD.SetTime (fecha); y = cd.get (calendar.year); regresar y; } / ** * Obtenga el día de la semana * * * * @param fecha * Ingrese la fecha * @return * * / public static int getWeekOfDate (fecha de fecha) {int wd = 0; Calendario CD = calendar.getInstance (); CD.SetTime (fecha); wd = cd.get (calendar.day_of_week) - 1; regresar WD; } / ** * Obtenga el primer día del mes de la fecha de entrada * * * * @param fecha * fecha de entrada * @return * * / public static fecha getFirstdayOfMonth (fecha de fecha) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); cd.set (calendar.day_of_month, 1); return cd.gettime (); } / ** * Obtenga el último día del mes de la fecha de entrada * * @param fecha * / fecha estática pública getlastdayofmonth (fecha fecha) {return dateUtilss.addday (dateUtils.getFirstdayOfMonth (dateUtils.addmonth (fecha, 1)), -1); } / ** * Determine si es un año de salto * * * * @param fecha * fecha de entrada * @return y true no false * * / public static boolean isleapyear (fecha de fecha) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); int año = cd.get (calendar.year); if (año % 4 == 0 && año % 100! = 0 | año % 400 == 0) {return true; } else {return false; }} / ** * Generar formato de tipo de fecha basado en el año, mes y día representado por el número entero * * @param año * año * @param mes * mes * @param día * día * @return * * / pública fecha estática getDateByymd (intane, int mes, int day) {calendario CD = calendar.getInstance (); CD.Set (año, mes-1, día); return cd.gettime (); } / ** * Obtenga el ciclo de fecha correspondiente del año * * @param fecha * Ingrese fecha * @param iyear * El número de años representa el anterior * @return * * / public static date getyearcleofdate (fecha fecha, int iyear) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); CD.Add (Calendar.Year, Iyear); return cd.gettime (); } / ** * Obtenga la fecha correspondiente del ciclo del mes * * @param fecha * Ingrese fecha * @param i * @return * * / public static fecha getMonthcycleOfDate (fecha de fecha, int i) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); CD.Add (calendario. Month, i); return cd.gettime (); } / ** * Calcule cuántos años es la diferencia entre DetDate y Todate * * @Param fromdate * @param Todate * @return años * * / public static int df.setTime (DTARDATE); Calendario dt = calendar.getInstance (); dt.setTime (Todate); return dt.get (calendar.year) -df.get (calendar.year); } / ** * Calcule cuántos meses la diferencia entre DetDate y Todate * * @Param fromdate * @param Todate * @return meses * * / public static int df.setTime (DTARDATE); Calendario dt = calendar.getInstance (); dt.setTime (Todate); return dt.get (calendar.year)*12+dt.get (calendario.month)- (df.get (calendar.year)*12+df.get (calendar.month)); } / ** * Calcule cuántos días la diferencia entre DetDate y Todate * * @param fromdate * @param toldate * @return days * * / public static long getdaybyminusDate (object frondate, object toDate) {date f = dateUtils.chgObject (dhallate); Fecha t = dateUtils.chgobject (Todate); largo fd = f.gettime (); largo TD = T.GetTime (); return (td-fd)/(24l * 60l * 60l * 1000l); } / ** * Calcule la edad * * @param Cumpleaños * Fecha de fecha * @param calcDate * punto de fecha para calcularse * @return * * / public static int calcage (fecha de cumpleaños, fecha calcDate) {int cyear = dateUtils.getyearofdate (calcDate); int cmonth = dateUtils.getMonthofdate (calcDate); int cday = dateUtils.getdayofdate (calcDate); int byear = dateUtils.getYiAfDate (cumpleaños); int bmonth = dateUtils.getMonthofdate (cumpleaños); int bday = dateUtils.getdayofdate (cumpleaños); if (cmonth> bmonth || (cmonth == bmonth && cday> bday)) {return cyear-byear; } else {return cyear-1-auarra; }} / ** * Obtenga la fecha de nacimiento de la tarjeta de identificación * * @param idno * número de tarjeta de identificación * @return * * / public static string getBirthdayFromIDCard (String Idno) {calendar cd = calendar.getInstance (); if (idno.length () == 15) {cd.set (calendar.year, integer.valueOf ("19" + idno.substring (6, 8)) .intvalue ()); CD.Set (calendario.month, integer.valueOf (idno.substring (8, 10)) .intvalue () - 1); cd.set (calendar.day_of_month, integer.valueof (idno.substring (10, 12)). intValue ()); } else if (idno.length () == 18) {cd.set (calendar.year, integer.valueOf (idno.substring (6, 10)) .intvalue ()); CD.Set (calendario.month, integer.valueOf (idno.substring (10, 12)) .intvalue () - 1); cd.set (calendar.day_of_month, integer.valueof (idno.substring (12, 14)). intValue ()); } return dateUtils.datetoString (cd.gettime ()); } / *** aumentar (+) o restar (-) días en la fecha de entrada** @param fecha* fecha de entrada* @param emblema* Número de días para aumentar o disminuir* / fecha pública de fecha adicional Añadir (fecha de fecha, int iday) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); cd.add (calendar.day_of_month, iday); return cd.gettime (); } / *** Agregar (+) o restar (-) meses en la fecha de entrada** @Param Fecha* Ingrese Fecha* @Param Puntuación de la imagen* Mes para aumentar o disminuir* / Fecha estática pública ADDMONTH (Fecha de fecha, INT Image) {Calendar CD = calendar.getInstance (); CD.SetTime (fecha); CD.Add (calendario. Month, imagen); return cd.gettime (); } / ** * agregar (+) o restar (-) años en la fecha de entrada * * @param fecha * Ingrese fecha * @param emblema * Número de años para aumentar o disminuir * / fecha pública de fecha estática addyear (fecha de fecha, int iyear) {calendar cd = calendar.getInstance (); CD.SetTime (fecha); CD.Add (Calendar.Year, Iyear); return cd.gettime (); } / ** * Fecha de tipo de objeto * @param fecha * @return * / public static date chgobject (fecha de objeto) {if (date! = Null && date instanceOf date) {return (date) fecha; } if (date! = null && date instanceOf string) {return dateUtilss.stringtodate ((string) date); } return null; } public static long getAgebybirthday (fecha de cadena) {date birthday = stringTodate (fecha, "aaa yyyy-mm-dd"); larga sec = nueva fecha (). getTime () - cumpleaños.gettime (); larga edad = sec/(1000*60*60*24)/365; edad de retorno; }/** * @param args */public static void main (string [] args) {// string temp = dateUtil.datetoString (getLastdayOfMonth (new date ()), // dateUtil.date_format_chinese); // cadena s = dateUtil.datetostring (dateUtil.addday (dateUtil.addyear (nueva fecha (), 1),-1)); largo s = dateUtils.getdaybyminusdate ("2012-01-01", "2012-12-31"); System.err.println (s); }}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.