Time-size comparisons and the conversion of string types to date classes are very common in development, and the following are primarily a tool approach
Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString stime = "2015-07-13"; String Ftime= "2015-07-15"; System.out.println (Comparedate (Stringtodate (Ftime), Stringtodate (stime))); } Public StaticDate stringtodate (String datestring) {returnStringtodate (datestring, "Yyyy-mm-dd"); } Public StaticDate stringtodate (String datetext, string format) {DateFormat df=NULL; Try { if(Format = =NULL) {DF=NewSimpleDateFormat (); } Else{DF=NewSimpleDateFormat (format); } df.setlenient (false); returnDf.parse (Datetext); } Catch(ParseException e) {return NULL; } } /*** Time judgment *@paramFirsttime *@paramSecondtime *@returnThe first time is before the second time-1; The first time equals the second time 0; the first time is late with the second time 1*/ Public Static intcomparedate (Date firsttime, date secondtime) {LongLfirsttime =Firsttime.gettime (); LongLsecondtime =Secondtime.gettime (); if(Lfirsttime <lsecondtime) { return-1; }Else if(Lfirsttime >lsecondtime) { return1; }Else{ return0; } }}
Comparison of Java implementation time