Convert a date and send to the bank

2

The code is all working, the problem is the date, I want to get the date of a jformatedtextfiel ## / ## / #### in the txt_data case and send it to the bank, I'm in the basics of java, I'm hooked on this code there are days and I researched a lot but it did not reach a result. Please if anyone can help, thank you.

con_cliente.conecta();
    Date data;
    SimpleDateFormat in = new SimpleDateFormat("yyyy-mm-dd");//do sistema para o banco
    String dataformatada = in.format(txt_data.getText());
        try{
            data = in.parse(dataformatada);
            String gry = "insert into cadastrodeprocesso (Datadecadastro, Cliente, Classificacao, Usuario) values ('"+
                            data.getDate()+"','"+
                            txt_nome.getText()+"','"+
                            cb_classificacao.getSelectedItem()+"','"+
                            txt_usuario.getText()+"')";
                            con_cliente.exeQuery(gry);
                 JOptionPane.showMessageDialog(null,"Gravado com sucesso!");   
            } catch(Exception add){
                     JOptionPane.showMessageDialog(null,"Falha ao gravar o registro " +add);
            }
    }//area para impossibilitar duplicar cadastros de empresas de mesmo nome
    else{
    JOptionPane.showMessageDialog(null, "Clique em novo para inserir um novo registro");
    return;
    }
    }

}

    
asked by anonymous 05.08.2015 / 17:37

2 answers

1

If in your textfield the date is in the format dd / MM / yyyy, then you should construct an object of type SimpleDateFormat with this format, like this:

SimpleDateFormat in = new SimpleDateFormat("dd/MM/yyyy")

When you do this, you use the parse method of SimpleDateFormat to get an object of type Date from an input in String.

Date data = in.parse(txt_data.getText());

From there, it is written to the database. However, note that your Datadecadastro column should be of type Date (or DateTime, I do not know MySQL), otherwise it might be a problem.

Anyway, you do not need to use the format method of SimpleDateFormat. This method is to get a formatted String from a Date object.

    
05.08.2015 / 17:52
0

Thank you guys, I was able to solve it with the help of colleague cantoni

SimpleDateFormat in = new SimpleDateFormat("dd/MM/yyyy");

Date data = in.parse(txt_data.getText());

The only thing I inserted to convert that was missing was

String str = txt_data.getText();

and then I gave an insert and live

con_cliente.conecta();
        try{
            SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");  
            String str = txt_data.getText();  
            Date data = formatador.parse(str);
            String gry = "insert into cadastrodeprocesso (Datadecadastro, Cliente, Classificacao, Usuario) values ('"+
                            new java.sql.Date(data.getTime())+"','"+
                            txt_nome.getText()+"','"+
                            cb_classificacao.getSelectedItem()+"','"+
                            txt_usuario.getText()+"')";
                            con_cliente.exeQuery(gry);
                 JOptionPane.showMessageDialog(null,"Gravado com sucesso!");   
            } catch(Exception add){
                     JOptionPane.showMessageDialog(null,"Falha ao gravar o registro " +add);
            }
    }
    
05.08.2015 / 20:25