Cet article partage une collection complète de la conversion de la date et de l'horodatage Java pour votre référence. Le contenu spécifique est le suivant
Package Com.crm.util; import java.math.bigdecimal; import java.text.decimalformat; import java.text.parseException; import java.text.simpledateFormat; import java.util.calendar; Importer java.util.date; / ** * @author dingjiaceng * * / classe publique DateFormatutil {/ ** * TimeStamp à ce jour * @param ms * @return * / public static date transfordate (Integer ms) {if (ms == null) {ms = 0; } long msl = (long) ms * 1000; SimpledateFormat sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); Date de date = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printStackTrace (); }} retour de retour; } / ** * Obtenez l'horodatage à 21h30 * * @return * / public static int getTimes (int day, int hour, int mutte) {calendar cal = calendar.getInstance (); Cal.Add (calendar.date, jour); cal.set (calendar.hour_of_day, heure); cal.set (calendar.second, 0); cal.set (calendar.minute, minute); cal.set (calendar.millisecond, 0); return (int) (cal.getTimeInmillis () / 1000); } / ** * Obtenez le temps plein de l'heure actuelle * * @return * / public static int getIntegraltime () {calendar cal = calendar.getInstance (); cal.add (calendar.hour_of_day, 1); cal.set (calendar.second, 0); cal.set (calendar.minute, 0); cal.set (calendar.millisecond, 0); return (int) (cal.getTimeInmillis () / 1000); } public static int getIntegralTimeend () {calendar cal = calendar.getInstance (); cal.set (calendar.hour_of_day, 24); cal.set (calendar.second, 0); cal.set (calendar.minute, 0); cal.set (calendar.millisecond, 0); return (int) (cal.getTimeInmillis () / 1000); } / ** * Timestamp à ce jour * @param ms * @return * / public static Date transfordate3 (Integer ms) {if (ms == null) {ms = 0; } long msl = (long) ms * 1000; SimpledateFormat sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm"); Date de date = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printStackTrace (); }} retour de retour; } / ** * Timestamp à ce jour * @param ms * @return * / public static Date transfordate (long ms) {if (ms == null) {ms = (long) 0; } long msl = (long) ms * 1000; SimpledateFormat sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); Date de date = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printStackTrace (); }} retour de retour; } public static String Transfordate1 (entier 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 (entier 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 (entier 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 (entier 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; } String statique public TransfordateInChinese (entier MS) {String str = ""; if (ms! = null) {long msl = (long) ms * 1000; SimpledateFormat sdf = new SimpledateFormat ("Yyyyy Year MM MOSD DD DAT HH: MM: SS"); if (ms! = null) {try {str = sdf.format (msl); } catch (exception e) {e.printStackTrace (); }} return str; } / ** * Date à l'horodatage * @param Date * @return * / public static Integer transformIlliseCond (date de date) {if (date == null) return null; return (int) (date.getTime () / 1000); } / ** * Obtenez l'horodatage actuel * @return * / public static Integer currentTimestamp () {return (int) (System.CurrentTimeMillis () / 1000); } / ** * Date String to himestamp * @param datestr * @return * / public static Integer transformIlliseCond (String Datestr) {date date = dateFormatul.formatDate (datestr); Date de retour == null? null: dateFormatutil.transformillisecond (date); } / ** * Date String to timestamp * @param datestr * @return * / public static Integer transformIlliseCond (String Datestr, String Format) {Date Date = DateFormatul.FormatDate (Datestr, Format); Date de retour == null? null: dateFormatutil.transformillisecond (date); } / ** * Date String to TimeStamp * @param Datestr * @param Format tel que "Yyyy-mm-dd" * @return * / public static entier TransformIlliseCondBytim (String Datestr, String Tim) {SimpledateFormat sdf = new SimpledateFormat (Tim); Date de date = null; try {date = sdf.parse (datestr); } catch (parseException e) {e.printStackTrace (); } date de retour == null? null: dateFormatutil.transformillisecond (date); } / ** * chaîne à ce jour, le format est: "Yyyy-mm-dd hh: mm: ss" * @param datestr * @return * / public static date Formatdate (String Datestr) {SimpledateFormat sdf = new SimpledateFormat ("Yyyy-mm-dd hh: mm: ss"); Résultat de date = null; try {result = sdf.parse (datestr); } catch (parseException e) {e.printStackTrace (); } Retour Résultat; } / ** * chaîne à ce jour, format: "Yyyy-mm-dd hh: mm: ss" * @param datestr * @return * / public static date FormatDate (String Datestr, String Format) {SimpledateFormat sdf = new SimpledateFormat (format); Résultat de date = null; try {result = sdf.parse (datestr); } catch (parseException e) {e.printStackTrace (); } Retour Résultat; } / ** * Date à String * @param Date * @return * / public static String FormatDate (date de date) {SimpledateFormat sdf = new SimpledateFormat ("Yyyy-mm-dd hh: mm: ss"); Résultat de chaîne = null; result = sdf.format (date); Résultat de retour; } / ** * Date de chaîne * @param Date * @return * / public static String Formatdate (date de date, format de chaîne) {SimpledateFormat sdf = new SimpledateFormat (format); Résultat de chaîne = null; result = sdf.format (date); Résultat de retour; } / ** * Timestamp Format Sortie (pour le modèle HTTPL) * * @param ms TimeStamp * @param format format format * @return * / public static String Transfordate (entier ms, format de chaîne) {String str = ""; if (ms! = null) {long msl = (long) ms * 1000; SimpledateFormat sdf = new SimpledateFormat (format); if (! MS.Equals (0)) {try {str = sdf.format (msl); } catch (exception e) {e.printStackTrace (); }}} return str; } / ** * Prenez la partie entière ou décimale du numéro de type BigDecimal (pour le modèle HTTPL) * * @param B Valeur * @param mode mode 0 Rounds 1 Decimal Part * @return * / public static String SplitBigDecimal (BigDecimal B, int mode) {decimalformat df = new decimalformat ("0.00"); String s = df.format (b); if (mode == 0) {return s.split ("//.") [0]; } else {return "." + s.split ("//.") [1]; }} / ** * Calculez le nombre de jours que la différence entre deux dates (pour les modèles HTTP) * * @param TS1 Timestamp 1 * @param ts2 horodat Date SecondDate = DateFormatutil.transfordate (TS2); Calendar Calendar = calendar.getInstance (); Calendar.SetTime (FirstDate); int daynum1 = calendar.get (calendar.day_of_year); Calendar.SetTime (SecondDate); int daynum2 = calendar.get (calendar.day_of_year); Retour Math.Abs (DayNum1 - DayNum2); } / ** * Ajoutez un astérisque aux quatre chiffres du milieu du téléphone mobile * * @param mobile * @return * / public String mobileSerect (String mobile) {if (! StringUtils.isblank (mobile)) {int bnthing = mobile.length () / 2; mobile = mobile.substring (0, entre-2) + "****" + mobile.substring (entre + 2, mobile.length ()); } return mobile; } / ** * Ajoutez un astérisque à l'adresse e-mail * * @param e-mail * @return * / public String e-mailSerect (String Email) {if (! StringUtils.isblank (e-mail)) {int length = email.LastIndexof ("@"); e-mail = email.substring (0, 2) + "****" + email.substring (longueur-2, email.length ()); } retourner e-mail; } / ** * Additionner les données du type BigDecimal * * @param BigDecimal Source * @param BigDecimal Target * @return * / public Bigdecimal Sumbigdicimal (source BigDecimal, BigDecimal Target) {Source = Source.Add (Target); retour de la source; } / ** * Ajout de données de type BigDecimal * * @param BigDecimal Source * @param Bigdecimal Target * @return * / public bigdecimal sumbigdicimalandDouble (BigDecimal Source, double cible) {BigDecimal New_target = new Bigdecimal (Target); source = source.add (new_target); retour de la source; } / ** * Substraction des données de type BigDecimal * * @param BigDecimal Source * @param BigDecimal Target * @return * / public bigdecimal subbigdicimal (BigDecimal Source, BigDecimal Target) {Source = Source.Subtract (Target); retour de la source; } / ** * Obtenez le décalage horaire entre l'heure entrante et l'heure actuelle * @return * / public static long gettimediff (int himestamp) {date d1 = dateFormatul.transfordate (horodatamp); Date aujourd'hui = new Date (); if (d1.gettime () <aujourd'hui.getTime ()) {return null; } return (d1.getTime () - Today.getTime ()) / 1000; } / ** * Obtenez la première date de jour d'une certaine semaine * @param semaine 0 cette semaine 1 semaine précédente la semaine prochaine * @return * / public static String weekFirstday (int semaine) {calendar c1 = calendar.getinstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7 * (semaine-1) -5); String d1 = new SimpledateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); retourner d1 + "00:00:00"; } / ** * Heure actuelle plus un an * / public static String addyear (int starttime) {date firstDate = dateFormatul.transfordate (starttime); Calendar Calendar = calendar.getInstance (); Calendar.SetTime (FirstDate); calendar.add (calendar.year, 1); String d1 = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"). Format (calendar.getTime ()); retour d1; } / ** * Obtenez la dernière date du jour d'une certaine semaine * @param semaine * @return * / public static String WeekLastDay (int semaine) {calendar c1 = calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7 * (semaine-1) +1); String d1 = new SimpledateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); retourner d1 + "23:59:59"; } / ** * Comparaison avec l'heure actuelle * @return * / public static boolean oreadThannow (int himestamp) {date d1 = dateFormatul.transfordate (horodatamp); Date aujourd'hui = new Date (); if (d1.gettime ()> = aujourd'hui.getTime ()) {return true; } return false; } / ** * HH: MM: Le temps de format SS est converti en un horodatage de 1970-01-01 (c'est-à-dire, s'il n'y a que du temps et pas de date, il est nécessaire d'utiliser un horodatage pour représenter l'heure) * @author dingjiacheng * * / public static intfromtime (temps à cordes) {return transformillisecond ("1970-01-01" HH: MM: SS "); } / ** * Tampon horaire converti en HH: mm: SS Format HEUNE (Retrait de date) * @author dingJiAcheng * * / public static String TranStoTime (int time) {String s = new String (Transfordate1 (time)); String ss [] = s.split (""); retour ss [1]; } public static int transtochuo (string datestring) {simpledateFormat simpledateFormat = new SimpledateFormat ("yyyy-mm-dd"); int res = 0; try {date date = SimpledateFormat .Parse (dateString); res = (int) date.getTime (); } catch (parseException e) {e.printStackTrace (); } return res; } public static void main (String [] args) {//system.out.println (getIntegralTimeend ()); 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 "," Yyyy-mm-dd HH: MM: SS "))); //System.out.println (currentTiTaMP ()); //System.out.println (Transfordate (CurrentTiTamp ())); //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"))); }} 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.