Comparing two dates javafx

I am writing an application based on javafx / hibernate. Right now I have a problem where I want to create a new table with articles that are on sale for the last 7/30/365 days (or the selected selected date).

  • articles are stored in the database with the current date ( Date articleDate = new Date();

    )
  • all articles are retrieved from the database when the program starts and are located in ObserveList

    named articlesDBList

    .

What I've tried so far:

ObserveList<ArticlesDB> sortForSevenDays =FXCollections.observableArrayList(); 
for(ArticleDB article: articleDBList) {
    if() { //missing statement for comparing articles by date for past 7 days
        sortForSevenDays.add(article);
    }
}  

      

+3


source to share


1 answer


In mkyong have 3 possibilities of comparison Date

in Java. An even better choice (IF Javaversion <8) Joda-Time is presented in the comment section .

The simplest solution without Joda would probably just compare with Calendar

:



Calendar articleCal = Calendar.getInstance();
articleCal.setTime(articleDate);
//check for past 7 days
Calendar check = Calendar.getInstance();
check.add(Calendar.DATE, -7);
if(articleCal.after(check)) 
    sortForSevenDays.add(article);

      

INFO: Just keep in mind that this compares for exactly 7 days (including minutes, etc.).

+1


source







All Articles