Import Java. Text. parseexception;
Import Java. Text. simpledateformat;
Import Java. util. RegEx. pattern;
ImportOrg. Apache. commons. beanutils. converter;
Public ClassUtildateconverterImplementsConverter {
Private Simpledateformat ymdhmssss = New Simpledateformat (
" Yyyy-mm-dd hh: mm: Ss. Sss " );
Private Simpledateformat ymdhms = New Simpledateformat (
" Yyyy-mm-dd hh: mm: SS " );
Private Simpledateformat ymd = New Simpledateformat ( " Yyyy-mm-dd " );
Public Object convert (class arg1, object arg2 ){
If ( Null ! = Arg2 && Arg2 Instanceof String ){
Try {
String datestr = (String) arg2;
If (Pattern. Matches ( " \ D {4}-\ D {1}-\ D {1, 2} \ s \ D {1, 2 }:\\ d {1, 2 }: \ D {1, 2 }. \ D {1, 3} " , Datestr ))
Return Ymdhmssss. parse (datestr );
Else If (Pattern. Matches ( " \ D {4}-\ D {1}-\ D {1, 2} \ s \ D {1, 2 }:\\ d {1, 2 }: \ D {1, 2} " , Datestr ))
Return Ymdhms. parse (datestr );
Else If (Pattern. Matches ( " \ D {4}-\ D {1}-\ D {1, 2} \ s \ D {1, 2 }:\\ d {1, 2} " , Datestr ))
Return Ymdhms. parse (datestr + " : 00 " );
Else If (Pattern. Matches ( " \ D {4}-\ D {1}-\ D {1, 2} \ s \ D {1, 2} " , Datestr ))
Return Ymdhms. parse (datestr + " : 00: 00 " );
Else If (Pattern. Matches ( " \ D {4}-\ D {1, 2}-\ D {1, 2} " , Datestr ))
Return Ymd. parse (datestr );
Else If (Pattern. Matches ( " \ D {4}-\ D {1, 2} " , Datestr ))
Return Ymd. parse (datestr + " -01 " );
} Catch (Parseexception e ){
E. printstacktrace ();
Return Null ;
}
}
Return Null ;
}
/*
* The usage of this class is as follows: Create a Servlet and use only one Init () method in the servlet.
* Public void Init (){
* Convertutils. Register (New utildateconverter (), java. util. Date. calss );
*}
* Then register this servlet class in Web. XML, similar Code As follows:
* <Servlet>
* <Servlet-Name> utildateconverterregister </servlet-Name>
* <Servlet-class> package name. utildateconverterregister </servlet-class>
* <Load-on-startup> 2 </load-on-startup>
* </Servlet>
* Note: this servlet does not need to write the <servlet-mapping> information.
*/
}