Este artigo compartilha a classe DateUTils Tool para conversão de tempo para sua referência. O conteúdo específico é o seguinte
importar java.sql.timestamp; importar java.text.simpledateFormat; importar java.util.calendar; importar java.util.date; importar java.util.hashmap; import java.util.map; import java.util.mapsp****** ^ ** ** ** ** *** ^*** ^ ** ** ** *** ^*** ^ ** ** ** ***** ^ ** *** ^*** ^ ** ** *** ^*** ^ ** ** ** ****** ^ ** ** *** ^*** ^* ^* ^ **), import.json.json.json. wul * 2015-12-31 */public public static final string 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"; public static final string ym_compact_format = "yyyyymm"; / ** * String para timestamp * @param datestr * @return * @author wul * 2016-1-17 */ public static timestamp stringtotimestamp (string dateST) {try {if (datest.length () <= 10) {dateST += "00:00:00"; } retornar timestamp.valueof (DATEST); } catch (Exceção e) {e.printStackTrace (); retornar nulo; }} / ** * String até a data * @param DATEST * @param format * @return * @author wul * 2016-1-17 * / public static date stringtodate (string datestr, string format) {if (datestr == null || ". } Data date = null; // Observe que o formato do formato deve corresponder ao formato da string de data simpledateFormat sdf = new SimpleDateFormat (formato); tente {date = sdf.parse (datestr); } catch (Exceção e) {e.printStackTrace (); } data de retorno; } / ** * Data para string * @param data * @param formato * @return * @author wul * 2016-1-17 * / public static string dateToString (data de data, formato de string) {SimpleDateFormat sdf = new SimledEformat (formato); String currentDate = sdf.format (data); retornar currentDate; } / ** * Data para timestamp * @param data * @return * @author wul * 2016-1-17 * / public static timestamp datetotimestamp (data) {timestamp ts = novo timestamp (date.gettime ()); retornar ts; } / ** * Timestamp para 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); tente {tsstr = sdf.format (ts); } catch (Exceção e) {e.printStackTrace (); } retornar tsstr; } / ** * Timestamp até o momento * @param ts * @return * @author wul * 2016-1-17 * / public date static timestampodate (timestamp ts) {return ts; } / ** * Obtenha o horário e o formato atuais: AA YYYY-MM-DD HH: MM: SS * @return * / public static string getCurrentTimenormal () {SimpleDateFormat sdf = novo SimpleDateFormat (DateTime_Normal_Format); String currentDate = sdf.format (new Date ()); retornar currentDate; } / ** * Obtenha a hora e o formato atuais: yyyymmddhhmmss * @return * / public static string getCurrenttimecompact () {SimpleDateFormat sdf = new SimpleDateFormat (DateTime_Compact_Format); String currentDate = sdf.format (new Date ()); retornar currentDate; } / ** * Obtenha o horário e o formato atuais: AA YYYY-MM-DD * @RETURN * / public static string getCurrentDatEnormal () {SimpleDateFormat sdf = new SimpleDateFormat (date_normal_format); String currentDate = sdf.format (new Date ()); retornar currentDate; } / ** * Obtenha a hora e o formato atuais: yyyymmdd * @return * / public static string getCurrentDatecompact () {simledateFormat sdf = new SimpleDateFormat (date_compact_format); String currentDate = sdf.format (new Date ()); retornar currentDate; } / ** * Format 20101202 Hora para 2010-12-02 * * @Param Datestring Time Time Formato: yyyymmdd * @return * / public static string getDatecompacttonormal (string dateString) {stringbuilder sb = new stringbuilder (); sb.append (DATEstring.substring (0,4)). Append ("-"). Append (DataString.Subsequence (4, 6)). Append ("-"). Append (DATEstring.substring (6, 8)); return sb.toString (); } / ** * Format 20101202101423 Tempo para 2010-12-02 10:14:23 * * @Param Datestring Time Formato: yyyyymmddhhmms * @return * / public static string getDatetimecompacttonormal (string datestring) {stringbuaber sb = stringbu (stringbu (string) {stringbuabilder sb = newbringbu (string) {stringbuilder sb sb.append (DATEstring.substring (0,4)). Append ("-"). Append (DataString.Subsequence (4, 6)). Append ("-"). Append (DATestring.substring (8, 8)) .Append (""). 10)). Anexe (":"). Anexe (DATEstring.substring (10, 12)). Anexe (":"). Append (DataString.substring (12)); return sb.toString (); } / ** * Converta a entrada de tempo na interface em uma sequência de tempo intermitente * 2010-12-02 10:14:23 Formatada como 20101202101423 * @param datenormalstr string * @return string * / public static string getCompActstring (string datNormalStr) {String Rector tente {ret.append (datenormaltr.substring (0, 4)); ret.append (datenormaltr.substring (5, 7)); ret.append (datenormaltr.substring (8, 10)); ret.append (datenormaltr.substring (11, 13)); ret.append (datenormaltr.substring (14, 16)); ret.append (datenormaltr.substring (17, 19)); } catch (Exceção ex) {// Se a sequência não for longa o suficiente, retorne a parte anterior} retornar ret.toString (); } / ** * Obtenha o ano no formato de tempo de 201202 (101423) * @param Datestring Time Formato: yyyymmdd (hhmms) * @return * / public static string getyear (string datestring) {return datestring.substring (0,4); } / ** * Obtenha o mês com o formato de tempo 20101202 (101423) * @Param Datestring Time: yyyymmdd (hhmms) * @return * / public static string getMonth (string datestring) {return datestring.substring (4,6); } / ** * Obtenha a data com o formato de tempo de hora de 20101202 * @param Datestring Time Formato: yyyymmdd * @return * / public static string getDayNotime (string dateString) {return datestring.substring (6); } / ** * Obtenha a data antes da data atual e avançar pelo número de dias * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getBeForedatePlusday (int numval, string dateFormat) {calendar calendar = calendar.getinStance (); long Currenttimemillis = calendar.getTimeInmillis (); long hourinmillis = 60 * 60 * 1000; DVAL longo = numVal * 24 * hourinmillis; SimpledateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currenttimemillis - dval); retornar currentDate; } / ** * Obtenha a data antes da data atual e avançar pelo número de dias * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getAfterDatePlusday (int numval, string dateFormat) {calendar calendar = calendar.getinStance (); long Currenttimemillis = calendar.getTimeInmillis (); long hourinmillis = 60 * 60 * 1000; DVAL longo = numVal * 24 * hourinmillis; SimpledateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currenttimemillis + dval); retornar currentDate; } / ** * Obtenha a data antes da data atual e avançar por hora * @param numVal * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getBefordatePlushour (int numval, string dateformat) {calendar calendar = calendar.getInstance (); long Currenttimemillis = calendar.getTimeInmillis (); long hourinmillis = 60 * 60 * 1000; dVal longo = numval * hourinmillis; SimpledateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currenttimemillis - dval); retornar currentDate; } / ** * Obtenha a data antes da data atual e avançar por hora * @param numVal * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getafterdateplushour (int numval, string dateformat) {calendar calendar = calendar.getInstance (); long Currenttimemillis = calendar.getTimeInmillis (); long hourinmillis = 60 * 60 * 1000; dVal longo = numval * hourinmillis; SimpledateFormat sdf = new SimpleDateFormat (dateFormat); String currentDate = sdf.format (currenttimemillis + dval); retornar currentDate; } / ** * Número de dias diferentes entre duas datas * @param begindate * @param enddate * @return * @author wul * 2016-1-18 * / public static int Daysbetween (data iniciada, data enddate) {calendário cal = calendar.getInstance (); cal.setTime (Begindate); longo tempo1 = cal.getTimeInmillis (); cal.setTime (enddate); longo tempo2 = cal.getTimeInmillis (); longo entre_days = (TIME2 - TIME1) / (1000 * 3600 * 24); return integer.parseint (string.valueof (entre_days)); } / ** * Obtenha o número de dias em um determinado mês * @param ano * @param mês * @return * @author wul * 2016-1-18 * / public static int getMonthDays (int ano, int mês) {calendário cal = calendar.getInstance (); cal.set (calendário.year, ano); cal.set (calendário.month, mês - 1); retornar cal.getActualMaximum (calendar.date); } / ** * Adicione ou subtraia o ano a tempo * @param data * @param putime * @return * @author wul * 2016-1-18 * / public static date getDateplusyear (data, int putime) {calendário cal = calendar.getInstance (); cal.setTime (data); cal.add (calendário.year, paixão); Data d = cal.gettime (); retornar d; } / ** * Adicionar ou subtrair o mês do tempo * @param data * @param putime * @return * @author wul * 2016-1-18 * / public date static getDateplusMONTH (data, int putime) {calendário cal = calendar.getInstance (); cal.setTime (data); cal.add (calendário.month, paixão); Data d = cal.gettime (); retornar d; } / ** * Adicione e subtraia o número de dias ao tempo * @param data * @param putime * @return * @author wul * 2016-1-18 * / public static date getDateplusday (data, int putime) {calendário cal = calendar.getInstance (); cal.setTime (data); cal.add (calendário.date, paixão); Data d = cal.gettime (); retornar d; } / ** * Adicionar ou subtrair o tempo * @param Data * @param Putime * @return * @author wul * 2016-1-18 * / public date static getDateplushour (data, int putime) {calendário cal = calendário.getInstance (); cal.setTime (data); cal.add (calendário.hour, paixão); Data d = cal.gettime (); retornar d; } / ** * Adicione ou subtraia o tempo * @param Data * @param Putime * @return * @author wul * 2016-1-18 * / public date static getDateplusminute (data, int putime) {calendar cal = calendar.getInStance (); cal.setTime (data); cal.add (calendário.Minute, Putime); Data d = cal.gettime (); retornar d; } / ** * Adicione e subtraia segundos ao tempo * @param data * @param putime * @return * @author wul * 2016-1-18 * / public static date getDateplussUsCond (data, int putime) {calendário cal = calendar.getInstance (); cal.setTime (data); cal.add (calendário.second, paixão); Data d = cal.gettime (); retornar d; } / ** * Retorne o ano atual * @return * @author wul * 2016-1-18 * / public static int getCurrieear () {calendário calendário = calendar.getInstance (); retorno calendário.get (1); } / ** * Retorne o mês atual * @return * @author wul * 2016-1-18 * / public static int getCurrentMonth () {calendário calendário = calendar.getInstance (); retorno calendário.get (2) + 1; } / ** * Retorne o dia atual * @return * @author wul * 2016-1-18 * / public static int getCurrentday () {calendário calendário = calendar.getInstance (); calendário de retorno.get (5); } / ** * Retorne a hora atual * @return * @author wul * 2016-1-18 * / public static int getCurrentHour () {calendário calendário = calendar.getInstance (); calendário de retorno.get (11); } / ** * Retorne o minuto atual * @return * @author wul * 2016-1-18 * / public static int getCurrentMinute () {calendário calendário = calendar.getInstance (); calendário de retorno.get (12); } / ** * Retorne o segundo segundo * @return * @author wul * 2016-1-18 * / public static int getCurrentSecond () {calendário calendário = calendar.getInstance (); Calendar.get.get (13); } / ** * Retorne o ano atual * @return * @author wul * 2016-1-18 * / public static int getyear (data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); retorno calendário.get (1); } / ** * Retorne o mês atual * @return * @author wul * 2016-1-18 * / public static int getMonth (data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); retorno calendário.get (2) + 1; } / ** * Retorne o dia atual * @return * @author wul * 2016-1-18 * / public static int getDay (data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); calendário de retorno.get (5); } / ** * Retorne a hora atual * @return * @author wul * 2016-1-18 * / public static int gethour (data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); calendário de retorno.get (11); } / ** * Retorne o minuto atual * @return * @author wul * 2016-1-18 * / public static int getMinute (data de data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); calendário de retorno.get (12); } / ** * Retorne o segundo segundo * @return * @author wul * 2016-1-18 * / public static int getSecond (data) {calendário calendário = calendar.getInstance (); calendário.setTime (data); Calendar.get.get (13); } public static void main (string [] args) {System.out.println (dateutil.dateToString (new java.sql.date (system.currenttimemillis ()), dateutil.date_normal_format); Mapa <string, object> map = new hashmap <string, object> (); map.put ("date", new Date ()); String json = jsonObject.FromObject (map) .ToString (); System.out.println (JSON); }}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.