Este artículo comparte la clase de herramienta DateUtils para la conversión de tiempo para su referencia. El contenido específico es el siguiente
import java.sql.timestamp; import java.text.simpledateFormat; import java.util.calendar; import java.util.date; import java.util.hashmap; import java.util.map; import java.util.map; import net.sf.json.jsonObject;/*** TIEMPI WUL * 2015-12-31 */public class DateUtil {public static final String date_normal_format = "yyyy-mm-dd"; Cadena final estática pública datetime_normal_format = "yyyy-mm-dd hh: mm: ss"; public static final String date_compact_format = "yyyymmdd"; public static final String datetime_compact_format = "yyymmddhhmmss"; Public static final String ym_normal_format = "yyyy-mm"; cadena final estática pública ym_compact_format = "yyyymm"; / ** * Cadena a la marca de tiempo * @param datestr * @return * @author wul * 2016-1-11-17 */ public static timestamp stringToTimestamp (string datestr) {try {if (datestr.length () <= 10) {datestr += "00:00:00"; } return timestAMP.ValueOf (datEstr); } catch (Exception e) {E.PrintStackTrace (); regresar nulo; }} / ** * String hasta la fecha * @param datestr * @param format * @return * @author wul * 2016-1-117 * / public static date stringTodate (string datSSTER, format de cadena) {if (datestr == null || "" .equals (datestr)) {return null; } Fecha fecha = nulo; // Tenga en cuenta que el formato de formato debe coincidir con el formato de la cadena de fecha SimpledateFormat sdf = new SimpleDateFormat (formato); intente {date = sdf.parse (datestr); } catch (Exception e) {E.PrintStackTrace (); } fecha de retorno; } / ** * fecha para cadena * @param date * @param format * @return * @author wul * 2016-1-11-7 * / public static string dataToToString (fecha de fecha, formato de cadena) {simpledateFormat sdf = new SimpleDateFormat (format); String currentDate = sdf.format (fecha); devolver CurrentDate; } / ** * Fecha a la marca de tiempo * @param fecha * @return * @author wul * 2016-1-17 * / public static timestamp datetOtimestamp (fecha de fecha) {timestamp ts = new Timestamp (date.gettime ()); devolver ts; } / ** * TimeStamp to String * @param ts * @return * @author wul * 2016-1-17 * / public static string timestamptoString (timestamp ts) {string tsstr = null; SimpleDateFormat sdf = new SimpleDateFormat (DateTime_Normal_Format); intente {tsstr = sdf.format (ts); } catch (Exception e) {E.PrintStackTrace (); } return tsstr; } / ** * TimeStamp hasta la fecha * @param ts * @return * @author wul * 2016-1-17 * / public static date timeStamptodate (timestamp ts) {return ts; } / ** * Obtenga la hora actual y el formato: aaa yyyy-mm-dd hh: mm: ss * @return * / public static string getCurrentTimenormal () {SimpleDateFormat sdf = new SimpleDateFormat (datetime_normal_format); String currentDate = sdf.format (nueva fecha ()); devolver CurrentDate; } / ** * Obtenga la hora y el formato actuales: aaa yyymmddhhmmss * @return * / public static string getCurrentTimeCompact () {SimpleDateFormat sdf = new SimpleDateFormat (datTime_Compact_Format); String currentDate = sdf.format (nueva fecha ()); devolver CurrentDate; } / ** * Obtenga la hora y el formato actuales: aaa yyyy-mm-dd * @return * / public static string getCurrentDateNormal () {SimpleDateFormat sdf = new SimpledAformat (date_normal_format); String currentDate = sdf.format (nueva fecha ()); devolver CurrentDate; } / ** * Obtenga la hora y el formato actuales: aayyymmdd * @return * / public static string getCurrentDateCompact () {SimpleDateFormat sdf = new SimpleDateFormat (date_compact_format); String currentDate = sdf.format (nueva fecha ()); devolver CurrentDate; } / ** * Formato 20101202 Tiempo para 2010-12-02 * * @param Formato de tiempo de data: yyyymmdd * @return * / public static string getDateCompactTonormal (string dataString) {stringBuilder sb = new StringBuilder (); sb.append (dataTring.substring (0,4)). append ("-"). append (dataString.subSequence (4, 6)). append ("-"). append (datestring.substring (6, 8)); return sb.ToString (); } / ** * Formato 20101202101423 Tiempo para 2010-12-02 10:14:23 * * @param Formato de tiempo de data: yyyymmddhhmmss * @return * / public static string getDateTiMecompacttonormal (string dataString) {stringBuilder sb = new stringBuilder ();; SB.Append (DataString.Substring (0,4)). Append ("-"). Append (DataString.SubSequence (4, 6)). Append ("-"). Append (DataString.Substring (6, 8)) .Append ("") .Append (DataString.Substring (8,, 8, 10)). Append (":"). Append (DataString.Substring (10, 12)). Append (":"). Append (DataString.Substring (12)); return sb.ToString (); } / ** * Convierta la entrada de tiempo en la interfaz en una cadena de tiempo intermitente * 2010-12-02 10:14:23 Tiempo formateado como 20101202101423 * @param datenormaltr string * @return * / public static string getCompactString (string dataNormalStr) {stringBuffer ret = new StringBuffer (); intente {ret.append (datenormalstr.substring (0, 4)); ret.append (Datenormalstr.Substring (5, 7)); ret.append (Datenormalstr.Substring (8, 10)); ret.append (Datenormalstr.Substring (11, 13)); ret.append (Datenormalstr.Substring (14, 16)); ret.append (Datenormalstr.Substring (17, 19)); } catch (excepción ex) {// Si la cadena no es lo suficientemente larga, devuelva la parte anterior} return ret.toString (); } / ** * Obtenga el año en el formato de tiempo 20101202 (101423) * @param Formato de tiempo de datos: aaa yyymmdd (hhmmss) * @return * / public static string getyear (cadena dataTring) {return dataString.substring (0,4); } / ** * Obtenga el mes con el formato de tiempo 20101202 (101423) * @param Formato de tiempo de data: yyyymmdd (hhmmss) * @return * / public static string getMonth (cadena dataTring) {return dataString.substring (4,6); } / ** * Obtenga la fecha con el formato de tiempo 20101202 * @param Formato de tiempo de data: aaa yyymmdd * @return * / public static string getdaynotime (string dataString) {return dataString.substring (6); } / ** * Obtenga la fecha antes de la fecha actual y avance por el número de días * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getbeforedatePlusday (int numval, cadena dateFormat) {calendario calendario = calendario.getinstance (); Long CurrentTimemillis = calendar.gettimeinmillis (); Hora LonginMillis = 60 * 60 * 1000; dval largo = numval * 24 * HourInmillis; SimpleDateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currentTimemillis - dval); devolver CurrentDate; } / ** * Obtenga la fecha antes de la fecha actual y avance por el número de días * @param numval * @param dateFormat * @return * @author wul * 2016-1-11-11 / public static string getafterdatePlusday (int numval, cadena dateFormat) {calendario calendario = calendario.getInstance (); Long CurrentTimemillis = calendar.gettimeinmillis (); Hora LonginMillis = 60 * 60 * 1000; dval largo = numval * 24 * HourInmillis; SimpleDateFormat sdf = new SimpleDateFormat (dateFormat); String CurrentDate = sdf.format (currentTimemillis + dVal); devolver CurrentDate; } / ** * Obtenga la fecha antes de la fecha actual y avance por hora * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getBeForedatePlusHour (int numval, cadena dateFormat) {calendar calendar = calendAR.getInstance (); Long CurrentTimemillis = calendar.gettimeinmillis (); Hora LonginMillis = 60 * 60 * 1000; dval largo = numval * HourInmillis; SimpleDateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currentTimemillis - dval); devolver CurrentDate; } / ** * Obtenga la fecha antes de la fecha actual y avance por hora * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getAfterdatePlushour (int numval, cadena dateFormat) {calendar calendar = calendAR.getInstance (); Long CurrentTimemillis = calendar.gettimeinmillis (); Hora LonginMillis = 60 * 60 * 1000; dval largo = numval * HourInmillis; SimpleDateFormat sdf = new SimpleDateFormat (dateFormat); String CurrentDate = sdf.format (currentTimemillis + dVal); devolver CurrentDate; } / ** * Número de días diferentes entre dos fechas * @param begindate * @param enddate * @return * @author wul * 2016-1-18 * / public static int daysbetween (date begindate, date enddate) {calendar cal = calendar.getInstance (); Cal. mucho tiempo1 = cal.gettimeinmillis (); Cal.SetTime (End Data); mucho tiempo2 = cal.gettimeinmillis (); largo entre_days = (Time2 - Time1) / (1000 * 3600 * 24); return integer.parseInt (string.ValueOf (entre_days)); } / ** * Obtenga el número de días en un cierto mes * @param año * @param mes * @return * @author wul * 2016-1-18 * / public static int Cal.set (calendario.YAR, año); cal.set (calendario. Month, mes - 1); return cal.getActualMaximum (calendar.date); } / ** * Agregar o restar el año a la hora * @param fecha * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática pública getDatePlusyear (fecha de fecha, int PLUSTIME) {calendario cal = calendar.getInstance (); Cal.settime (fecha); cal.add (calendario. años, plustimia); Fecha d = cal.gettime (); regresar D; } / ** * Agregue o reste el mes de tiempo * @param fecha * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática pública getDatePlusmonth (fecha de fecha, int PLUSTIME) {calendario cal = calendar.getInstance (); Cal.settime (fecha); Cal.add (calendario. Month, Plususe); Fecha d = cal.gettime (); regresar D; } / ** * Agregue y reste el número de días a la hora * @param fecha * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática pública getDatePlusday (fecha de fecha, int PLUSTIME) {calendario cal = calendar.getInStance (); Cal.settime (fecha); cal.add (calendario.date, plustime); Fecha d = cal.gettime (); regresar D; } / ** * Agregar o restar la hora * @param fecha * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática public static getDatePlushour (fecha de fecha, int PLUSTIME) {calendar cal = calendar.getInstance (); Cal.settime (fecha); cal.add (calendario. Hur, plustimia); Fecha d = cal.gettime (); regresar D; } / ** * Agregar o restar la hora * @param date * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática pública getDatePlusMinute (fecha de fecha, int PLUSTIME) {calendar cal = calendar.getInstance (); Cal.settime (fecha); Cal.add (calendario. Minute, Plusus); Fecha d = cal.gettime (); regresar D; } / ** * Agregue y reste segundos a la hora * @param fecha * @param plusustime * @return * @author wul * 2016-1-18 * / fecha estática pública getDatePlussecond (fecha de fecha, int PLUSTIME) {calendario cal = calendar.getInstance (); Cal.settime (fecha); Cal.add (calendario. Segundo, Plusus); Fecha d = cal.gettime (); regresar D; } / ** * Devuelve el año actual * @return * @author wul * 2016-1-18 * / public static int getCurryear () {calendar calendar = calendar.getInstance (); return calendar.get (1); } / ** * Devuelve el mes actual * @return * @author wul * 2016-1-18 * / public static int getCurrentMonth () {calendar calendar = calendar.getInstance (); return calendar.get (2) + 1; } / ** * Devuelve el día actual * @return * @author wul * 2016-1-18 * / public static int getCurrentday () {calendar calendar = calendar.getInstance (); return calendar.get (5); } / ** * Devuelve la hora actual * @return * @author wul * 2016-1-18 * / public static int getCurrentinTour () {calendar calendar = calendar.getInstance (); return calendar.get (11); } / ** * Devuelve el minuto actual * @return * @author wul * 2016-1-18 * / public static int getCurrentMinute () {calendar calendar = calendar.getInstance (); return calendar.get (12); } / ** * Devuelve el segundo actual * @return * @author wul * 2016-1-18 * / public static int getCurrentSecond () {calendar calendar = calendar.getInstance (); return calendar.get (13); } / ** * Devuelve el año actual * @return * @author wul * 2016-1-18 * / public static int getyear (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (1); } / ** * Devuelve el mes actual * @return * @author wul * 2016-1-18 * / public static int getMonth (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (2) + 1; } / ** * Regrese el día actual * @return * @author wul * 2016-1-18 * / public static int getday (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (5); } / ** * Devuelve la hora actual * @return * @author wul * 2016-1-18 * / public static int gethour (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (11); } / ** * Devuelve el minuto actual * @return * @author wul * 2016-1-18 * / public static int getminute (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (12); } / ** * Devuelve el segundo actual * @return * @author wul * 2016-1-18 * / public static int getSecond (fecha de fecha) {calendar calendar = calendar.getInstance (); calendario. Settime (fecha); return calendar.get (13); } public static void main (string [] args) {System.out.println (dateUtil.datetoString (new java.sql.date (system.currentTimemillis ()), dateUtil.date_normal_format)); Map <string, object> map = new HashMap <String, Object> (); map.put ("fecha", nueva fecha ()); Cadena json = jsonObject.FromObject (MAP) .ToString (); System.out.println (JSON); }}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.