Convert a date and send to the bank

Asked

Viewed 1,512 times

2

The code is all working, the problem is the date, I want to take the date of a jformatedtextfiel ##/##/####### in the case txt_data and send to the bank, I’m in the java basics, I’ve been hooked into this code for days and I searched a lot but I didn’t get a result. Please if anyone can help I appreciate.

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;
    }
    }

}

  • 1

    Questions: does it give any error, if yes, which one? What kind of field Datecadastro in your table?

  • I think this has been answered here countless times.

  • I searched but I couldn’t find it here.

  • the field is of type date,

2 answers

1

If in your textfield the date is in the dd/MM/yyyy format, then you must build a Simpledateformat-like object with this format, like this:

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

Done this, you use the method parse from Simpledateformat to, from an input in String, obtain an object of type Date.

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

From there, it’s record in the database. However, note that your column Datecadastro must be of the type Date (or Datetime, I do not know Mysql), otherwise it may give problem.

Anyway, you don’t need to use the method format of Simpledateformat. This method is for, from an object of type Date, to obtain a formatted String.

  • yes the column is as date, unfortunately generated an error java.text.Parseexception: Unparseable date: "05/08/2015"

  • 05/08/2015 is the value that is in your Textfield? Look at this Ideone I made for you to test: http://ideone.com/wBtE5t. The month is printed with 1 less, because in the Calendar class it starts from 0.

0

Thanks guys, I managed to settle with the help of the colleague Cantoni

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

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

The only thing I inserted to convert was missing

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);
            }
    }
  • Hello @user28748, in this case, you don’t need to answer your own question, since another answer helped you. Here’s how it works: if an answer helped you with your question, accept it as the correct one. This is the signal you give to others that your problem has been solved based on the accepted answer. This helps anyone who has the same or similar doubt in the future.

  • Note that there is nothing wrong with yourself answering your question. But this you only do if another answer does not solve your problem. If the answer has partially resolved, include a comment so that the author of the reply completes it. Thus, the answer can serve more people.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.