Error java.lang.IllegalArgumentException with Date.valueOf

3

I'm generating this button:

private void btModificarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        if (this.validarDatos()) {
            Paciente pac = new Paciente();
            pac.setnum_asegurado(Integer.parseInt(txtnum_asegurado.getText()));
            pac.setNombre(txtNombre.getText());
            pac.setDireccion(txtDireccion.getText());
            pac.setEdad(Integer.parseInt(txtEdad.getText()));
            pac.setfecha_nacimiento(Date.valueOf(txtfecha_nacimiento.getText()));
            pac.sete_mail(txte_mail.getText());
            pac.setTelefono(Integer.parseInt(txtTelefono.getText()));
            pac.setProfesion(txtProfesion.getText());
            int res = this.pacBo.modificar(pac);
            switch (res) {
                case 0: JOptionPane.showMessageDialog(null, "Paciente Modificado Correctamente.");
                  break;
                case 1: JOptionPane.showMessageDialog(null, "No se pudo conectar a la BD.");
                  break;
                case 2: JOptionPane.showMessageDialog(null, "Problemas Almancenando Datos.");
                  break;  
                case 3: JOptionPane.showMessageDialog(null, "Error Modificando Datos en la BD.");
                  break;  
            }
            llenarTabla();
            limpiar();
        }

But I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
    at java.sql.Date.valueOf(Date.java:143)
    at ventanas.MantPaciente.btModificarActionPerformed(MantPaciente.java:385)
    at ventanas.MantPaciente.access$200(MantPaciente.java:20)
    at ventanas.MantPaciente$4.actionPerformed(MantPaciente.java:182)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)

Someone could clarify why. thank you very much

    
asked by Felix mejias 27.11.2017 в 22:23
source

2 answers

2

The documentation for Date.valueOf (string) specifies that if you do not pass a date in the format yyyy-[m]m-[d]d , then you throw a IllegalArgumentException .

Obviously, txtfecha_nacimiento.getText() does not contain a string in that format.

So to fix your problem, you can 1) make sure that txtfecha_nacimiento.getText() is in the format yyyy-[m]m-[d]d , or 2) you can use SimpleDateFormat to convert the date specifying the format you want.

For example, if the string is in the dd/mm/yyyy format, then you can do the conversion as follows:

pac.setfecha_nacimiento(
    new SimpleDateFormat("dd/MM/yyyy")
        .parse(txtfecha_nacimiento.getText()));
    
answered by 27.11.2017 в 22:43
1

Fix this line: (Date.valueOf (txtdate_naissance.getText ()), use SimpleDateFormat

    
answered by 27.11.2017 в 22:33