Use servlet to create a date formatter to format all dates in the project

Source: Internet
Author: User

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.
*/

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.