-7

First I get:

LocalDate today = LocalDate.now();

and second

Date date = new Date();
date.setDate(Integer.valueOf(s[0]));
date.setMonth(Integer.valueOf(s[1]));
date.setYear(Integer.valueOf(s[2]));
LocalDate topicDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

How to check whether the differences between the first date and the second is 7 days?
For example, today is 03-08-2015 and the second date is 20-07-2015 and the difference between is more than 7 days, but how to check this?

Should I convert date to millisecond?

Daniel Puiu
  • 962
  • 6
  • 21
  • 29
dekros
  • 59
  • 1
  • 8
  • What have you tried? There are a lot of questions on SO related to this topic. e.g: http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java – rmlan Aug 03 '15 at 20:32
  • 1
    There are multiple answers. You could compare timestamps based on seconds, or you could use a library like Joda-Time (http://www.joda.org/joda-time/) to solve this. (`Days.daysBetween()`) – dognose Aug 03 '15 at 20:32
  • possible duplicate of [Calculating the difference between two Java date instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Erick G. Hagstrom Aug 03 '15 at 20:34
  • Guys, this is a `java.time` question, so why are you suggesting answers that relate to `Date` or JodaTime? – RealSkeptic Aug 03 '15 at 20:34
  • http://www.joda.org/joda-time/ – ControlAltDel Aug 03 '15 at 20:34
  • convert to long and compare. see: http://stackoverflow.com/questions/12473550/how-to-convert-string-date-to-long-millseconds – GregH Aug 03 '15 at 20:35
  • Note to those that are suggesting joda-time, direct quote from joda-time website: "Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310)." – Andrew Mairose Aug 03 '15 at 20:36
  • possible duplicate of [Java 8: Calculate difference between two LocalDateTime](http://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime) – Tom Aug 03 '15 at 20:39

5 Answers5

0

I Believe that is still the best way at the moment. You can view some insights on the subject here: Calculate date/time difference in java

Community
  • 1
  • 1
Roy Shahaf
  • 494
  • 5
  • 10
  • You should include the most relevant information from the URL into your answer, in case the URL stops working, even though it's a SO URL. – Shar1er80 Aug 03 '15 at 20:37
0

You could convert to milliseconds or you could individually check if the year was larger, then if they are the same check to see if the month is larger then check day. Converting to milliseconds would be very easy though.

mumfy
  • 154
  • 1
  • 12
0

I believe you are looking for something like this:

Date date = /*your date object you want to compare*/;
Instant now = Instant.now();
Instant sevenDaysFromYourDate = Instant.ofEpochMilli(date.getTime()).plus(Duration.ofDays(7));

if (now.isAfter(sevenDaysFromYourDate)) {
    //today is more than seven days past date
}
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102
0

Since you are using Java 8 LocalDate, you can use the plusDays or minusDays methods of the LocalDate class.

Furthermore, you shouldn't be using an (old, not recommended for use) java.util.Date object to create your second date. It's better to use LocalDate.of which builds a date from the year, month and day.

Example code:

    LocalDate today = LocalDate.now();
    LocalDate topicDate = LocalDate.of(
                             Integer.valueOf(s[2]),
                             Integer.valueOf(s[1]),
                             Integer.valueOf(s[0]));
    System.out.println(today);
    System.out.println(topicDate);
    if ( today.minusDays(7).equals(topicDate)) {
        System.out.println( "Exactly a week difference between today and topicDate");
    } else if ( today.minusDays(7).compareTo(topicDate) > 0 ) {
        System.out.println("TopicDate is more than a week before today");
    } else {
        System.out.println("TopicDate is less than a week before today");
    }

Note that you can use the compareTo for exact equality as well - I just wanted to demonstrate that for equality, equals also works.

And of course, there are the isAfter and isBefore methods that also do the comparison in an elegant way.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0
LocalDate today = LocalDate.now();
if (topicDate.isAfter(today.plusDays(7))) {
    System.out.println("Yes");
}
else {
    System.out.println("No");
}