AleksandarDjokic
AleksandarDjokic

Reputation: 33

Comparing 2 dates javafx

I'm writing an application which is based on javafx/hibernate. Now i have problem when i want to make new table with articles that are sold last 7/30/365 days (or custom date chosen).

What i have 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);
    }
}  

Upvotes: 3

Views: 1232

Answers (1)

ceekay
ceekay

Reputation: 466

On mkyong there are 3 possibilities in comparing Date in Java. In the comment section even the better choice (IF Javaversion < 8) of Joda-Time is presented.

The simpliest Solution without Joda would probably be just to compare using 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 (incl. minutes etc.).

Upvotes: 1

Related Questions