Find if date is between two dates

I have three dates stored as strings as mmddyyyy
one is start
two is end
three is entered by user

I want to see if three is between one and two.

I've looked through the calendar class at sun and am confused about it.
This needs to be done in JSP
 
MillkindAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
SimpleDateFormat df = new SImpleDateFormat("MMddyyy");
Date d1 = df.parse(s1);
Date d2 = df.parse(s2);
if (d.after(d1) && d.before(d2))
{
   // d is between d1 and d2
}

0
 
KuldeepchaturvediCommented:
well there are two ways of doing it...
one which is easy wd go something like this..

convert all the dates in YYYYMMDD
convert them in number and see if third number falls within the range.. ( simple mathmatics)..
since you have got them in string format all you will have to do with each of them is..

String date1 = request.getParameter("date1");

if(date1!=null &&date1.length()=8)
{
String years=date1.subString(4);
String rest = date1.subString(0,4);
date1=years+rest;
}
int ncdate1=new Integer(date1).intValue();

Do the same stuff with all three and after that is a small if condition as

if(ndat1<ndate2<ndate3)
{
yes
}

// PS I am very pethatic with substring numbers so please check them before using the above code as is..
0
 
objectsCommented:
Use SimpleDateFormat to first parse them into Dates and then use the before() and after() methods to check if date is between the two.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
KuldeepchaturvediCommented:
yup thats the second method which I forgot to mention ( thanks Objects)
0
 
koppchaConnect With a Mentor Commented:
Ohh it is already posted anyways just i will just post it

import java.util.*;

public class NoDot
{
      public static void main(String[] args)
      {
            String startDate="20040228";
            String endDate="20040314";
            String testDate="20040401";
            try
         {
                  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd" ) ;
            sdf.setLenient( false ) ;
            Date sDate=sdf.parse( startDate ) ;
                  Date eDate=sdf.parse(endDate);
                  Date tDate=sdf.parse(testDate);
                  if((tDate.after(sDate)&& tDate.before(eDate))){
                        System.out.println("I am in the Range");
                  }
                  else{
                  System.out.println("I am not in the range");
                  }

                  
                     
          }
                     
        catch( Exception ex )
        {  
                  System.out.println("This is the exception" + ex);
                  //do what ever you want to do here
        }    
         
       
           
}
}
0
 
MillkindAuthor Commented:
I've got it. Thanks to all who helped and many of the answers did so im gonna split the points up.
0
 
objectsCommented:
:)
0
All Courses

From novice to tech pro — start learning today.