บทความนี้แบ่งปันคอลเลกชันที่สมบูรณ์ของวันที่ Java และการแปลงเวลาสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
แพ็คเกจ com.crm.util; นำเข้า java.math.bigdecimal; นำเข้า java.text.decimalformat; นำเข้า java.text.parseException; นำเข้า java.text.simpledateFormat; นำเข้า Java.util.Calendar; นำเข้า java.util.date; / ** * @author dingjiacheng * */ คลาสสาธารณะวันที่ DateFormatUtil {/ ** * timestamp จนถึงวันที่ * @param ms * @return */ วันที่สาธารณะ transfordate (จำนวนเต็ม ms) {ถ้า (ms == null) {ms = 0; } ยาว msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD HH: MM: SS"); วันที่อุณหภูมิ = null; if (ms! = null) {ลอง {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseexception e) {e.printstacktrace (); }} ส่งคืนอุณหภูมิ; } / ** * รับการประทับเวลาเวลา 21:30 น. * * @return * / public Static int getTimes (วัน int, int ชั่วโมง, int นาที) {ปฏิทิน 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 * / สาธารณะคงที่ int getInteGralTime () {ปฏิทิน 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 getInteGralTimeEnd () {ปฏิทิน 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 * / วันที่สาธารณะคงที่ transfordate3 (จำนวนเต็ม ms) {ถ้า (ms == null) {ms = 0; } ยาว msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD HH: MM"); วันที่อุณหภูมิ = null; if (ms! = null) {ลอง {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseexception e) {e.printstacktrace (); }} ส่งคืนอุณหภูมิ; } / ** * timestamp ถึงวันที่ * @param ms * @return * / วันที่สาธารณะคงที่ transfordate (ยาว ms) {ถ้า (ms == null) {ms = (ยาว) 0; } ยาว msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD HH: MM: SS"); วันที่อุณหภูมิ = null; if (ms! = null) {ลอง {string str = sdf.format (msl); temp = sdf.parse (str); } catch (parseexception e) {e.printstacktrace (); }} ส่งคืนอุณหภูมิ; } สตริงคงที่สาธารณะ transfordate1 (จำนวนเต็ม MS) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD HH: MM: SS"); if (ms! = null) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }}} return str; } สตริงคงที่สาธารณะ transfordate2 (จำนวนเต็ม MS) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD"); if (ms! = null) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }}} return str; } สตริงคงที่สาธารณะ transfordate4 (จำนวนเต็ม MS) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("yyyy.mm.dd"); if (ms! = null) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }} return str; } สตริงคงที่สาธารณะ transfordate5 (จำนวนเต็ม MS) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("yyyy/mm/dd Hh: mm: ss"); if (ms! = null) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }} return str; } สตริงคงที่สาธารณะ transfordateinchinese (จำนวนเต็ม MS) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = New SimpledateFormat ("Yyyyy Year MM เดือน DD วันที่ HH: MM: SS"); if (ms! = null) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }} return str; } / ** * วันที่ timestamp * @param วันที่ * @return * / public Static Integer transformillisecond (วันที่) {ถ้า (วันที่ == null) ส่งคืน null; return (int) (date.getTime ()/1000); }/*** รับ timestamp ปัจจุบัน* @return*/public Static Integer currentTimestamp () {return (int) (System.CurrentTimeMillis ()/1000); } / ** * วันที่สตริงเป็น timestamp * @param datestr * @return * / public Static Integer transformillisecond (String datestr) {วันที่ = วันที่ = วันที่ = dateformatutil.formatdate (datestr); วันที่ส่งคืน == null? NULL: DateFormatUtil.Transformillisecond (วันที่); } / ** * วันที่สตริงเป็น timestamp * @param datestr * @return * / public Static Integer transformillisecond (String datestr, รูปแบบสตริง) {วันที่ = วันที่ = dateformatutil.formatdate (datestr, รูปแบบ); วันที่ส่งคืน == null? NULL: DateFormatUtil.Transformillisecond (วันที่); } / ** * วันที่สตริงเป็น timestamp * @param datestr * @param รูปแบบเช่น "yyyy-mm-dd" * @return * / public Static Integer transformillisecondbytim (สตริง datestr, string tim) {simpledateformat sdf = ใหม่ วันที่วันที่ = null; ลอง {date = sdf.parse (datestr); } catch (parseexception e) {e.printstacktrace (); } วันที่ส่งคืน == null? NULL: DateFormatUtil.Transformillisecond (วันที่); } / ** * สตริงจนถึงวันที่รูปแบบคือ: "yyyy-mm-dd hh: mm: ss" * @param datestr * @return * / วันที่สาธารณะรูปแบบ datedate (สตริง datestr) {simpledateFormat sdf = ใหม่ simpledateFormat ("yyyy-mm-dd hh: mm: mm: ss" ผลลัพธ์วันที่ = null; ลอง {result = sdf.parse (datestr); } catch (parseexception e) {e.printstacktrace (); } ผลตอบแทนผลลัพธ์; } / ** * สตริงจนถึงวันที่รูปแบบ: "yyyy-mm-dd hh: mm: ss" * @param datestr * @return * / วันที่สาธารณะคงที่รูปแบบ (สตริง datestr, รูปแบบสตริง) {simpledateFormat sdf = new SimpledateFormat (รูปแบบ); ผลลัพธ์วันที่ = null; ลอง {result = sdf.parse (datestr); } catch (parseexception e) {e.printstacktrace (); } ผลตอบแทนผลลัพธ์; } / ** * วันที่สตริง * @param วันที่ * @return * / ฟอร์แมตสตริงคงที่สาธารณะ (วันที่วันที่) {SimpledateFormat SDF = New SimpledateFormat ("YYYY-MM-DD HH: MM: SS"); สตริงผลลัพธ์ = null; ผลลัพธ์ = sdf.format (วันที่); ผลการกลับมา; } / ** * วันที่สตริง * @param วันที่ * @return * / ฟอร์แมตสตริงคงที่สาธารณะ (วันที่วันที่, รูปแบบสตริง) {simpledateFormat sdf = new SimpledateFormat (รูปแบบ); สตริงผลลัพธ์ = null; ผลลัพธ์ = sdf.format (วันที่); ผลการกลับมา; } / ** * เอาท์พุทรูปแบบ Timestamp (สำหรับเทมเพลต httpl) * * @param ms timestamp * @param รูปแบบรูปแบบรูปแบบ * @return * / สตริงคงที่สาธารณะ transfordate (จำนวนเต็ม MS, รูปแบบสตริง) {string str = ""; if (ms! = null) {long msl = (ยาว) ms*1000; SimpledateFormat SDF = ใหม่ SimpleDateFormat (รูปแบบ); if (! ms.equals (0)) {ลอง {str = sdf.format (msl); } catch (exception e) {e.printstacktrace (); }}} return str; } / ** * ใช้จำนวนเต็มหรือทศนิยมของหมายเลขประเภท bigdecimal (สำหรับเทมเพลต httpl) * * @param b ค่า * @param โหมดโหมด 0 รอบ 1 ทศนิยม * @return * / สตริงคงที่สาธารณะ splitbigdecimal (bigdecimal b, int mode) สตริง 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 * / สาธารณะ int caculate2days วันที่ seconddate = dateformatutil.transfordate (TS2); ปฏิทินปฏิทิน = ปฏิทิน GetInstance (); Calendar.Settime (FirstDate); int daynum1 = calendar.get (calendar.day_of_year); Calendar.Settime (วันที่สอง); int daynum2 = calendar.get (calendar.day_of_year); return math.abs (daynum1 - daynum2); }/** * เพิ่มเครื่องหมายดอกจันลงในสี่หลักกลางของโทรศัพท์มือถือ * * @param mobile * @return */public String mobileserect (String mobile) {ถ้า (! stringutils.isblank (มือถือ)) {int ระหว่าง = mobile.length ()/2; mobile = mobile.substring (0, ระหว่าง -2)+"****"+mobile.substring (ระหว่าง+2, mobile.length ()); } return mobile; } / ** * เพิ่มเครื่องหมายดอกจันลงในที่อยู่อีเมล * * @param อีเมล * @return * / public String EmailSerect (อีเมลสตริง) {ถ้า (! stringutils.isblank (อีเมล)) {int length = email.lastindexof (" @"); email = email.substring (0, 2)+"****"+email.substring (ความยาว -2, email.length ()); } ส่งคืนอีเมล; } / ** * เพิ่มข้อมูลประเภท bigdecimal * * @param แหล่งที่มา bigdecimal * @param เป้าหมายใหญ่ bigdecimal * @return * / สาธารณะ bigdecimal sumbigdicimal (แหล่งที่มา bigdecimal, bigdecimal เป้าหมาย) {source = source.add (เป้าหมาย); แหล่งที่มากลับ; } / ** * การเพิ่มข้อมูลประเภท bigdecimal * * @param แหล่งที่มา bigdecimal * @param เป้าหมายใหญ่ bigdecimal * @return * / สาธารณะ bigdecimal sumbigdicimalanddouble (แหล่งที่มาขนาดใหญ่เป้าหมายสองเท่า) source = source.add (new_target); แหล่งที่มากลับ; } / ** * การลบข้อมูลประเภท bigdecimal * * @param แหล่งที่มา bigdecimal * @param เป้าหมาย BigDecimal * @return * / สาธารณะ subbigdicimal สาธารณะ (แหล่งที่มา bigdecimal, เป้าหมายใหญ่) {แหล่งที่มา = แหล่งที่มา แหล่งที่มากลับ; } / *** รับความแตกต่างของเวลาระหว่างเวลาที่เข้ามาและเวลาปัจจุบัน* @return* / สาธารณะคงที่คงที่ Gettimediff (int timestamp) {วันที่ d1 = dateformatutil.transfordate (timestamp); วันที่วันนี้ = วันที่ใหม่ (); if (d1.getTime () <today.getTime ()) {return null; } return (d1.getTime ()-Today.getTime ())/1000; } / *** รับวันแรกของวันแรกของสัปดาห์ที่แน่นอน* @param สัปดาห์ 0 นี้สัปดาห์นี้ 1 สัปดาห์ก่อนหน้า 1 สัปดาห์หน้า* @return* / สตริงคงที่สาธารณะวันหยุดสัปดาห์ (สัปดาห์ int) {ปฏิทิน C1 = ปฏิทิน. getInstance (); int dow = c1.get (calendar.day_of_week); C1.Add (Calendar.date, -dow-7*(Week-1) -5); String d1 = ใหม่ simpledateFormat ("yyyy-mm-dd") รูปแบบ (c1.gettime ()); ส่งคืน d1+"00:00:00"; } / *** เวลาปัจจุบันบวกหนึ่งปี* / สตริงคงที่สาธารณะ addyear (int starttime) {วันที่ FirstDate = DateFormatUtil.Transfordate (เริ่มต้น); ปฏิทินปฏิทิน = ปฏิทิน GetInstance (); Calendar.Settime (FirstDate); Calendar.add (Calendar.year, 1); สตริง d1 = ใหม่ simpledateFormat ("yyyy-mm-dd hh: mm: ss") รูปแบบ (calendar.getTime ()); กลับ D1; } / ** * รับวันสุดท้ายของวันที่แน่นอน * @param Week * @return * / String String สาธารณะ WeekLastday (int สัปดาห์) {Calendar C1 = Calendar.getInstance (); int dow = c1.get (calendar.day_of_week); C1.Add (Calendar.date, -dow-7*(Week-1) +1); String d1 = ใหม่ simpledateFormat ("yyyy-mm-dd") รูปแบบ (c1.gettime ()); ส่งคืน D1+"23:59:59"; } / *** เปรียบเทียบกับเวลาปัจจุบัน* @return* / บูลีนคงที่สาธารณะ Greaterthannow (int timestamp) {วันที่ d1 = dateformatutil.transfordate (timestamp); วันที่วันนี้ = วันที่ใหม่ (); if (d1.getTime ()> = today.getTime ()) {return true; } return false; } / ** * hh: mm: เวลารูปแบบ SS ถูกแปลงเป็นเวลาแสตมป์ของปี 1970-01-01 (นั่นคือถ้ามีเวลาและไม่มีวันที่จะต้องใช้เวลาประทับเวลาเพื่อแสดงเวลา) * @author dingjiacheng * * / transfromtime HH: MM: SS "); } / ** * การประทับเวลาแปลงเป็น hh: mm: เวลารูปแบบ SS (การลบวันที่) * @author dingjiacheng * * / สตริงคงที่สาธารณะ transtotime (เวลา int) {สตริง S = สตริงใหม่ (transfordate1 (เวลา)); สตริง ss [] = s.split (""); คืน SS [1]; } สาธารณะ int transtochuo (String datestring) {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; } โมฆะคงที่สาธารณะหลัก (สตริง [] 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 (วันที่ใหม่ ()); //system.out.println (dateutils.getdate ()); System.out.println (transfromtime ("00:00:01")); System.out.println (transtotime (transfromtime ("15:01:13"))); - ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น