By ant


2010-04-07 12:48:36 8 Comments

How do I compare dates in between in Java?

Example:

date1 is 22-02-2010
date2 is 07-04-2010 today
date3 is 25-12-2010

date3 is always greater than date1 and date2 is always today. How do I verify if today's date is in between date1 and date 3?

11 comments

@Basil Bourque 2014-01-29 04:38:28

tl;dr

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Boolean isBetween = 
    ( ! today.isBefore( localDate1 ) )  // “not-before” is short for “is-equal-to or later-than”.
    &&
    today.isBefore( localDate3 ) ; 

Or, better, if you add the ThreeTen-Extra library to your project.

LocalDateRange.of(
    LocalDate.of( … ) ,
    LocalDate.of( … )
).contains(
    LocalDate.now()
)

Half-open approach, where beginning is inclusive while ending is exclusive.

Bad Choice of Format

By the way, that is a bad choice of format for a text representation of a date or date-time value. Whenever possible, stick with the standard ISO 8601 formats. ISO 8601 formats are unambiguous, understandable across human cultures, and are easy to parse by machine.

For a date-only value, the standard format is YYYY-MM-DD. Note how this format has the benefit of being chronological when sorted alphabetically.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Table of all date-time types in Java, both modern and legacy

DateTimeFormatter

As your input strings are non-standard format, we must define a formatting pattern to match.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" );

Use that to parse the input strings.

LocalDate start = LocalDate.parse( "22-02-2010" , f );
LocalDate stop = LocalDate.parse( "25-12-2010" , f );

In date-time work, usually best to define a span of time by the Half-Open approach where the beginning is inclusive while the ending is exclusive. So we want to know if today is the same or later than the start and also before the stop. A briefer way of saying “is the same or later than the start” is “not before the start”.

Boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ;

See the Answer by gstackoverflow showing the list of comparison methods you can call.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


UPDATE: This “Joda-Time” section below is left intact as history. The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

Joda-Time

Other answers are correct with regard to the bundled java.util.Date and java.util.Calendar classes. But those classes are notoriously troublesome. So here's some example code using the Joda-Time 2.3 library.

If you truly want a date without any time portion and no time zone, then use the LocalDate class in Joda-Time. That class provides methods of comparison including compareTo (used with Java Comparators), isBefore, isAfter, and isEqual.

Inputs…

String string1 = "22-02-2010";
String string2 = "07-04-2010";
String string3 = "25-12-2010";

Define a formatter describing the input strings…

DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd-MM-yyyy" );

Use formatter to parse the strings into LocalDate objects…

LocalDate localDate1 = formatter.parseLocalDate( string1 );
LocalDate localDate2 = formatter.parseLocalDate( string2 );
LocalDate localDate3 = formatter.parseLocalDate( string3 );

boolean is1After2 = localDate1.isAfter( localDate2 );
boolean is2Before3 = localDate2.isBefore( localDate3 );

Dump to console…

System.out.println( "Dates: " + localDate1 + " " + localDate2 + " " + localDate3 );
System.out.println( "is1After2 " + is1After2 );
System.out.println( "is2Before3 " + is2Before3 );

When run…

Dates: 2010-02-22 2010-04-07 2010-12-25
is1After2 false
is2Before3 true

So see if the second is between the other two (exclusively, meaning not equal to either endpoint)…

boolean is2Between1And3 = ( ( localDate2.isAfter( localDate1 ) ) && ( localDate2.isBefore( localDate3 ) ) );

Working With Spans Of Time

If you are working with spans of time, I suggest exploring in Joda-Time the classes: Duration, Interval, and Period. Methods such as overlap and contains make comparisons easy.

For text representations, look at the ISO 8601 standard’s:

  • duration
    Format: PnYnMnDTnHnMnS
    Example: P3Y6M4DT12H30M5S
    (Means “three years, six months, four days, twelve hours, thirty minutes, and five seconds”)
  • interval
    Format: start/end
    Example: 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z

Joda-Time classes can work with strings in both those formats, both as input (parsing) and output (generating strings).

Joda-Time performs comparisons using the Half-Open approach where the beginning of the span is inclusive while the ending is exclusive. This approach is a wise one for handling spans of time. Search StackOverflow for more info.

@Hemil 2020-02-09 19:02:37

This should be at the top of all other answers

@Chandra Sekar 2010-04-07 12:50:19

Use compareTo:

date1.compareTo(date2);

@Giancarlo Ventura Granados 2016-01-11 17:26:48

Works perfect with Date, Byte, Long, Integer...

@Syed 2018-08-15 04:41:54

Sometimes it doesn't work. I triggered multiple times using in a main method to check equality. Most of the times it says equal, but not all times.

@Chandra Sekar 2018-08-16 14:02:22

@Syed Can you share your code?

@Syed 2018-08-17 07:26:58

@ChandraSekarS, try to execute as a main method code by taking same dates and see the output. Sometimes, it gives 0 and sometimes 1.

@Iman Marashi 2017-01-06 19:37:48

This method worked for me:

 public static String daysBetween(String day1, String day2) {
    String daysBetween = "";
    SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {
        Date date1 = myFormat.parse(day1);
        Date date2 = myFormat.parse(day2);
        long diff = date2.getTime() - date1.getTime();
        daysBetween = ""+(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return daysBetween;
}

@Basil Bourque 2017-01-07 01:23:19

Does not answer the Question. The Question involves three dates, not two. “How do I verify if today's date is in between date1 and date 3?” Furthermore, this calculation returns the number of 24-hour periods, not the number of calendar days elapsed. Yet another problem: This answer uses date-time values while the Question is about date-only values without time-of-day.

@Bart Kiers 2010-04-07 12:50:14

Date has before and after methods and can be compared to each other as follows:

if(todayDate.after(historyDate) && todayDate.before(futureDate)) {
    // In between
}

For an inclusive comparison:

if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) {
    /* historyDate <= todayDate <= futureDate */ 
}

You could also give Joda-Time a go, but note that:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Back-ports are available for Java 6 and 7 as well as Android.

@Daniel Hári 2015-12-02 22:52:38

Is this inclusive, or exclusive for the borders?

@Mifmif 2016-05-10 10:16:55

@DanielHári no its not inclusive. you can use the solution suggested in the first comment , or use CompareTo()>=0 .

@Daniel Hári 2016-05-10 10:20:10

The normal usually "left inclusive, right exclusive", this why I think this should be specified exactly. With "left inclusive, right exclusive" borders, you can specify fe: a month interval easily: [2016/04/01, 2016/05/01], this why this is the normal and used as default in many use cases.

@Basil Bourque 2018-02-22 23:47:52

FYI, the troublesome old date-time classes such as java.util.Date are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle.

@Bart Kiers 2018-02-23 07:52:15

@BasilBourque yes, correct, hence the remarks about Joda and java.time for Java 8, which is now the standard version (contrary to the time I wrote this answer). Feel free to edit this answer and add more recent example code, of course! ;) (EDIT: ah, I see you yourself already provided an answer with said examples)

@gstackoverflow 2015-11-13 12:48:23

Update for Java 8 and later

These methods exists in LocalDate, LocalTime, and LocalDateTime classes.

Those classes are built into Java 8 and later. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

@Punithapriya 2016-08-08 04:55:17

how to download these three classes

@gstackoverflow 2016-08-08 08:08:21

@Punithapriya These classes exists in java 8 standart library. Thus just download java 8

@Punithapriya 2016-08-08 09:13:35

yes i have seen thanks

@Garambe 2015-11-24 10:49:30

Try this

public static boolean compareDates(String psDate1, String psDate2) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
        Date date1 = dateFormat.parse(psDate1);
        Date date2 = dateFormat.parse(psDate2);
        if(date2.after(date1)) {
            return true;
        } else {
            return false;
        }
    }

@Salman A 2015-05-14 18:57:03

You can use Date.getTime() which:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

This means you can compare them just like numbers:

if (date1.getTime() <= date.getTime() && date.getTime() <= date2.getTime()) {
    /*
     * date is between date1 and date2 (both inclusive)
     */
}

/*
 * when date1 = 2015-01-01 and date2 = 2015-01-10 then
 * returns true for:
 * 2015-01-01
 * 2015-01-01 00:00:01
 * 2015-01-02
 * 2015-01-10
 * returns false for:
 * 2014-12-31 23:59:59
 * 2015-01-10 00:00:01
 * 
 * if one or both dates are exclusive then change <= to <
 */

@Sunil Kumar Sahoo 2014-01-13 07:26:04

Following are most common way of comparing dates. But I have prefer first one

Approach-1 : Using Date.before(), Date.after() and Date.equals()

            if(date1.after(date2)){
                System.out.println("Date1 is after Date2");
            }

            if(date1.before(date2)){
                System.out.println("Date1 is before Date2");
            }

            if(date1.equals(date2)){
                System.out.println("Date1 is equal Date2");
            }

Approach-2 : Date.compareTo()

           if(date1.compareTo(date2)>0){
                System.out.println("Date1 is after Date2");
            }else if(date1.compareTo(date2)<0){
                System.out.println("Date1 is before Date2");
            }else{
                System.out.println("Date1 is equal to Date2");
            }

Approach-3 : Calender.before(), Calender.after() and Calender.equals()

Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(date1);
            cal2.setTime(date2);

            if(cal1.after(cal2)){
                System.out.println("Date1 is after Date2");
            }

            if(cal1.before(cal2)){
                System.out.println("Date1 is before Date2");
            }

            if(cal1.equals(cal2)){
                System.out.println("Date1 is equal Date2");
            }

@Basil Bourque 2018-02-22 23:48:51

FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle.

@Brownsoo Han 2013-11-27 07:28:19

This code determine today is in some duration.. based on KOREA locale

    Calendar cstart = Calendar.getInstance(Locale.KOREA);
    cstart.clear();
    cstart.set(startyear, startmonth, startday);


    Calendar cend = Calendar.getInstance(Locale.KOREA);
    cend.clear();
    cend.set(endyear, endmonth, endday);

    Calendar c = Calendar.getInstance(Locale.KOREA);

    if(c.after(cstart) && c.before(cend)) {
        // today is in startyear/startmonth/startday ~ endyear/endmonth/endday
    }

@sam 2010-04-07 12:56:12

Compare the two dates:

  Date today = new Date();                   
  Date myDate = new Date(today.getYear(),today.getMonth()-1,today.getDay());
  System.out.println("My Date is"+myDate);    
  System.out.println("Today Date is"+today);
  if (today.compareTo(myDate)<0)
      System.out.println("Today Date is Lesser than my Date");
  else if (today.compareTo(myDate)>0)
      System.out.println("Today Date is Greater than my date"); 
  else
      System.out.println("Both Dates are equal"); 

@Dr. No 2011-11-03 15:48:29

I think that "new Date(today.getYear(),today.getMonth()-1,today.getDay());" it's deprecated. download.oracle.com/javase/6/docs/api/java/util/Date.html

@Lii 2015-03-23 07:31:26

@Muath: Although I am not 100 % sure, I think it is because the month component in Date is zero-indexed.

@Sachith 2016-12-24 20:13:50

i think in there else part is not working :)

@Everyone 2010-04-07 13:18:21

Use getTime() to get the numeric value of the date, and then compare using the returned values.

Related Questions

Sponsored Content

55 Answered Questions

[SOLVED] How do I get the current date in JavaScript?

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4055391 View
  • 3499 Score
  • 66 Answer
  • Tags:   java random integer

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

87 Answered Questions

56 Answered Questions

[SOLVED] How to format a JavaScript date

39 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

15 Answered Questions

[SOLVED] Calculate difference between two dates (number of days)?

  • 2009-10-22 13:47:15
  • leora
  • 1159637 View
  • 1092 Score
  • 15 Answer
  • Tags:   c# date

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

15 Answered Questions

[SOLVED] Comparing Java enum members: == or equals()?

  • 2009-11-17 17:26:27
  • Matt Ball
  • 666406 View
  • 1735 Score
  • 15 Answer
  • Tags:   java enums

38 Answered Questions

[SOLVED] Compare two dates with JavaScript

Sponsored Content