It is often necessary to convert a datetime into a string of various formats, or to convert various types of string to a datetime type.
This article provides a conversion tool class for DateTime and string:
Importorg.apache.commons.lang3.StringUtils;ImportOrg.joda.time.DateTime;ImportOrg.joda.time.DateTimeZone;ImportOrg.joda.time.format.DateTimeFormat;ImportOrg.joda.time.format.DateTimeFormatter;Importjava.util.Date;ImportJava.util.TimeZone; Public classDatetimeutils {Private Static FinalDateTimeFormatter fmt_yyyymmddhhmmsssss = Datetimeformat.forpattern ("Yyyy-mm-dd HH:mm:ss. SSS "); Private Static FinalDateTimeFormatter FMT_YYYYMMDDHHMMSS = Datetimeformat.forpattern ("Yyyy-mm-dd HH:mm:ss"); Private Static FinalDateTimeFormatter fmt_yyyymmddhhmm = Datetimeformat.forpattern ("Yyyy-mm-dd hh:mm"); Private Static FinalDateTimeFormatter FMT_YYYYMMDD = Datetimeformat.forpattern ("Yyyy-mm-dd"); Private Static FinalDateTimeFormatter FMT_HHMMSS = Datetimeformat.forpattern ("HH:mm:ss"); Private Static FinalDateTimeFormatter fmt_hhmm = Datetimeformat.forpattern ("hh:mm"); Private Static FinalDateTimeFormatter fmt_yyyymmddhhmmss_nosplit = Datetimeformat.forpattern ("Yyyymmddhhmmss"); Private Static FinalDateTimeFormatter fmt_yyyymmddhhmm_nosplit = Datetimeformat.forpattern ("Yyyymmddhhmm"); Private Static FinalDateTimeFormatter fmt_yyyymmdd_nosplit = Datetimeformat.forpattern ("YyyyMMdd"); Private Static FinalDateTimeFormatter fmt_hhmmss_nosplit = Datetimeformat.forpattern ("Hhmmss"); Private Static FinalDateTimeFormatter fmt_hhmm_nosplit = Datetimeformat.forpattern ("HHmm"); Private Static FinalDateTimeFormatter fmt_yyyymmddhhmm = Datetimeformat.forpattern ("YyyyMMdd hh:mm"); Private Static FinalDatetimezone TimeZone = Datetimezone.fortimezone (Timezone.gettimezone ("Gmt+8")); Public StaticString Fmtyyyymmddhhmmss (DateTime d) {returnFmt_yyyymmddhhmmss.print (D.withzone (TimeZone)); } Public StaticString fmtyyyymmddhhmm (DateTime d) {returnFmt_yyyymmddhhmm.print (D.withzone (TimeZone)); } Public StaticString FMTYYYYMMDD (DateTime d) {returnFmt_yyyymmdd.print (D.withzone (TimeZone)); } Public StaticString Fmthhmmss (DateTime d) {returnFmt_hhmmss.print (D.withzone (TimeZone)); } Public StaticString fmthhmm (DateTime d) {returnFmt_hhmm.print (D.withzone (TimeZone)); } //no separators Public StaticString Fmtyyyymmddhhmmss_nosplit (DateTime d) {returnFmt_yyyymmddhhmmss_nosplit.print (D.withzone (TimeZone)); } Public StaticString Fmtyyyymmddhhmm_nosplit (DateTime d) {returnFmt_yyyymmddhhmm_nosplit.print (D.withzone (TimeZone)); } Public StaticString Fmtyyyymmdd_nosplit (DateTime d) {returnFmt_yyyymmdd_nosplit.print (D.withzone (TimeZone)); } Public StaticString Fmthhmmss_nosplit (DateTime d) {returnFmt_hhmmss_nosplit.print (D.withzone (TimeZone)); } Public StaticString Fmthhmm_nosplit (DateTime d) {returnFmt_hhmm_nosplit.print (D.withzone (TimeZone)); } Public Staticdatetime parseyyyymmddhhmmsssss (String str) {datetime d=fmt_yyyymmddhhmmsssss.parsedatetime (str); returnD; } Public Staticdatetime PARSEYYYYMMDDHHMMSS (String str) {datetime d=fmt_yyyymmddhhmmss.parsedatetime (str); returnD; } Public Staticdatetime parseyyyymmddhhmm (String str) {datetime d=fmt_yyyymmddhhmm.parsedatetime (str); returnD; } Public Staticdatetime parseyyyymmddhhmm (String str) {datetime d=fmt_yyyymmddhhmm.parsedatetime (str); returnD; } Public Staticdatetime PARSEYYYYMMDD (String str) {datetime d=fmt_yyyymmdd.parsedatetime (str); returnD; } Public Staticdatetime PARSEHHMMSS (String str) {datetime d=fmt_hhmmss.parsedatetime (str); returnD; } Public Staticdatetime parsehhmm (String str) {datetime d=fmt_hhmm.parsedatetime (str); returnD; } Public Staticdatetime parseyyyymmddhhmmss_nosplit (String str) {datetime d=fmt_yyyymmddhhmmss_nosplit.parsedatetime (str); returnD; } Public Staticdatetime parseyyyymmddhhmm_nosplit (String str) {datetime d=fmt_yyyymmddhhmm_nosplit.parsedatetime (str); returnD; } Public Staticdatetime parseyyyymmdd_nosplit (String str) {datetime d=fmt_yyyymmdd_nosplit.parsedatetime (str); returnD; } Public Staticdatetime parsehhmmss_nosplit (String str) {datetime d=fmt_hhmmss_nosplit.parsedatetime (str); returnD; } Public Staticdatetime parsehhmm_nosplit (String str) {datetime d=fmt_hhmm_nosplit.parsedatetime (str); returnD; } /*** Determine if the date being tested is within two given date range. * * @paramtestdate Date Tested *@paramstartdate Start Date *@paramendDate End Date *@return */ Public Static Booleanisindaterange (DateTime testdate, datetime startdate, datetime endDate) {if(TestDate = =NULL) { return false; } if(StartDate = =NULL&& EndDate = =NULL) { return true; } if(StartDate! =NULL&& EndDate = =NULL) { return(Testdate.compareto (StartDate) >= 0); } if(StartDate = =NULL&& EndDate! =NULL) { return(Testdate.compareto (endDate) <= 0); } if(StartDate! =NULL&& EndDate! =NULL) { return((Testdate.compareto (StartDate) >= 0) && (Testdate.compareto (endDate) <= 0)); } return false; } Public StaticDateTime plusseconds (date date,intseconds) { returntodatetime (date). Plusseconds (seconds); } Public StaticDateTime todatetime (date date) {if(Date = =NULL) { return NULL; } return NewDateTime (). Withmillis (Date.gettime ()); } Public Staticstring format2string (date date, string pattern) {if(Date = =NULL||Stringutils.isempty (pattern)) { return NULL; } datetime datetime=Formatdate2dt (date); returnformat2string (datetime, pattern); } Public StaticDateTime formatdate2dt (date date) {if(Date = =NULL) { return NULL; } returnDateTime.Now (). Withmillis (Date.gettime ()); } Public Staticstring format2string (DateTime time, String pattern) {if(Time = =NULL||Stringutils.isempty (pattern)) { return NULL; } returntime.tostring (pattern); }}
View Code
Chapter II DateTime Tool classes