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

Asked

Viewed 407 times

0

I made the following form of license

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, wanted to get a date not of the system more yes example a Timezone do not know how to do this someone could help me on this?

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

1 answer

4


Considering that your database will be on a server that is out of the user’s control, one of the ways to resolve it is directly on 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 bank creation to validate the 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 status and will return directly in the field if you are VENCIDO or LIBERADO. So the control is exactly in your 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);
}

Okay, validation will be done. But I have a tip for you. It’s 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 is important that you try to organize and separate the responsibilities of each class so that the code is not messy.

  • 1

    Okay Thanks for helping me !

Browser other questions tagged

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