Cet article partage la classe d'outils DateUtils pour la conversion de temps pour votre référence. Le contenu spécifique est le suivant
Importer java.sql.timestamp; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.map; importer java.util.map; * 2015-12-31 * / classe publique DateUtil {public static final chaîne date_normal_format = "yyyy-mm-dd"; 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 = "Yyyymm"; / ** * String to timestamp * @param datestr * @return * @author wul * 2016-17 * / public static horodatamp stringToTiMestamp (String Datestr) {try {if (datestr.length () <= 10) {datestr + = "00:00:00"; } return timestamp.valueof (datestr); } catch (exception e) {e.printStackTrace (); retourner null; }} / ** * chaîne à ce jour * @param datestr * @param format * @return * @author wul * 2016-1-17 * / public static date stringTodate (String Datestr, String Format) {if (datestr == null || "" .equals (datestr)) {return null; } Date de date = null; // Notez que le format de format doit correspondre au format de la chaîne de date SimpledateFormat sdf = new SimpledateFormat (format); try {date = sdf.parse (datestr); } catch (exception e) {e.printStackTrace (); } Date de retour; } / ** * Date à String * @param Date * @param Format * @return * @author wul * 2016-17 * / public static String DateToString (date de date, format de chaîne) {SimpledateFormat sdf = new SimpledateFormat (format); String currentDate = sdf.format (date); return currentDate; } / ** * Date à l'horodatage * @param Date * @return * @author wul * 2016-1-17 * / public static horodatamp DateToTiMestamp (date de date) {TimeStamp ts = new TimeStamp (date.getTime ()); retourner ts; } / ** * TimeStamp to String * @param ts * @return * @author wul * 2016-1-17 * / public static static timestamptoString (horodatamp ts) {String tsstr = null; SimpledateFormat sdf = new SimpledateFormat (datetime_normal_format); try {tsstr = sdf.format (ts); } catch (exception e) {e.printStackTrace (); } return tsstr; } / ** * TimeStamp à ce jour * @param ts * @return * @author wul * 2016-1-17 * / public static Date TimeStamptodate (TimeStamp TS) {return ts; } / ** * Obtenez l'heure et le format actuels: yyyy-mm-dd hh: mm: ss * @return * / public static static getCurrentTimeNormal () {SimpledateFormat sdf = new SimpledateFormat (DateTime_Normal_Format); String currentDate = sdf.format (new Date ()); return currentDate; } / ** * Obtenez l'heure et le format actuels: yyyymmddhhmmss * @return * / public static string getCurrentTimeCompact () {SimpledateFormat sdf = new SimpledateFormat (datetime_compact_format); String currentDate = sdf.format (new Date ()); return currentDate; } / ** * Obtenez l'heure et le format actuels: yyyy-mm-dd * @return * / public static static getCurrentDaTeNormal () {SimpledateFormat sdf = new SimpledateFormat (date_normal_format); String currentDate = sdf.format (new Date ()); return currentDate; } / ** * Obtenez l'heure et le format actuels: yyyymmdd * @return * / public static static getCurrentDateCompact () {SimpledateFormat sdf = new SimpledateFormat (date_compact_format); String currentDate = sdf.format (new Date ()); return currentDate; } / ** * Format 20101202 Temps vers 2010-12-02 * * @param Datestring Time Format: yyyymmdd * @return * / public static String getDateCompactTonormal (String datestring) {StringBuilder sb = new StringBuilder (); SB.APPEND (DateString.SubString (0,4)). APPEND ("-"). APPEND (DateString.SubSequence (4, 6)). APPEND ("-"). A SPEND (DateString.SubString (6, 8)); return sb.toString (); } / ** * Format 20101202101423 Temps vers 2010-12-02 10:14:23 * * @param Datestring Time Format: yyyymmddhhmmss * @return * / public static string getDateTiMeCactTonormal (string datestring) {stringBuilder sb = new StringBuilder (); SB.APPEND (DateString.SubString (0,4)). APPEND ("-"). APPEND (Datestring.SubSequence (4, 6)). APPEND ("-"). APPEND (DateString.Substring (6, 8)) .APPEND ("") .APPEND (Datestring.Substring (8, 10)). APPEND (":"). APPEND (DateString.Substring (10, 12)). APPEND (":"). APPEND (DateString.Substring (12)); return sb.toString (); } / ** * Convertissez l'entrée de temps dans l'interface en une chaîne de temps intermittent * 2010-12-02 10:14:23 Time formaté comme 20101202101423 * @param datenormalstr * @return String * / public static getCompactStRing (string datenormalstr) {stringBuffer ret = new StringBuffer ();); try {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 (exception ex) {// Si la chaîne n'est pas assez longue, renvoyez la partie précédente} return ret.toString (); } / ** * Obtenez l'année dans le format de temps 20101202 (101423) * @param datestring Time Format: yyyymmdd (hhmmss) * @return * / public static String getyear (String datestring) {return datestring.substring (0,4); } / ** * Obtenez le mois avec le format de temps 20101202 (101423) * @param DateString Time Format: yyyymmdd (hhmmss) * @return * / public static static getMonth (String datestring) {return datestring.substring (4,6); } / ** * Obtenez la date avec le format de temps 20101202 * @param Datestring Time Format: yyyymmdd * @return * / public static static getDayNoTime (String dateString) {return datestring.substring (6); } / ** * Obtenez la date avant la date actuelle et avancez par le nombre de jours * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static string getBeforeDatePlusday (int numval, string dateFormat) {Calendar Calendar = Calendar.GedAnstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); long hourminmillis = 60 * 60 * 1000; long dval = numval * 24 * hourminmillis; SimpledateFormat sdf = new SimpledateFormat (DateFormat); String currentDate = sdf.format (currentTimemillis - dval); return currentDate; } / ** * Obtenez la date avant la date actuelle et avancez par le nombre de jours * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static String getAfterDatePlusday (int numval, string dateFormat) {Calendar Calendar = Calendar.gendary (); Long CurrentTimeMillis = calendar.getTimeInmillis (); long hourminmillis = 60 * 60 * 1000; long dval = numval * 24 * hourminmillis; SimpledateFormat sdf = new SimpledateFormat (DateFormat); String currentDate = sdf.format (currentTimemillis + dval); return currentDate; } / ** * Obtenez la date avant la date actuelle et avancez à l'heure * @param numval * @param dateFormat * @return * @author wul * 2016-1-17 * / public static String getBeforeDatePlushour (int numval, string dateFormat) {calendar calendar = calendar.GetInstance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); long hourminmillis = 60 * 60 * 1000; long dval = numval * hourminmillis; SimpledateFormat sdf = new SimpledateFormat (DateFormat); String currentDate = sdf.format (currentTimemillis - dval); return currentDate; } / ** * Obtenez la date avant la date actuelle et poussez à l'heure * @param numval * @param dateFormat * @return * @author wul * 2016-17 * / public static static getafterDatePlushour (int numval, string dateFormat) {Calendar Calendar = calendar.GetInStance (); Long CurrentTimeMillis = calendar.getTimeInmillis (); long hourminmillis = 60 * 60 * 1000; long dval = numval * hourminmillis; SimpledateFormat sdf = new SimpledateFormat (DateFormat); String currentDate = sdf.format (currentTimemillis + dval); return currentDate; } / ** * Nombre de jours différents entre deux dates * @param Begindate * @param enddate * @return * @author wul * 2016-1-18 * / public static int Daysbetween (date Begindate, date EndDate) {Calendar Cal = calendar.getInstance (); Cal.settime (Begindate); long time1 = cal.getTimeInmillis (); cal.settime (enddate); long time2 = cal.getTimeInmillis (); long entre_days = (Time2 - Time1) / (1000 * 3600 * 24); return Integer.ParseInt (String.ValueOf (Entre_Days)); } / ** * Obtenez le nombre de jours dans un certain mois * @param année * @param mois * @return * @author wul * 2016-18 * / public static int getMonthdays (int an, int mois) {calendar cal = calendar.getInSistance (); Cal.set (calendar.year, année); Cal.set (calendrier.month, mois - 1); return cal.getactualMaxim (calendar.date); } / ** * Ajouter ou soustraire l'année à temps * @param Date * @param pLusTime * @return * @author wul * 2016-18 * / public static date getDatePlusyear (date de date, int plustime) {calendar cal = calendar.getInstance (); cal.settime (date); Cal.Add (calendar.year, plustime); Date d = cal.gettime (); retour d; } / ** * Ajouter ou soustraire le mois de l'heure * @param Date * @param Plustime * @return * @author wul * 2016-18 * / public static date getDatePlusMonth (date de date, int plustime) {calendar cal = calendar.getInstance (); cal.settime (date); Cal.Add (Calendar.month, Plustime); Date d = cal.gettime (); retour d; } / ** * Ajouter et soustraire le nombre de jours à temps * @param Date * @param PLUSTIME * @return * @author wul * 2016-1-18 * / public static Date GetDatePlusday (date de date, int plustime) {Calendar Cal = Calendar.getInstance (); cal.settime (date); Cal.Add (calendar.date, plustime); Date d = cal.gettime (); retour d; } / ** * Ajouter ou soustraire l'heure * @param Date * @param PLUSTIME * @return * @author wul * 2016-18-18 * / public static Date getDatePlushour (date de date, int plustime) {Calendar Cal = Calendar.getInStance (); cal.settime (date); Cal.Add (calendar.hour, plustime); Date d = cal.gettime (); retour d; } / ** * Ajouter ou soustraire l'heure * @param Date * @param PLUSTIME * @return * @author wul * 2016-18-18 * / public static Date getDatePlusMinute (date de date, int plustestime) {Calendar Cal = Calendar.getInStance (); cal.settime (date); Cal.Add (calendar.minute, plustime); Date d = cal.gettime (); retour d; } / ** * Ajouter et soustraire les secondes à l'heure * @param Date * @param PLUSTIME * @return * @author wul * 2016-18 * / public static date getDatePlusSecond (date de date, int plustime) {calendar cal = calendar.getInstance (); cal.settime (date); Cal.Add (calendar.second, plustime); Date d = cal.gettime (); retour d; } / ** * Renvoie l'année en cours * @return * @author wul * 2016-18 * / public static int getCurlentyEar () {calendar calendar = calendar.getInstance (); retour calendar.get (1); } / ** * Renvoie le mois en cours * @return * @author wul * 2016-1-18 * / public static int getCurrentmonth () {calendar calendar = calendar.getInstance (); retour calendar.get (2) + 1; } / ** * Renvoie le jour actuel * @return * @author wul * 2016-1-18 * / public static int getCurrentDay () {calendar calendar = calendar.getInstance (); retour calendar.get (5); } / ** * Renvoie l'heure actuelle * @return * @author wul * 2016-1-18 * / public static int getCurrentHour () {calendar calendar = calendar.getInstance (); retour calendar.get (11); } / ** * Renvoie la minute actuelle * @return * @author wul * 2016-1-18 * / public static int getCurrentMinute () {calendar calendar = calendar.getInSitance (); retour calendar.get (12); } / ** * Renvoie le deuxième actuel * @return * @author wul * 2016-1-18 * / public static int getCurrentSecond () {calendar calendar = calendar.getInstance (); retour calendar.get (13); } / ** * Renvoie l'année en cours * @return * @author wul * 2016-1-18 * / public static int getyear (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour calendar.get (1); } / ** * Renvoie le mois en cours * @return * @author wul * 2016-1-18 * / public static int getMonth (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour calendar.get (2) + 1; } / ** * Renvoie le jour en cours * @return * @author wul * 2016-1-18 * / public static int getday (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour calendar.get (5); } / ** * Renvoie l'heure actuelle * @return * @author wul * 2016-1-18 * / public static int gethour (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour calendar.get (11); } / ** * Renvoie la minute actuelle * @return * @author wul * 2016-1-18 * / public static int getMinute (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour calendar.get (12); } / ** * Renvoie le deuxième actuel * @return * @author wul * 2016-1-18 * / public static int getSecond (date de date) {calendar calendar = calendar.getInstance (); calendar.settime (date); retour 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 ("Date", new Date ()); String JSON = JSONObject.FromObject (Map) .ToString (); System.out.println (JSON); }}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.