"Incorrect datetime" value in MySQL

7
Hello, I'm trying to insert dates in dd/mm/Y format into two jTextField , in MYSQL, however I get an error message only for the first field:

Incorrect datetime value: '01/01/2016' for function str_to_date

The java code is:

if (novoCadastro) {
                //Cria a String para inserir os dados
                query = "INSERT INTO Clientes(Nome,Endereço,Cidade,CPF,TelefoneCliente,EmailCliente,Produto,UltimaCompra,ClienteDesde) VALUES(?,?,?,?,?,?,?,DATE_FORMAT(STR_TO_DATE(?, '%dd/%mm/%Y'),'%d-%m-%Y'),DATE_FORMAT(STR_TO_DATE(?,'%dd/%mm/%Y'),'%d-%m-%Y'))";
            //Seta os valores na String de inserção
           System.out.println(txtUltComp.getText());
           System.out.println(txtClienDesd.getText());
            stmt = con.prepareStatement(query);
            stmt.setString(1, txtNome.getText());
            stmt.setString(2, txtEndereco.getText());
            stmt.setString(3, txtCidade.getText());
            stmt.setString(4, txtCPF.getText());
            stmt.setString(5, txtTelefone.getText());
            stmt.setString(6, txtEmail.getText());
            stmt.setString(7, txtProduto.getText());
            stmt.setString(8, txtUltComp.getText());
            stmt.setString(9, txtClienDesd.getText());
            JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso! ");
    
asked by anonymous 25.11.2016 / 11:18

1 answer

6

As per the documentation for formatting of dates, a %d already represents the two digits of the day of the month. Similarly, %m already represents the two digits referring to the month itself.

In this way, instead of

STR_TO_DATE(?, '%dd/%mm/%Y')

do

STR_TO_DATE(?, '%d/%m/%Y')
    
25.11.2016 / 11:39