Solved

# Find if date is between two dates

Posted on 2005-04-06
293 Views
Last Modified: 2010-04-01
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

0
Question by:Millkind
7 Comments

LVL 19

Expert Comment

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

LVL 92

Expert Comment

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

LVL 19

Expert Comment

yup thats the second method which I forgot to mention ( thanks Objects)
0

LVL 92

Accepted Solution

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

LVL 8

Assisted Solution

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

Author Comment

I've got it. Thanks to all who helped and many of the answers did so im gonna split the points up.
0

LVL 92

Expert Comment

:)
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

#### 779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!