How to get the date of a server instead of local server?

0

I made the following license form

con.executaSql("select *from vencimento");
    con.rs.last();
    SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
    Date hoje = new Date();
    String dataAtual = df.format(hoje);
    String dataSistema = con.rs.getString("data");
    int diaAt, mesAt, anoAt, diaVenc, mesVenc, anoVenc;
    diaAt = Integer.parseInt("" + dataAtual.charAt(0) + dataAtual.charAt(1));
    mesAt = Integer.parseInt("" + dataAtual.charAt(2) + dataAtual.charAt(3));
    anoAt = Integer.parseInt("" + dataAtual.charAt(4) + dataAtual.charAt(5) + dataAtual.charAt(6) + dataAtual.charAt(7));

    diaVenc = Integer.parseInt("" + dataSistema.charAt(0) + dataSistema.charAt(1));
    mesVenc = Integer.parseInt("" + dataSistema.charAt(2) + dataSistema.charAt(3));
    anoVenc = Integer.parseInt("" + dataSistema.charAt(4) + dataSistema.charAt(5) + dataSistema.charAt(6) + dataSistema.charAt(7));

    if ((diaAt <= diaVenc) && (mesAt <= mesVenc) && (anoAt == anoVenc)) {

        TelaPrincipal tela = new TelaPrincipal(jTextFieldUsername.getText());
        tela.setVisible(true);
        dispose();

    }else{
        TelaValidaSis validasis = new TelaValidaSis();
        validasis.setVisible(true);

    }

When the user or client changes the date of the computer the system still works, I would like to take a date not from the system but rather a timezone example I do not know how to do this could someone help me about this?

I am using MYSQL as the database the date table is in varchar

    
asked by anonymous 17.11.2016 / 01:15

1 answer

4

Considering that your database will be on a server that is out of user control, one of the ways to resolve it is directly in query as follows:

SELECT CASE
         WHEN STR_TO_DATE(data, '%d%m%Y') > CURDATE() THEN 'LIBERADO'
         ELSE 'VENCIDO'
       END AS estado
  FROM vencimento

I used the following database creation to validate query :

create table vencimento(data varchar(8));
insert into vencimento values('20112016');
insert into vencimento values('15112016');

The case will already validate the current license state and return directly to the field if it is VENCIDO or LIBERADO . So the control stays exactly in its query .

After this change you can change the validation performed in the application to:

String estado = con.rs.getString("estado");

if (estado.equals("LIBERADO")) {
  TelaPrincipal tela = new TelaPrincipal(jTextFieldUsername.getText());
  tela.setVisible(true);
  dispose();
} else {
  TelaValidaSis validasis = new TelaValidaSis();
  validasis.setVisible(true);
}

Ready, the validation will be performed. But I have a tip to give you. It is important that you separate the responsibilities in your program. You have a method that accesses the database and forwards everything to the screen, so it's important that you try to organize and separate the responsibilities of each class so that the code does not get messy.

    
17.11.2016 / 02:35