-2

Hello I am trying to store the birthdate of the user in database with the code below:

 private void btnActionPerformed(java.awt.event.ActionEvent evt) {                                                 

    String username = txtUserName.getText();
    String password = txtPassword.getText();
    String email = txtEmail.getText();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String birthdate = sdf.format(JDateChooser.getDate());      
    Users user = new Users();
    user.setUserName(cin);
    user.setPassWord(firstName);
    user.setEmail(email);
    user.setBirthDate(birthdate);
    try {        
       int count = Users.getInstance().insert(user);
       if(count == 1){
       JOptionPane.showMessageDialog(null,"success");
       reset();
       }else{
       JOptionPane.showMessageDialog(null,"Faild");
       }
    } catch (Exception ex) {
        Logger.getLogger(AddNewPatient.class.getName()).log(Level.SEVERE, null, ex);
    }
} 

I got an error which says String connot be converted to Date in the line "user.setBirthDate(birthdate);" Because the parameter birthdate is assigned as Date type in the encapsulation(setBirthDate) is there any way to solve this issue, I am new in java programming and I am trying to improve my skills in java.

David
  • 208,112
  • 36
  • 198
  • 279
  • 2
    Well currently you're *formatting* a `Date` to a string, then trying to use it as if it were a `Date`... can't you just use the result of `JDateChooser.getDate()` without formatting it as text at all? (Note that I'd recommend using `java.time.LocalDate` and avoiding `java.util.Date` entirely, but that's a slightly separate matter.) – Jon Skeet Jan 06 '21 at 16:45

2 Answers2

1

If this returns a Date:

JDateChooser.getDate()

And what you need is a Date, then don't convert it to a String. Just keep it as a Date:

Date birthdate = JDateChooser.getDate();
// later...
user.setBirthDate(birthdate);

Note that you can then also remove this line, since you're not using the variable it declares:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

In general you want to keep data types in their raw form pretty much as often as possible. Unless there's a specific need for something to be represented as a string (displaying it to the user, sending it over a serialized API of some kind, etc.) then just use the data as-is instead of converting it to something else.

David
  • 208,112
  • 36
  • 198
  • 279
  • Hi @David I tried this before "java.util.Date birthdate = JDateChooser.getDate();" and I got this error: java.util.Date cannot be converted to java.sql.Date, the forma of the date should be stored as java.sql.Date forma database if I am not wrong ? –  Jan 06 '21 at 17:39
  • @Anass: Types are important. The error is referring to two different types called `Date`. What specific type does `JDateChooser.getDate()` return? What specific type does `user.setBirthDate()` expect? If you need to convert between the two types, [there's a duplicate question for that](https://stackoverflow.com/q/530012/328193). – David Jan 06 '21 at 17:49
  • JDateChooser.getDate() retun a Date format "yyyy-MM-dd" from JDateChooser field and user.setBirthDate() expect the same type, it works with JtxtFileds but with JDateChooser doesn't work –  Jan 06 '21 at 18:09
  • @Anass: It's important to be clear and specific. Does `JDateChooser.getDate()` return a `Date` object of some kind (which doesn't have a "format"), or does it return a `String` representing the `Date` in a specific format? The code/error implies the former, but your comment implies the latter. Also, if `JDateChooser.getDate()` returns the same type that `user.setBirthDate()` expects then the code in this answer would solve the error in the question. The other error you mention in a previous comment would be somewhere else. You have to be very specific about these things in order to correct it. – David Jan 06 '21 at 18:12
  • it return a string representing the Date in a specific format, the user has to choose its birthday through JDatechooser or typing it manualy I have updated the code in top and I included all the file can you take a look on it please –  Jan 06 '21 at 19:20
  • @Anass: If `JDateChooser.getDate()` is returning a `String` (I don't think it is, but in the scope of this question you indicate that *in your case* it is) then you still don't need the `SimpleDateFormat` object at all, since there's no reason to format a string as a string. But in that case if what you have is a `String` and `user.setBirthDate()` requires a `Date` then you need to create a new `Date` object to send to `user.setBirthDate()`. Depending on the construction of that `Date` type you may be able to pass it a formatted string or parse out the individual values from the string. – David Jan 06 '21 at 19:23
  • @Anass: As an aside... While you're encouraged to add information to the question where needed, please don't completely replace the question with an entirely new one that's unrelated to the original. This invalidates existing answers and causes confusion to future readers. If there's a new problem in another part of the code, that could merit a new question to be posted separate from this one. This question is already asking about a specific error from a specific code segment. – David Jan 06 '21 at 19:24
  • the code I updated it was my first instruction and when I got the error I tried to pass the date format as string (the code I posted first) because it worked with the JTextField when I enter the date manually but not with JDateChooser, I will try with formatted string and parseto, I am newbie in java programmaming and still learning haha thank you bro –  Jan 06 '21 at 19:35
1

After you get the date with JDateChooser.getDate(), you are immediately converting it to a string: sdf.format(JDateChooser.getDate());

You should store the returned Date from JDateChooser.getDate() as an actual Date object.

Date birthdate = JDateChooser.getDate();

Then you can use it in your other function directly:

user.setBirthDate(birthdate);

If you do need the date as a string for some other purpose (perhaps display to the user), you can store a formatted string version in a different variable:

String birthdateString = sdf.format(birthdate);   

Otherwise, if you don't need a string version, you can delete the line where you create sdf.

Luke B
  • 2,075
  • 2
  • 18
  • 26
  • Hi Baked Potato I tried this and I got the same error in Preparedstatement "ps.setDate(7, u.getBirthDate());' –  Jan 06 '21 at 17:53