开发者

not able to convert data type

in my customerInfoForm i hav开发者_JAVA技巧e a field String DateOfBirth.

in my Customer class, DateOfBirth is of type DateTime.

in DataBase also column type is DateTime.

When I call customer.setDateOfBirth(customerInfoForm.getDateOfBirth); It shows error .how to convert this?

Please suggest what to do?


You need a DateFormat object to parse the string version into a Date.

DateFormat format = new SimpleDateFormat("yyyyMMdd");
customer.setDateOfBirth(format.parse(customerInfoForm.getDateOfBirth()));

Note, you talk about DateTime which is not the core Java object for dates. If you are using another library (such as joda-time) you will need to use a different formatter.


Have a overloaded method for setDateOfBirth in customer which accepts String and sets as the DateOfBirth as DateTime.


public Date getDate(String dateString, String format) {
                        Date toReturn=null;     
                        try {
            SimpleDateFormat dFormat = new SimpleDateFormat(format);
            toReturn= dFormat.parse(dateString);
            return toReturn;
        } catch (Exception e) {         
            return null;
        }
    }

Use this function to convert your Date in String format to a Date object. make sure you pass the correct format. Eg. of formats ("MM/dd/yy HH:mm a" etc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜