تشترك هذه المقالة في مجموعة كاملة من تاريخ 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 dateformatutil {/ ** * timestamp حتى الآن * param ms * regurn */ public static date transfordate (integer ms) {if (ms == null) {ms = 0 ؛ } msl long = (long) ms*1000 ؛ SimpleDateFormat sdf = جديد simpledateFormat ("Yyyy-MM-DD HH: MM: SS") ؛ تاريخ التاريخ = فارغ ؛ if (ms! = null) {try {string str = sdf.format (msl) ؛ temp = sdf.parse (str) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ }} temp ؛ } / ** * احصل على الطابع الزمني في الساعة 9:30 مساءً * * return * / public static int getTimes (int day ، int int ، int minute) {calendar cal = calendar.getInstance () ؛ Cal.Add (Calendar.Date ، Day) ؛ cal.set (calendar.hour_of_day ، ساعة) ؛ cal.set (Calendar.Second ، 0) ؛ cal.set (calendar.minute ، دقيقة) ؛ cal.set (Calendar.MillisEcond ، 0) ؛ return (int) (cal.getTimeInMillis () / 1000) ؛ } / ** * احصل على الوقت الكامل للوقت الحالي * * return * / public 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) ؛ } int 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 حتى الآن * param ms * return * / public static date transfordate3 (integer ms) {if (ms == null) {ms = 0 ؛ } msl long = (long) ms*1000 ؛ SimplEdateFormat sdf = جديد simpledateFormat ("Yyyy-MM-DD HH: mm") ؛ تاريخ التاريخ = فارغ ؛ if (ms! = null) {try {string str = sdf.format (msl) ؛ temp = sdf.parse (str) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ }} temp ؛ } / ** * timestamp حتى الآن * param ms * @return * / public static date transfordate (long ms) {if (ms == null) {ms = (long) 0 ؛ } msl long = (long) ms*1000 ؛ SimpleDateFormat sdf = جديد simpledateFormat ("Yyyy-MM-DD HH: MM: SS") ؛ تاريخ التاريخ = فارغ ؛ if (ms! = null) {try {string str = sdf.format (msl) ؛ temp = sdf.parse (str) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ }} temp ؛ } السلسلة الثابتة العامة transfordate1 (integer ms) {string str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimpleDateFormat sdf = جديد simpledateFormat ("Yyyy-MM-DD HH: MM: SS") ؛ if (ms! = null) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }}} return str ؛ } السلسلة الثابتة العامة transfordate2 (integer ms) {string str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimplEdateFormat SDF = جديد spiStFormat ("Yyyy-MM-DD") ؛ if (ms! = null) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }}} return str ؛ } السلسلة الثابتة العامة transfordate4 (Integer MS) {String str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimplEdateFormat SDF = جديد spiStformat ("yyyy.mm.dd") ؛ if (ms! = null) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} return str ؛ } السلسلة الثابتة العامة transfordate5 (integer ms) {string str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimpleDateFormat SDF = جديد spiStFormat ("Yyyy/mm/dd HH: mm: ss") ؛ if (ms! = null) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} return str ؛ } السلسلة الثابتة العامة transfordateinchinese (integer ms) {string str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimpleDateFormat SDF = جديد spiStformat ("Yyyyy Year MM Month DD DATE HH: MM: SS") ؛ if (ms! = null) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} return str ؛ } / ** * التاريخ إلى Timestamp * param Date * @Return * / integer integer public transformillisecond (تاريخ التاريخ) {if (date == null) return null ؛ إرجاع (int) (date.getTime ()/1000) ؛ }/*** احصل على الطابع الزمني الحالي* regurn*/integer integer currentTimestamp () {int) (System.CurrentTimeMillis ()/1000) ؛ } / ** * سلسلة التاريخ إلى timestamp * param dateStr * @return * / integer integer public transformillisecond (سلسلة datest) {date date = dateformatutil.formatdate (datest) ؛ تاريخ العودة == فارغ؟ NULL: DateFormatutil.transformillisecond (DATE) ؛ } / ** * سلسلة التاريخ إلى timestamp * param dateStr * @RETURN * / integer static integer transformillisecond (String dateStr ، تنسيق السلسلة) {date dateformatutil.formatdate (datest ، format) ؛ تاريخ العودة == فارغ؟ NULL: DateFormatutil.transformillisecond (DATE) ؛ } / ** * سلسلة التاريخ إلى timestamp * param datestr * param تنسيق مثل "yyyymm-dd" * @return * / integer integer public transformillisecondbytim (String dateSt ، String tim) {simpledateformat sdf = new simpledateFormat (tim) ؛ تاريخ التاريخ = فارغ ؛ حاول {date = sdf.parse (dateSt) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ } تاريخ العودة == فارغ؟ NULL: DateFormatutil.transformillisecond (DATE) ؛ } / ** * سلسلة حتى الآن ، التنسيق هو: "yyyy-mm-dd hh: mm: ss" * param datestr * regurn * / public static date dateDate (String datest) {simpledateformat sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss") ؛ نتيجة التاريخ = فارغة ؛ حاول {result = sdf.parse (dateSt) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ } نتيجة الإرجاع ؛ } / ** * سلسلة حتى الآن ، التنسيق: "yyyy-mm-dd hh: mm: ss" * param dateStr * @return * / public static date tormatdate (String dateStr ، تنسيق السلسلة) {simpledateformat sdf = new SimplEdateFormat (format) ؛ نتيجة التاريخ = فارغة ؛ حاول {result = sdf.parse (dateSt) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ } نتيجة الإرجاع ؛ } / ** * تاريخ السلسلة * param Date * @RETURN * / public Static String FormatDate (Date Date) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-MM-DD HH: MM: SS") ؛ نتيجة السلسلة = فارغة ؛ النتيجة = sdf.format (التاريخ) ؛ نتيجة العودة } / ** * تاريخ السلسلة * param Date * return * / public static string formatdate (تاريخ التاريخ ، تنسيق السلسلة) {simpleDateFormat sdf = new SimplEdateFormat (format) ؛ نتيجة السلسلة = فارغة ؛ النتيجة = sdf.format (التاريخ) ؛ نتيجة العودة } / ** * إخراج تنسيق الطابع الزمني (لقالب httpl) * * param ms timestamp * param format format * @return * / public Static String transfordate (Integer MS ، تنسيق السلسلة) {String str = "" ؛ if (ms! = null) {long msl = (long) ms*1000 ؛ SimplEdateFormat SDF = جديد spileDateFormat (تنسيق) ؛ if (! ms.equals (0)) {try {str = sdf.format (msl) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }}} return str ؛ } / ** * خذ الجزء العدد أو الجزء العشري من رقم النوع BigDecimal (لقالب httpl) * * param b value * mode mode mode 0 routs 1 part * @return * / public static string splitbigdecimal (bigdecimal b ، int) سلسلة s = df.format (b) ؛ if (mode == 0) {return S.Split ("//.") [0] ؛ } آخر {return "."+S.Split ("//.") [1] ؛ }} / ** * احسب عدد الأيام التي الفرق بين تاريخين (لقوالب http) * * param ts1 timestamp 1 * param ts2 timestamp 2 * @return * / public static int caculate2days (Integer ts1 ، integer ts2) Date SecondDate = DateFormatutil.transfordate (TS2) ؛ التقويم التقويم = calendar.getInstance () ؛ Calendar.Settime (FirstDate) ؛ int daynum1 = calendar.get (calendar.day_of_year) ؛ Calendar.Settime (SecondDate) ؛ int daynum2 = calendar.get (calendar.day_of_year) ؛ return Math.ABS (daynum1 - daynum2) ؛ }/** * أضف علامة النجمة إلى الأرقام الأربعة المتوسطة من الهاتف المحمول * * param mobile * @RETURN */public string mopileserect (String mobile) {if (! stringutils.isblank (mobile)) {int in inter = mobile.length ()/2 ؛ mobile = mobile.substring (0 ، بين 2)+"****"+mobile.substring (بين+2 ، mobile.length ()) ؛ } إرجاع الهاتف المحمول ؛ } / ** * أضف علامة نجمية إلى عنوان البريد الإلكتروني * * param email * regurn * / string public emailSerSt (string email) {if (! stringUtils.isblank (email)) {int length = elem.lastindexof (" @") ؛ البريد الإلكتروني = البريد الإلكتروني. } البريد الإلكتروني لإرجاع ؛ } / ** * إضافة بيانات من نوع BigDecimal * * param bigDecimal Source * param bigdecimal target * regurn * / public bigdecimal sumbigdicimal (مصدر كبير ، الهدف الكبير) {source = source.add (target) ؛ مصدر الإرجاع ؛ } / ** * إضافة بيانات النوع BigDecimal * * param bigDecimal Source * param target bigdecimal * @REGRURN * / public bigdecimal sumbigdicimalanddouble (مصدر كبير ، هدف مزدوج) {bigdecimal new_target = new bigdecimal (target) ؛ Source = source.add (new_target) ؛ مصدر الإرجاع ؛ } / ** * طرح بيانات النوع BigDecimal * * param bigDecimal Source * param bigdecimal target * regurn * / public bigdecimal subbigdicimal (bigDecimal source ، target bigdecimal) {source = subtrict. مصدر الإرجاع ؛ } / *** احصل على فرق الوقت بين الوقت الوارد والوقت الحالي* @RETURN* / public static getTimediff (int timestamp) {date d1 = dateformatutil.transfordate (timestamp) ؛ تاريخ اليوم = تاريخ جديد () ؛ if (d1.getTime () <today.gettime ()) {return null ؛ } return (d1.getTime ()-اليوم. getTime ())/1000 ؛ } / *** احصل على تاريخ اليوم الأول لأسبوع معين* param week 0 هذا الأسبوع 1 الأسبوع السابق 1 الأسبوع المقبل* @REGARN* / public Static String Weekfirstday (int week) {calendar c1 = calendar.getInstance () ؛ int dow = c1.get (calendar.day_of_week) ؛ c1.add (Calendar.date ، -dow-7*(week-1) -5) ؛ String D1 = New SimplEdateFormat ("Yyyy-MM-DD"). التنسيق (c1.getTime ()) ؛ إرجاع D1+"00:00:00" ؛ } / *** الوقت الحالي بالإضافة إلى سنة واحدة* / السلسلة الثابتة العامة addyear (int startTime) {date firstDate = dateFormatutil.transfordate (starttime) ؛ التقويم التقويم = calendar.getInstance () ؛ Calendar.Settime (FirstDate) ؛ Calendar.add (Calendar.year ، 1) ؛ String D1 = new SimplEdateFormat ("Yyyy-MM-DD HH: MM: SS"). Format (Calendar.getTime ()) ؛ إرجاع D1 ؛ } / ** * احصل على آخر تاريخ لأسبوع معين * @param week * @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*(week-1) +1) ؛ String D1 = New SimplEdateFormat ("Yyyy-MM-DD"). التنسيق (c1.getTime ()) ؛ إرجاع D1+"23:59:59" ؛ } / *** المقارنة مع الوقت الحالي* @RETURN* / public static boolean greaterthannow (int timestamp) {date d1 = dateFormatutil.transfordate (timestamp) ؛ تاريخ اليوم = تاريخ جديد () ؛ if (d1.getTime ()> = today.getTime ()) {return true ؛ } إرجاع خطأ ؛ } / ** * HH: MM: يتم تحويل وقت تنسيق SS إلى طابع زمني من 1970-01-01 أيام (أي ، إذا كان هناك وقت فقط ولا يوجد تاريخ ، فمن المطلوب استخدام طابع زمني لتمثيل الوقت) * Author dingjiAcheng * / public static intfromtime (وقت السلسلة) HH: MM: SS ") ؛ } / ** * تم تحويل الطابع الزمني إلى HH: MM: SS Time Time (إزالة التاريخ) * Author DingjiAcheng * * / Static String transtotime (int time) {String s = new String (transfordate1 (time)) ؛ سلسلة ss [] = S.Split ("") ؛ إرجاع SS [1] ؛ } static int transtochuo (سلسلة البيانات) {simpleDateFormat SimpleDateFormat = new SimplEdateFormat ("Yyyy-MM-DD") ؛ int res = 0 ؛ جرب {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 (TransformIliSecond ("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"))) ؛ }} ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.