Comparison of Dates

Hi allI need to compare two datesand need to find out if the second date is ahead of the first by some days (eg. 7).What is the best way to do this. In what format do i generate the two datesThanks Rajani
[260 byte] By [rajani_s] at [2007-9-26 2:10:30]
# 1

I have enclosed some code for you....it is Javascript and it will take the date and parse it into variables you can work with.

--

var dateStr = date.value;

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null)

{

alert("Date is not in a valid format.")

date.select();

return false;

}

// parse date into variables

day = matchArray[1];

month = matchArray[3];

year = matchArray[4];

--

If you have two dates, pass them both in and then you can compare the number of days, months or years between them.

The secod line in the code above is a regular expression string that makes sure the date conforms to:

dd/mm/yyyy

or

dd-mm-yyyy

hope it helps

wmacey at 2007-6-29 9:01:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi Thans for ur reply.Buit I am looking for a method using Pure Java alone, not using Javascript.Rajani
rajani_s at 2007-6-29 9:01:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Hi

>

> Thans for ur reply.

>

> Buit I am looking for a method using Pure Java alone,

> not using Javascript.

>

> Rajani

Hi.

Here's an example using jdk 1.3, but I think this also works pretty easy with jdk 1.2.x.

The example can only tell which date is before / after another date, but not how much is the difference, though.

Regards,

Rick

import java.util.*;

import java.text.*;

public class DateExample {

public static void main (String args[]) {

// I am not checking here the minimum number of parameters though I might

SimpleDateFormat sdf=new SimpleDateFormat("MMM'/'dd'/'yyyy");

try{

Date date0=sdf.parse(args[0]);

Date date1=sdf.parse(args[1]);

if (date0.compareTo(date1) <= 0)

System.out.println(args[0]+" before or equal "+args[1]);

else

System.out.println(args[0]+" after "+args[1]);

}

catch (ParseException pe) {

System.out.println("One of the input dates is invalid : \n"+pe);

}

}

}

amihailescu at 2007-6-29 9:01:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

See if this is what you want...

import java.util.*;

import java.text.*;

public class DateCompare

{

public static final void main(String[] args) throws Exception

{

DateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

Date aDate = dateFormatter.parse(args[0]);

Date bDate = dateFormatter.parse(args[1]);

System.out.println("It is " + compareDates(aDate, bDate, Integer.parseInt(args[2])) + " that " + args[1] + " is " + args[2] + " days in future of " + args[0]);

}

/**

* Returns true if and only if bDate is in the future of aDate by exactly differenceInDays

*/

static boolean compareDates(Date aDate, Date bDate, int differenceInDays)

{

boolean returnValue = false;

Calendar aCalendar = Calendar.getInstance();

aCalendar.setTime(aDate);

aCalendar.add(Calendar.DATE, differenceInDays);

Calendar bCalendar = Calendar.getInstance();

bCalendar.setTime(bDate);

returnValue = bCalendar.equals(aCalendar);

return(returnValue);

}

};

Here is an example usage...

java DateCompare 07/20/2001 07/28/2001 7

gives...

It is false that 07/28/2001 is 7 days in future of 07/20/2001

Another example usage...

java DateCompare 07/21/2001 07/28/2001 7

gives...

It is true that 07/28/2001 is 7 days in future of 07/21/2001

neville_sequeira at 2007-6-29 9:01:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi allThanks for the help. I was able to modify your program to suit my needs.Also, sorry for the cross psoting. Wasnt sure which forum was ideal for this questionRajani
rajani_s at 2007-6-29 9:01:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...