Add days to a jDateChooser

Asked

Viewed 630 times

1

I would like to add 45 days on my date2, from the date1 Example:

data2 = data1+45

 public void adiciona(Usuario usuario){


    String sql = "INSERT INTO usuario(nome,endereco,cpf,email,telefone,data1,data2) VALUES(?,?,?,?,?,?,?)";
    try {

       PreparedStatement stmt = connection.prepareStatement(sql);
       java.sql.Date data1 = new java.sql.Date(usuario.getData1().getTime()); 


       stmt.setString(1, usuario.getNome());
       stmt.setString(2, usuario.getEndereco());
       stmt.setString(3, usuario.getCpf());           
       stmt.setString(4, usuario.getEmail());
       stmt.setString(5, usuario.getTelefone());
       stmt.setDate(6, data1);
       stmt.setDate(7, data2 );  //QUERO ADICIONAR A DATA2 NO BANCO COM 45 DIAS APÓS A DATA1, COMO FAZER?

       stmt.execute();
       stmt.close();

    } catch (SQLException u) {
        throw new RuntimeException(u);
    }
}

interface

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// instanciando a classe Usuario do pacote modelo e criando seu objeto usuarios
Usuario usuarios = new Usuario();
usuarios.setNome(jTextField1.getText()); 
usuarios.setEndereco(jTextField2.getText());
usuarios.setCpf(jTextField3.getText());
usuarios.setEmail(jTextField4.getText());
usuarios.setTelefone(jTextField5.getText());
usuarios.setData1(jDateChooser1.getDate());

2 answers

1


If you have to use pre-java 8 native libraries you can do so:

Calendar c1 = Calendar.getInstance(); //pega data e hora atual
java.sql.Date data1 = new java.sql.Date(c1.getTimeInMillis()); //transforma p/ java.sql.Date
System.out.println(data1); //imprime para testar
c1.add(Calendar.DAY_OF_YEAR, 45); //soma 45 dias
data1.setTime(c1.getTimeInMillis()); //transforma para java.sql.Date
System.out.println(data1);//imprime para testar

Upshot:

2014-08-07
2014-09-21

The above code is just a functional example, in case you need to associate to the variable c1 the value of its variable data1, thus:

c1.setTime(data1);

instead of picking up current date and time. At the same time, realize that where I created data1 in my example I did just the reverse way, I initialized it with the value of c1. Both cases are possible.

  • Partner, thank you so much for the help. I solved my problem. Now next.. My system needs to send emails 2 days before each date. How do you advise me to do this? Would it be the case every time the system calls it check for emails to be sent? If you can give a light, it would be great!! Thank you dnv for the help!

  • @user11464 although this is a completely new issue and you can create a new topic for it, I’ll say what I think here because I won’t go on long. It depends a little on how your system works, if you turn on the system every day you can check yes the way you said, if it stays on all the time you should include some timer or set some event that will trigger the verification of the need of sending the email. If my comment left you with questions create a new question, and do not forget to give plenty of details, including whether it is a web system, desktop or Android.

0

When needed I used this function and worked very well:

   public Date somaDias(Date data, int dias) {
        Calendar cal = new GregorianCalendar();

        cal.setTime(data);

        cal.add(Calendar.DAY_OF_MONTH, dias);

        return cal.getTime();
    }

Browser other questions tagged

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