تشترك هذه المقالة في فئة أدوات DateUtils لتحويل الوقت للرجوع إليها. المحتوى المحدد كما يلي
استيراد java.sql.timestamp ؛ استيراد java.text.simpledateformat ؛ استيراد java.util.calendar ؛ استيراد java.util.date ؛ استيراد java.util.hashmap WUL * 2015-12-31 */public class dateutil {public static final string date_normal_format = "yyyy-mm-dd" ؛ Static Static Final Final DateTime_Normal_Format = "YyyyM-MM-DD HH: MM: SS" ؛ السلسلة النهائية الثابتة العامة Date_compact_format = "Yyyymmdd" ؛ السلسلة النهائية الثابتة العامة DateTime_Compact_Format = "YyyMMDDHHMMSS" ؛ السلسلة النهائية الثابتة العامة ym_normal_format = "yyyy-mm" ؛ السلسلة النهائية الثابتة العامة YM_COMPACT_FORMAT = "Yyyymm" ؛ / ** * سلسلة إلى Timestamp * param datestr * regurn * author wul * 2016-17 */ public static timestamp stringtotimestamp (سلسلة datest) {try {if (datest.length () <= 10) {datest += "00:00:00" ؛ } إرجاع timestamp.valueof (dateSt) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ العودة لاغية. }} / ** * سلسلة حتى الآن * param datestr * param format * return * author wul * 2016-17 * / public static date stringtodate (سلسلة datestr ، تنسيق السلسلة) {if (dateStr == null || } تاريخ التاريخ = فارغ ؛ // لاحظ أن تنسيق التنسيق يجب أن يتطابق مع تنسيق سلسلة التاريخ SimpledAteFormat SDF = جديد SimpleDateFormat (التنسيق) ؛ حاول {date = sdf.parse (dateSt) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ } تاريخ العودة ؛ } / ** * تاريخ السلسلة * param تاريخ * param format * return * author wul * 2016-17 * / sTatic Static State DateToString (تاريخ التاريخ ، تنسيق السلسلة) {simpledateformat sdf = new simpledateFormat (format) ؛ String CurrentDate = sdf.format (date) ؛ إرجاع CurrentDate ؛ } / ** * تاريخ الطابع الزمني * param التاريخ * return * Author WUL * 2016-1-17 * / public static timestamp datetotimestamp (date date) {timestamp ts = new timestamp (date.gettime ()) ؛ إرجاع TS ؛ } / ** * timestamp إلى سلسلة * param ts * return * author wul * 2016-1-17 * / static string public timestamptoString (timestamp ts) {String tsstr = null ؛ SimplEdateFormat SDF = جديد spileDateFormat (dateTime_NorMal_Format) ؛ حاول {tsstr = sdf.format (ts) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ } إرجاع tsstr ؛ } / ** * timestamp حتى الآن * param ts * return * author wul * 2016-1-17 * / public static date timestamptodate (timestamp ts) {return ts ؛ } / ** * احصل على الوقت الحالي والتنسيق: yyyy-mm-dd hh: mm: ss * regurn * / string static public getCurrentTimenormal () {simpledateformat sdf = new SimpleDateFormat (datetime_normal_format) ؛ String CurrentDate = sdf.format (Date ()) ؛ إرجاع CurrentDate ؛ } / ** * احصل على الوقت الحالي والتنسيق: yyyymmddhhmmss * regurn * / سلسلة ثابتة getCurrentTimeCompact () {simpledateformat sdf = new SimpleTformat (datetime_compact_format) ؛ String CurrentDate = sdf.format (Date ()) ؛ إرجاع CurrentDate ؛ } / ** * احصل على الوقت الحالي والتنسيق: yyyy-mm-dd * regurn * / سلسلة ثابتة getCurrentDateNormal () {simpledateformat sdf = new SimplEdateFormat (date_normal_format) ؛ String CurrentDate = sdf.format (Date ()) ؛ إرجاع CurrentDate ؛ } / ** * احصل على الوقت الحالي والتنسيق: yyyymmdd * regurn * / سلسلة ثابتة عامة getCurrentDateCompact () {simpledateformat sdf = new SimpledAteFormat (date_compact_format) ؛ String CurrentDate = sdf.format (Date ()) ؛ إرجاع CurrentDate ؛ } / ** * format 20101202 time to 2010-12-02 * * param time time format: yyyymmdd * regurn * / string static public getDateCompActtonormal (سلسلة dateString) {StringBuilder sb = new StringBuilder () ؛ sb.append (dateString.SubString (0،4)). إلحاق ("-"). إلحاق (dateString.Subsequence (4 ، 6)). إلحاق ("-"). إرجاع sb.tostring () ؛ } / ** * Format 20101202101423 to to 2010-12-02 10:14:23 * * param time time format: yyyymmddhhmmss * @return * / public static string getDateTimeCompActtonormal (سلسلة) sb.append (datestring.substring (0،4)) 10)). إلحاق (":") إرجاع sb.tostring () ؛ } / ** * قم بتحويل إدخال الوقت في الواجهة إلى سلسلة زمنية متقطعة * 2010-12-02 10:14:23 تم تنسيق الوقت كـ 20101202101423 * @param datenormalstr string * return string * / public static string getcompactstring (سلسلة datenormstr) حاول {ret.append (datenormalstr.substring (0 ، 4)) ؛ ret.append (datenormalstr.substring (5 ، 7)) ؛ ret.append (datenormalstr.substring (8 ، 10)) ؛ ret.append (datenormalstr.substring (11 ، 13)) ؛ ret.append (datenormalstr.substring (14 ، 16)) ؛ ret.append (datenormalstr.substring (17 ، 19)) ؛ } catch (استثناء ex) {// إذا لم تكن السلسلة طويلة بما يكفي ، فأرجع الجزء السابق} return ret.toString () ؛ } / ** * احصل على السنة في 20101202 (101423) تنسيق الوقت * param time time format: yyyymmdd (hhmmss) * @return * / public static string getyear (String dateString) {return datestring.substring (0،4) ؛ } / ** * احصل على الشهر مع تنسيق الوقت 201011202 (101423) * param تنسيق وقت البيانات: yyyymmdd (hhmmss) * @return * / public static string getMonth (String dateString) {return datestring.substring (4،6) ؛ } / ** * احصل على التاريخ مع تنسيق الوقت 20101202 * param تنسيق وقت البيانات: yyyymmdd * return * / سلسلة ثابتة getDayNotime (سلسلة dateString) {return datestring.substring (6) ؛ } / ** * احصل على التاريخ قبل التاريخ الحالي وادفع إلى الأمام بعدد الأيام * param numVal * param dateFormat * regurn * author wul * 2016-17 * / static string getBeforedatePlusday (int numval ، سلسلة dateFormat) {calendar calendar = calendar.getinstance () ؛ currentTimeMillis long = calendar.getTimeInmillis () ؛ hournmillis طويلة = 60 * 60 * 1000 ؛ long dval = numVal * 24 * hourinmillis ؛ SimplEdateFormat SDF = جديد spiStFormat (DateFormat) ؛ String CurrentDate = sdf.format (CurrentTimeMillis - Dval) ؛ إرجاع CurrentDate ؛ } / ** * احصل على التاريخ قبل التاريخ الحالي وادفع إلى الأمام بعدد الأيام * param numVal * param dateFormat * regurn * author wul * 2016-17 * / static string getAfterDatePlusday (int numval ، سلسلة dateFormat) {calendar calendar = calendar.getinstance () ؛ currentTimeMillis long = calendar.getTimeInmillis () ؛ hournmillis طويلة = 60 * 60 * 1000 ؛ long dval = numVal * 24 * hourinmillis ؛ SimplEdateFormat SDF = جديد spiStFormat (DateFormat) ؛ String CurrentDate = sdf.format (CurrentTimeMillis + Dval) ؛ إرجاع CurrentDate ؛ } / ** * احصل على التاريخ قبل التاريخ الحالي ودفع إلى الأمام حسب الساعة * param numVal * param dateFormat * @REGANTURN * Author WUL * 2016-1-17 * / Static String public getBeedTeLtPlushour (int numval ، سلسلة dateFormat) {calendar calendar = calendar.getInstance () ؛ currentTimeMillis long = calendar.getTimeInmillis () ؛ hournmillis طويلة = 60 * 60 * 1000 ؛ long dval = numVal * hourinmillis ؛ SimplEdateFormat SDF = جديد spiStFormat (DateFormat) ؛ String CurrentDate = sdf.format (CurrentTimeMillis - Dval) ؛ إرجاع CurrentDate ؛ } / ** * احصل على التاريخ قبل التاريخ الحالي ودفع إلى الأمام حسب الساعة * param numVal * param dateFormat * @return * author wul * 2016-1-17 * / سلسلة ثابتة عامة getAfterDatePlushour (int numval ، سلسلة dateFormat) {calendar calendar = calendar.getInstance () ؛ currentTimeMillis long = calendar.getTimeInmillis () ؛ hournmillis طويلة = 60 * 60 * 1000 ؛ long dval = numVal * hourinmillis ؛ SimplEdateFormat SDF = جديد spiStFormat (DateFormat) ؛ String CurrentDate = sdf.format (CurrentTimeMillis + Dval) ؛ إرجاع CurrentDate ؛ } / ** * عدد الأيام المختلفة بين تواريخين * param begindate * param enddate * return * author wul * 2016-1-18 * static int static intenden (date beginate ، date enddate) {calendar cal = calendar.getinstance () ؛ Cal.Settime (begindate) ؛ time 1 = cal.getTimeInMillis () ؛ Cal.Settime (enddate) ؛ الوقت long2 = cal.getTimeInMillis () ؛ طويل بين_days = (time2 - time1) / (1000 * 3600 * 24) ؛ إرجاع integer.parseint (string.valueof (inter_days)) ؛ } / ** * احصل على عدد الأيام في شهر معين * param السنة * param month * return * author wul * 2016-1-18 * / public static intmonthdays (int year ، int month) {calendar cal = calendar.getinstance () ؛ Cal.Set (Calendar.year ، year) ؛ Cal.Set (Calendar.month ، month - 1) ؛ return cal.getActualMaximum (calendar.date) ؛ } / ** * إضافة أو طرح السنة إلى الوقت * param date * param plustime * regurn * author wul * 2016-1-18 / date static date getDatePlusyear (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getinstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.year ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * أضف أو طرح شهر الوقت * param Date * param plustime * regurn * author wul * 2016-1-18 * / تاريخ ثابت عام getDatePlusmonth (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getinstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.month ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * أضف وطرح عدد الأيام إلى الوقت * param Date * param plustime * regurn * author wul * 2016-1-18 * / date static public getDatePlusDay (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getinstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.Date ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * إضافة أو طرح الوقت * param تاريخ * param plustime * return * author wul * 2016-1-18 * / تاريخ ثابت عام getDatePlushour (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getInstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.hour ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * إضافة أو طرح الوقت * param تاريخ * param plustime * return * author wul * 2016-1-18 * / تاريخ ثابت عام getDatePlusMinute (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getinstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.minute ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * إضافة وطرح ثواني إلى الوقت * param التاريخ * param plustime * regurn * author wul * 2016-1-18 / date static public getDatePlusSecond (تاريخ التاريخ ، int plustime) {calendar cal = calendar.getinstance () ؛ Cal.Settime (التاريخ) ؛ Cal.Add (Calendar.Second ، plustime) ؛ التاريخ d = cal.getTime () ؛ العودة د ؛ } / ** * إرجاع السنة الحالية * return * author wul * 2016-1-18 * / static int getCurrentyear () {calendar calendar = calendar.getInstance () ؛ إرجاع calendar.get (1) ؛ } / ** * إرجاع الشهر الحالي * return * Author wul * 2016-1-18 * / static int getCurrentMonth () {calendar calendar = calendar.getInstance () ؛ إرجاع Calendar.get (2) + 1 ؛ } / ** * إرجاع اليوم الحالي * return * author wul * 2016-1-18 * / static int getCurrentDay () {calendar calendar = calendar.getInstance () ؛ إرجاع Calendar.get (5) ؛ } / ** * إرجاع الساعة الحالية * return * author wul * 2016-1-18 * / static static int getCurrentHour () {calendar calendar = calendar.getInstance () ؛ إرجاع Calendar.get (11) ؛ } / ** * إرجاع الدقيقة الحالية * regurn * author wul * 2016-18 * / static int getCurrentMinute () {calendar calendar = calendar.getInstance () ؛ إرجاع Calendar.get (12) ؛ } / ** * إرجاع الثانية الحالية * regurn * author wul * 2016-1-18 * / static int getCurrentSecond () {calendar calendar = calendar.getInstance () ؛ إرجاع Calendar.get (13) ؛ } / ** * إرجاع السنة الحالية * return * Author wul * 2016-1-18 * / public static int getyear (تاريخ التاريخ) {calendar calendar = calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع calendar.get (1) ؛ } / ** * إرجاع الشهر الحالي * return * author wul * 2016-1-18 * / public static int getMonth (Date Date) {Calendar Calendar = calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع Calendar.get (2) + 1 ؛ } / ** * إرجاع اليوم الحالي * return * author wul * 2016-1-18 * / public static int getDay (date date) {calendar calendar = calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع Calendar.get (5) ؛ } / ** * إرجاع الساعة الحالية * return * author wul * 2016-1-18 * / public static int gethour (date date) {calendar calendar = calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع Calendar.get (11) ؛ } / ** * إرجاع الدقيقة الحالية * regurn * author wul * 2016-1-18 * / public static int getMinute (Date Date) {Calendar Calendar = calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع Calendar.get (12) ؛ } / ** * إرجاع الثاني الحالي * @REGRENT * Author wul * 2016-18 * / public static int getCond (Date Date) {Calendar Calendar = Calendar.getInstance () ؛ Calendar.Settime (التاريخ) ؛ إرجاع Calendar.get (13) ؛ } public static void main (string [] args) {system.out.println (dateutil.datetoString (new java.sql.date (system.currentTimeMillis ()) ، dateutil.date_normal_format)) ؛ خريطة <string ، object> map = new hashmap <string ، Object> () ؛ map.put ("Date" ، New Date ()) ؛ String json = jsonobject.fromObject (map) .ToString () ؛ System.out.println (JSON) ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.