В этой статье разделяется полная коллекция конверсии даты Java и временной метки для вашей ссылки. Конкретный контент заключается в следующем
Пакет com.crm.Util; импортировать java.math.bigdecimal; импортировать java.text.decimalformat; импортировать java.text.parseexception; импортировать java.text.simpledateformat; импортировать java.util.calendar; импортировать java.util.date; / ** * @author dingjiacheng * */ public class dateformatil {/ ** * timeStamp до даты * @param ms * @return */ public static date transfordate (целое число MS) {if (ms = null) {ms = 0; } long msl = (long) ms*1000; SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-mm-dd HH: MM: SS"); Дата температура = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseexception e) {e.printstacktrace (); }} return temp; } / ** * Получить временную метку в 9:30 вечера * * @return * / public static int getTimes (int day, int int miname) {calendar cal = calendar.getinstance (); Cal.Add (Calendar.Date, Day); cal.set (calendar.hour_of_day, час); Cal.Set (Calendar.second, 0); Cal.Set (Calendar.minute, Minute); cal.set (calendar.millisecond, 0); return (int) (cal.getTimeInmillis () / 1000); } / ** * Получите полное время в текущее время * * @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 to Date * @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"); Дата температура = null; if (ms! = null) {try {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseexception e) {e.printstacktrace (); }} return temp; } / ** * TimeStamp to Date * @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"); Дата температура = 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 = (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 (Integer 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 (Integer 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 (Integer 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 TransfordateInchinese (Integer MS) {string str = ""; if (ms! = null) {long msl = (long) ms*1000; SimpleDateFormat SDF = new SimpleDateFormat ("yyyyy Year Mm Month DD Date HH: MM: SS"); if (ms! = null) {try {str = sdf.format (msl); } catch (Exception e) {e.printstackTrace (); }} return str; } / ** * Дата TIMESTAMP * @param date * @return * / public static integer transformillisecond (дата дата) {if (date == null) return null; return (int) (date.gettime ()/1000); }/*** Получить текущую временную метку* @return*/public static integer currentTimeStamp () {return (int) (System.currentTimeMillis ()/1000); } / ** * Строка даты до timestamp * @param datestr * @return * / public static integer transformillisecond (string datestr) {date = date formatutil.formatdate (datestr); Дата возвращения == NULL? null: dateformatutil.transformillisecond (date); } / ** * Строка даты до timeStamp * @param datestr * @return * / public static integer transformillisecond (String datestr, string format) {date = date formatutil.formatdate (datest, format); Дата возвращения == NULL? null: dateformatutil.transformillisecond (date); } / ** * Строка даты к временной метке * @param datestr * @param формат, такой как "yyyy-mm-dd" * @return * / public static integer transformillisecondbytim (строка Datestr, String Tim) {SimpleDateformat sdf = new SimpleDateFormat (TIM); Дата даты = null; try {date = sdf.parse (datest); } catch (parseexception e) {e.printstacktrace (); } Дата возврата == NULL? null: dateformatutil.transformillisecond (date); } / ** * Строка На сегодняшний день формат: "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"); Дата результат = null; try {result = sdf.parse (datest); } catch (parseexception e) {e.printstacktrace (); } return Result; } / ** * Строка на сегодняшний день, формат: "yyyy-mm-dd hh: mm: ss" * @param datestr * @return * / public static date formatdate (string datestr, string format) {simpledateformat sdf = new SimpleDateFormat (format); Дата результат = null; try {result = sdf.parse (datest); } catch (parseexception e) {e.printstacktrace (); } return Result; } / ** * Дата строки * @param date * @return * / public static String formatdate (дата дата) {simpledateformat sdf = new SimpleDateFormat ("yyyy-mm-dd Hh: mm: ss"); String result = null; result = sdf.format (date); результат возврата; } / ** * Дата на строку * @param date * @return * / public Static String formatdate (дата дата, формат строки) {SimpleDateFormat sdf = new SimpleDateFormat (format); String result = null; result = sdf.format (date); результат возврата; } / ** * Выход формата TimeStamp (для шаблона httpl) * * @param ms timestamp * @param формат формата формата * @return * / public Static String Transfordate (Integer MS, строка формата) {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; } / ** * Возьмите целочисленную или десятичную часть числа BigDecimal Type (для шаблона HTTPL) * * @param b Значение * @param режим режима 0 раундов 1 десятичная часть * @return * / public Static String SplitBigDecimal (BigDecimal B, int Mode) {decimalformat df = new Decimalformat (0.00 "); Строка s = df.format (b); if (mode == 0) {return s.split ("//") [0]; } else {return "."+s.split ("//") [1]; }} / ** * Рассчитайте количество дней, в течение которых разница между двумя датами (для шаблонов HTTP) * * @param ts1 timestamp 1 * @param ts2 timestamp 2 * @return * / public int caculate2days (integer ts1, integer ts2) {date firstdate = dateformatil.transfortattate (ts1); Date SecondDate = DateFormatutil.Transfordate (TS2); Календарь календарь = календарь.getinstance (); Calendar.SetTime (FirstDate); int daynum1 = calendar.get (calendar.day_of_year); Calendar.SetTime (SecondDate); int daynum2 = calendar.get (calendar.day_of_year); вернуть math.abs (daynum1 - daynum2); }/** * Добавить звездочку в средние четыре цифры мобильного телефона * * @param mobile * @return */public String mobileserect (String Mobile) {if (! StringUtils.isblank (mobile)) {int moide = mobile.length ()/2; mobile = mobile.substring (0, между 2)+"****"+mobile.substring (между+2, mobile.length ()); } return Mobile; } / ** * Добавить звездочку к адресу электронной почты * * @param email * @return * / public String electserect (string email) {if (! Stringutils.isblank (email)) {int length = email.lastIndexof (" @"); email = email.substring (0, 2)+"****"+email.substring (длина 2, email.length ()); } вернуть электронное письмо; } / ** * Сложите данные о BigDecimal Type * * @param Bigdecimal Source * @param Bigdecimal Target * @return * / public BigDecimal Sumbigdicimal (BigDecimal Source, BigDecimal Target) {Source = Source.Add (Target); источник возврата; } / ** * Добавление данных BigDecimal Type * * @param BigDecimal Source * @param Bigdecimal Target * @return * / public BigDecimal SumbigdicimalandDuble (BigDecimal Source, Double Target) {BigDecimal new_Target = new BigDecimal (Target); source = source.add (new_target); источник возврата; } / ** * Вычитание данных BigDecimal Type * * @param BigDecimal Source * @param Bigdecimal Target * @return * / public BigDecimal subbigdicimal (BigDecimal Source, BigDecimal Target) {source = source.subtract (target); источник возврата; } / *** Получите разницу во времени между входящим временем и текущим временем* @return* / public static long gettimediff (int timestamp) {date d1 = dateformatil.transfordate (timestamp); Дата сегодня = новая дата (); if (d1.gettime () <сегодня.gettime ()) {return null; } return (d1.getTime ()-сегодня.getTime ())/1000; } / *** Получите первый день даты определенной недели* @param Week 0 На этой неделе 1 предыдущая неделя-1 следующая неделя* @return* / public Static String Weekfirstday (int Week) {Calendar C1 = Calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(неделя-1) -5); String D1 = new SimpleDateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); вернуть D1+"00:00:00"; } / *** Текущее время плюс один год* / public Static String Addyear (int startTime) {date firstDate = dateFormatil.Transfordate (startTime); Календарь календарь = календарь.getinstance (); Calendar.SetTime (FirstDate); Calendar.add (Calendar.year, 1); String D1 = new SimpleDateFormat ("yyyy-mm-dd HH: MM: SS"). Format (calendar.getTime ()); вернуть D1; } / ** * Получить дату последнего дня определенной недели * @param неделя * @return * / public Static String Weeklastday (int Week) {Calendar C1 = Calendar.getInstance (); int dow = c1.get (calendar.day_of_week); c1.add (calendar.date, -dow-7*(неделя-1) +1); String D1 = new SimpleDateFormat ("yyyy-mm-dd"). Format (c1.getTime ()); вернуть D1+"23:59:59"; } / *** Сравнение с текущим временем* @return* / public static boolean bighnhannow (int timestamp) {date d1 = dateformatil.transfordate (timestamp); Дата сегодня = новая дата (); if (d1.gettime ()> = сегодня.gettime ()) {return true; } вернуть false; } / ** * HH: MM: SS Формат Время конвертируется в марки времени 1970-01-01 дней (то есть, если есть только время, а дата не требуется использовать марку времени для представления времени) * @author dingjiacheng * * / public int transfromtime (String Time) {return TransformIlisecececer (»1970-01-0,011111 годы. HH: MM: SS "); } / ** * Трэнд, преобразованный в HH: MM: SS Format Time (Date Stremoval) * @author Dingjiacheng * * / Public Static String Transtotime (int Time) {String S = New String (transfortate1 (time)); String ss [] = s.split ("" "); вернуть 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 (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 (Transpromtime ("15:01:13"))); }} Выше всего содержание этой статьи. Я надеюсь, что это будет полезно для каждого обучения, и я надеюсь, что все будут поддерживать Wulin.com больше.