To convert a string to a date type by using the Beanutils setting property

Source: Internet
Author: User

1 when setting properties using Beanutils, the String,int can be converted automatically. such as the following example

Define a person class first

Package pkg6;import Java.util.date;import Java.text.SimpleDateFormat; Public classPerson {PrivateString name; PrivateString Gender; Private intAge ; PrivateDate birthday;  PublicPerson () {} PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getgender () {returngender; }     Public voidSetgender (String gender) { This. Gender =gender; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; } @Override PublicString toString () {return ' {name: ' +name+ ' Age: ' +age+ ' Birthday: ' +birthday+ '} ';    }}

Use a function to test

Package Pkg6;import Java.lang.reflect.invocationtargetexception;import org.apache.commons.beanutils.BeanUtils ; Public classDemo1 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubperson P1 =NewPerson (); String name="Eric"; String Gender="M"; intAge = A; String Birthday="1991-32-56"; Try{Beanutils.setproperty (P1,"name","Eric"); //Beanutils.setproperty (P1, "Birthday", birthday);                    } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println (p1); }}

Its output is

{Name:eric age:0 Birthday:null}

No problem. In the Web development process often encounter Web page submission user data is a string, the data stored in the database is a date, then directly set up what happens, as follows

After the code that sets the property, add

Beanutils.setproperty (P1, "Birthday", birthday);

Operation results will be error-

Org.apache.commons.beanutils.ConversionException:String must bes in JDBC format [YYYY-MM-DD] to create a java.sql.Date
At Org.apache.commons.beanutils.converters.DateTimeConverter.toDate (datetimeconverter.java:436)
At Org.apache.commons.beanutils.converters.DateTimeConverter.convertToType (datetimeconverter.java:343)
At Org.apache.commons.beanutils.converters.AbstractConverter.convert (abstractconverter.java:156) {name:eric age:0 Birthday:null}

This is because Beanutils does not support the conversion of strings to other object types. To convert, you need to register a converter.

Convertutils.register (NewConverter () { PublicObject Convert (Class type, Object value) {SimpleDateFormat simpleDate Format=NewSimpleDateFormat ("YYYY-MM-DD"); Try {                    returnSimpledateformat.parse (value.tostring ()); } Catch(ParseException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                return NULL; }}, Date.class);

When the Java.util.Date type is encountered, the converter is used to convert the string to the date type.

The complete code is as follows:

Package Pkg6;import Java.lang.reflect.invocationtargetexception;import java.text.parseexception;import Java.text.simpledateformat;import Java.util.date;import Org.apache.commons.beanutils.beanutils;import Org.apache.commons.beanutils.convertutils;import Org.apache.commons.beanutils.Converter; Public classDemo1 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubperson P1 =NewPerson (); String name="Eric"; String Gender="M"; intAge = A; String Birthday="1991-32-56"; Convertutils.register (NewConverter () { PublicObject Convert (Class type, Object value) {SimpleDateFormat simpleDate Format=NewSimpleDateFormat ("YYYY-MM-DD"); Try {                    returnSimpledateformat.parse (value.tostring ()); } Catch(ParseException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                return NULL; }}, Date.class); Try{Beanutils.setproperty (P1,"name","Eric"); Beanutils.setproperty (P1,"Birthday", birthday); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println (p1); }}

The results of the operation are as follows:

{name:eric age:0 Birthday:sat Sep 00:00:00 CST 1993}

To convert a string to a date type by using the Beanutils setting property

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.