无法转换数据类型

时间:2021-11-15 00:50:33

in my customerInfoForm i have a field String DateOfBirth.

在我的customerInfoForm中,我有一个字段String DateOfBirth。

in my Customer class, DateOfBirth is of type DateTime.

在我的Customer类中,DateOfBirth的类型为DateTime。

in DataBase also column type is DateTime.

在DataBase中,列类型也是DateTime。

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

当我打电话给customer.setDateOfBirth(customerInfoForm.getDateOfBirth);它显示错误。如何转换这个?

Please suggest what to do?

请建议做什么?

3 个解决方案

#1


0  

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

您需要一个DateFormat对象来将字符串版本解析为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.

注意,您谈的是DateTime,它不是日期的核心Java对象。如果您正在使用其他库(例如joda-time),则需要使用其他格式化程序。

#2


0  

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

在customer中为setDateOfBirth设置一个重载方法,该方法接受String并将DateOfBirth设置为DateTime。

#3


0  

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)

使用此函数将日期字符串格式转换为Date对象。确保传递正确的格式。例如。格式(“MM / dd / yy HH:mm a”等)

#1


0  

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

您需要一个DateFormat对象来将字符串版本解析为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.

注意,您谈的是DateTime,它不是日期的核心Java对象。如果您正在使用其他库(例如joda-time),则需要使用其他格式化程序。

#2


0  

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

在customer中为setDateOfBirth设置一个重载方法,该方法接受String并将DateOfBirth设置为DateTime。

#3


0  

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)

使用此函数将日期字符串格式转换为Date对象。确保传递正确的格式。例如。格式(“MM / dd / yy HH:mm a”等)