11

I would like to check if two date exceeds a week, like, check if two dates have seven days,

at point the data range should be within a week only(7 Days).

i have tried something like this,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IsDateRangeExceedsWeek 
{
    public static void main( String[] args ) 
    {
        try{

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date fromDate = sdf.parse("2015-05-01");
            Date toDate = sdf.parse("2015-05-07");

            System.out.println(sdf.format(fromDate));
            System.out.println(sdf.format(toDate));

            if(fromDate.compareTo(toDate)>0){
                System.out.println("From Date should be less than To Date");
            } else if(fromDate.compareTo(toDate)==0){
                System.out.println("From Date is equal to To Date");
            } 

        }catch(ParseException ex){
            ex.printStackTrace();
        }
    }
}

Could some one help ?

Edi G.
  • 2,432
  • 7
  • 24
  • 33
Joe
  • 4,460
  • 19
  • 60
  • 106

3 Answers3

19

Using the java.time classes built into Java 8 and later:

LocalDate from = LocalDate.parse("2015-05-01");
LocalDate to = LocalDate.parse("2015-05-07");

long days = ChronoUnit.DAYS.between(from, to);    // 6 days
long weeks = ChronoUnit.WEEKS.between(from, to);  // 0 weeks
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Misha
  • 27,433
  • 6
  • 62
  • 78
15

The easiest way is to use Jodatime and use

Days.daysBetween(start, end).getDays()

Another solution is to use Calendar, add 7 days and compare again.

  Calendar c=Calendar.getInstance();
  c.setTime(fromDate);
  c.add(Calendar.DATE,7);
  if(c.getTime().compareTo(toDate)<0){
    It's more than 7 days.
  }
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See the [correct Answer by Misha](https://stackoverflow.com/a/30207933/642706) for how to use *java.time*. – Basil Bourque Jul 23 '18 at 05:24
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 23 '18 at 05:24
  • Yeah I know about that. But my answer is more than 3 years old. Java 8 was just released then and I don't think anyone (including me) wasn't using it yet ;) If I was asked now I would vote for the other answer using java.time – Veselin Davidov Jul 23 '18 at 07:29
  • My comments are not criticisms of your original writing (correct and useful when written), just “FYI” facts for today’s readers. – Basil Bourque Jul 23 '18 at 12:10
  • Yeah - its completely valid. I even upvoted the other answer ;) – Veselin Davidov Jul 23 '18 at 12:27
5

You can try like this:

if(Days.daysBetween(fromDate,toDate ).getDays()>7)

Check JodaTime API

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). See the [correct Answer by Misha](https://stackoverflow.com/a/30207933/642706) for how to use *java.time*. – Basil Bourque Jul 23 '18 at 05:26