ayoubitou
ayoubitou

Reputation: 35

convert string to date java error "incompatible types"

I success to get my date from JDateChooser into a string with this line:

String d1  = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();

I want to know if there is any method to get a date from a JDateChooser ?

I tried to convert this string into a date , and i have an error in the third line .

"incompatible types : java.util.date cannot be converted to java.sql.Date "

    String s = "2011-07-08";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(s);

i have to import the java.util.Date , But i have another error in the same line

unreported exception ParseException ; must be caught or declared to be thrown

Upvotes: 0

Views: 18181

Answers (2)

Leo
Leo

Reputation: 6570

try

String s = "2011-07-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse(s);

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

You've imported

java.sql.Date

instead of

java.util.Date

Change the import statement to the appropriate type, ie. java.util.Date.

Upvotes: 3

Related Questions