Dieser Artikel hat eine vollständige Sammlung von Java -Datum und Zeitstempelkonvertierung für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Paket com.crm.util; import Java.math.bigdecimal; importieren java.text.decimalformat; Java.text.ParseException importieren; Java.Text.SimpledateFormat importieren; import Java.util.calendar; import Java.util.date; / ** * @Author dingjiACHENG * */ public class dateFormatutil {/ ** * Zeitstempel bis Datum * @param ms * @return */ public static date transfordate (Integer ms) {if (ms == null) {ms = 0; } lange msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); Datum temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} return temp; } / ** * Erhalten Sie den Zeitstempel um 21:30 Uhr * * @return * / public static int Gettime (int Day, int stündlich, int minute) {Calendar cal = calendar.getInstance (); Cal.add (Kalender.Date, Tag); Cal.set (Calendar.hour_of_day, Stunde); Cal.set (Calendar.second, 0); Cal.set (Calendar.minute, Minute); Cal.set (Calendar.Millisecond, 0); return (int) (cal.getTimeInmillis () / 1000); } / ** * Die aktuelle Zeit in Vollzeit erhalten * * @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 get integralTimeend () {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); } / ** * Zeitstempel bis Datum * @param ms * @return * / public static Date transfordate3 (Integer ms) {if (ms == null) {ms = 0; } lange msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy-mm-dd hh: mm"); Datum temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} return temp; } / ** * Zeitstempel bis Datum * @param ms * @return * / public statisches Datum transfordate (long ms) {if (ms == null) {ms = (lang) 0; } lange msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); Datum temp = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseException e) {e.printstacktrace (); }} return temp; } public static String transfordate1 (Integer ms) {String str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }}} return str; } public static String transfordate2 (Integer ms) {String str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpledateFormat SDF = new SimpledateFormat ("yyyy-mm-dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }}} return str; } public static String transfordate4 (Integer ms) {String str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy.mm.dd"); if (ms! = null) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }} return str; } public static String transfordate5 (Integer ms) {String str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyy/mm/dd hh: mm: ss"); if (ms! = null) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }} return str; } public static String transfordateinchinese (Ganzzahl ms) {String str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat ("yyyyy Year Mm Monat DD Datum HH: MM: SS"); if (ms! = null) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }} return str; } / ** * Datum zum Zeitstempel * @param Datum * @return * / public static Integer Transformillisecond (Datum Datum) {if (Datum == null) return null; return (int) (date.getTime ()/1000); }/*** den aktuellen Zeitstempel erhalten* @return*/public static Integer currentTimestamp () {return (int) (System.currentTimemillis ()/1000); } / ** * Datumsstring zum Zeitstempel * @param datenStr * @return * / public static Integer Transformillisecond (String datestr) {Date date = dateFormatutil.formatDate (datenStr); Rückgabedatum == NULL? NULL: DateFormatutil.transformillisecond (Datum); } / ** * Datumsstring zum Zeitstempel * @param datenStr * @return * / public static Integer Transformillisecond (String datestr, String -Format) {Date date = dateFormAtil.formatdate (datenStr, Format); Rückgabedatum == NULL? NULL: DateFormatutil.transformillisecond (Datum); } / ** * Datum String to Timestamp * @param datenStr * @param-Format wie "yyyy-mm-dd" * @return * / public static Integer transformillisecondbytim (String datenStr, String Tim) {SimpleDateFormat Sdf = new SimpleDateFormat (Tim); Datum Datum = null; try {date = sdf.parse (datestryStrySt); } catch (parseException e) {e.printstacktrace (); } Rückgabedatum == NULL? NULL: DateFormatutil.transformillisecond (Datum); } / ** * String bisher, das Format ist: "yyyy-mm-dd hh: mm: ss" * @param datenStr * @return * / public static date formatdate (String datenStr) {SimpleDateFormat Sdf = new SimpleDateFormat ("yyyy-mm-dd hh: mm: mm: SSS"); Datumsergebnis = null; try {result = sdf.parse (datestrySt); } catch (parseException e) {e.printstacktrace (); } Rückgabeergebnis; } / ** * String bisher, Format: "yyyy-mm-dd hh: mm: ss" * @param datenESTR * @return * / public statisches Datumsformatdat (String datenStr, String-Format) {SimpleDateFormat sdf = new SimpleDateFormat (Format); Datumsergebnis = null; try {result = sdf.parse (datestrySt); } catch (parseException e) {e.printstacktrace (); } Rückgabeergebnis; } / ** * Datum auf Zeichenfolge * @param Datum * @return * / public static String FormatDate (Datum) {SimpleDateFormat sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); String -Ergebnis = null; result = sdf.format (Datum); Rückgabeergebnis; } / ** * Datum an Zeichenfolge * @param Datum * @return * / public static String FormatDate (Datum, String -Format) {SimpledateFormat SDF = new SimpleDateFormat (Format); String -Ergebnis = null; result = sdf.format (Datum); Rückgabeergebnis; } / ** * Zeitstempelformat Ausgabe (für HTTPL -Vorlage) * * @param ms Timestamp * @param Formatformat * @return * / public static String transfordate (Ganzzahl ms, String -Format) {String Str = ""; if (ms! = null) {long msl = (lang) ms*1000; SimpleDateFormat SDF = new SimpledateFormat (Format); if (! ms.equals (0)) {try {str = sdf.format (msl); } catch (Ausnahme e) {e.printstacktrace (); }}} return str; } / ** * Nehmen Sie den Ganzzahl- oder Dezimalteil der BigDecimal -Typ -Nummer (für HTTPL -Vorlage) * * @param b value * @param Modus 0 Runden 1 Dezimaler Teil * @return * / public static String SplitbigDecimal (BigDecimal B, Int -Modus) {{DecimalFormat df = newn Newalformat ("0.00"); String S = df.format (b); if (modus == 0) {return ssplit ("//.") [0]; } else {return "."+ssplit ("//.") [1]; }} / ** * Berechnen Sie die Anzahl der Tage, an denen der Unterschied zwischen zwei Daten (für HTTP -Vorlagen) * * @param TS1 TIMESTAMP 1 * @PARAM TS2 TIMESTAMP 2 * @RETURN * / public static int Caculate2days (Integer ts1, Integer ts2) {Datum erster Date = DateDate = DateDate = DateDate = DateDate = DateMatil.Til.Refordate (ts2); Datum SecondDate = DateFormatutil.transfordate (TS2); Calendar Calendar = Calendar.getInstance (); Kalender.Settime (FirstDate); int daynum1 = calendar.get (calendar.day_of_year); Kalender.Settime (SecondDate); int daynum2 = calendar.get (Calendar.day_of_year); return math.abs (Daynum1 - Daynum2); }/** * Fügen Sie den mittleren vier Ziffern des Mobiltelefons ein Sternchen hinzu * @param Mobile * @return */public String mobileSerect (String mobile) {if (! Stringutils.isblank (mobil)) {int zwischen = mobilen.Length ()/2; mobil = mobile.substring (0, zwischen 2)+"****"+mobil.substring (zwischen+2, mobile.length ()); } Mobil zurückgeben; } / ** * Fügen Sie der E -Mail -Adresse ein Sternchen hinzu * * @param E -Mail * @return * / public String E -MailSerect (String -E -Mail) {if (! Stringutils.isblank (E -Mail)) {int Länge = E -Mail.lastindexof (" @"); E-Mail = E-Mail.Substring (0, 2)+"****"+E-Mail.substring (Länge-2, E-Mail.length ()); } E -Mail zurückgeben; } / ** * Daten des BigDecimal -Typs addieren * * @param BigDecimal Source * @param BigDecimal Ziel * @return * / public BigDecimal SumbigDicimal (BigDecimal Source, BigDecimal Target) {Source = Source.Add (Ziel); Quelle zurückkehren; } / ** * Zugabe von BigDecimal -Typ -Daten * * @param BigDecimal Source * @param BigDecimal Ziel * @return * / public bigDecimal sumbigDicimalandDouble (BigDecimal Source, Double Target) {BigDecimal new_target = new BigDecimal (Ziel); Source = Source.Add (new_target); Quelle zurückkehren; } / ** * Subtraktion von Daten von BigDecimal -Typen * * @param BigDecimal Source * @param BigDecimal Ziel * @return * / public BigDecimal subbigDicimal (BigDecimal Source, BigDecimal Target) {Source = Source.Subtract (Ziel); Quelle zurückkehren; } / *** Erhalten Sie den Zeitunterschied zwischen der eingehenden Zeit und der aktuellen Zeit* @return* / public static long Timediff (int timestamp) {Datum d1 = DateFormatutil.transfordate (TimeStamp); Datum heute = neues Datum (); if (d1.getTime () <Today.getTime ()) {return null; } return (d1.getTime ()-Today.getTime ())/1000; } / *** Erhalten Sie den ersten Tag einer bestimmten Woche* @param week 0 in dieser Woche 1 in der Vorwoche-1 Nächste Woche* @return* / public static String weekFirstday (int week) {calendar c1 = calender.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (Calendar.date, -dow-7*(Woche-1) -5); String d1 = new SimpledateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); Rückkehr D1+"00:00:00"; } / *** aktuelle Zeit plus ein Jahr* / public static String addyear (int startTime) {Datum firstDate = DateFormatutil.transfordate (StartTime); Calendar Calendar = Calendar.getInstance (); Kalender.Settime (FirstDate); Calendar.add (Kalender.YEAR, 1); String d1 = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"). Format (Calendar.getTime ()); Rückkehr D1; } / ** * Erhalten Sie den letzten Tag einer bestimmten Woche * @param week * @return * / public static String weelastday (int week) {calendar c1 = calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (Calendar.date, -dow-7*(Woche-1) +1); String d1 = new SimpledateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); Rückkehr D1+"23:59:59"; } / *** Vergleich mit der aktuellen Zeit* @return* / public static boolean Greaterthannow (int timestamp) {Datum d1 = DateFormatutil.transfordate (TimeStamp); Datum heute = neues Datum (); if (d1.getTime ()> = Today.getTime ()) {return true; } return false; } /** * HH:mm:ss format time is converted to a time stamp of 1970-01-01 days (that is, if there is only time and no date, it is required to use a time stamp to represent the time) * @author DingJiaCheng * */ public static int transFromTime(String time){ return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH: MM: SS "); } / ** * Zeitstempel in HH: MM: SS -Formatzeit (Datumentfernung) * @Author dingjiACHENG * * / public static String transtotime (int Zeit) {String S = new String (transfordate1 (Zeit))); String ss [] = S.Split (""); Rückkehr SS [1]; } public static int transtochuo (String datestring) {SimpleDateFormat SimpleDateFormat = new SimpledateFormat ("yyyy-mm-dd"); int res = 0; try {date date = SimpledateFormat .Parse (DETESTRING); 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","yyyy-mm-dd ")); //System.out.println(Transfordate1(Transformillisecond("1970-01-01 00:00:00 "," yyyy-mm-dd hh: mm: ss "))); //System.out.println (currentTimestamp ()); //System.out.println (transfordate (currentTimestamp ())); //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")); }} Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.