Compare two dates

Asked

Viewed 1,518 times

4

I have two string containing dates dataSalva and dataDoDia and I’m wondering if the dataDoDia and 5 days longer than dataSalva how do I do this

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date data = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(data);
    Date data_atual = cal.getTime();
    String dataDoDia= dateFormat.format(data_atual);

Caught this data_completaand saved at the bank Ai when I enter I open the application I want to take this date in the bank and compare with the date of the day , that I will take the same way the date has been saved

String dataSalva = banco.get();

here I will have the date that is already saved then I’ll do the top part to get the date of the day

I want to compare these two dates dataSalva and dataDoDia

  • 1

    Put your code... It is better, because we will have more information, as the date format...

  • update her.. .. .

  • data is the dataSalva? I imagine that data_atual be the DataATual. Your code does not match the description of the question. Does this code have any relevance? Or is the question wrong?

  • I don’t have code , this one and the format of my date

  • look at if it became clearer

  • I gave an answer but I do not know if it is what you want since you do not have more information. I note that the last edition introduced a new name for the date.

  • I switched so as not to confuse

Show 2 more comments

1 answer

7


In general everyone recommends using the Jodatime which is much better than the original API. But you can do the following:

(DataAtual.getTime() - DataSalva.getTime()) / (1000 * 60 * 60 * 24) == 5

I put in the Github for future reference.

Java now has a more modern official API that is practically Jodatime.

  • That’s right I was wanting to vlw

  • I didn’t quite understand the first part of the answer. It would be an example of what?

  • You’re welcome, I was testing later :)

Browser other questions tagged

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