cancel
Showing results for 
Search instead for 
Did you mean: 

Date comparision not working as Expected

former_member214651
Active Contributor
0 Kudos

Hi All,

I want to validate one of the date fields in my application. I need to check if start date is not before today's date for which i have written the following code:

Date dtStartDate = wdContext.currentContextElement().getStartDate();

Date sqlCurrDate = new Date(System.currentTimeMillis());

if(dtStartDate.before(sqlCurrDate))

{

---messageManager.reportException("Start Date is before Today's Date...");

}

this works fine if i select any day before current date, but this message displays even if i have selected today's date in the UI element, which is not what i want

I tried the same using compare(), equals(), but the same result.

the NWDS used is CE 71. composition environment SP06. Does this have anything to do with the above behavior?

Kindly let me know if there is any other way to achieve this functionality?

Regards,

Poojith MV

Edited by: Poojith M V on Mar 8, 2010 2:48 PM

Accepted Solutions (1)

Accepted Solutions (1)

p330068
Active Contributor
0 Kudos

Hi Poojith

Please print dtStartDate and sqlCurrDate using MessageManager and check the result. then compare result with method.

Hope this will helps you.

Regards,

Arun Jaiswal

former_member214651
Active Contributor
0 Kudos

Hi Arun,

I tried printing both the dates using messagemanager, if the selected date is 08/03/2010 and the current date is 08/03/2010. it prints the same date, but when i print the comparision result, the following is printed:

1. dtStartDate--08/03/2010, Current Dste -- 09/03/2010, result-- false

2. dtStartDate--10/03/2010, Current Date -- 09/03/2010, result-- true

3. dtStartDate-- 09/03/2010, Current Date -- 09/03/2010, result - false

This is the problem, even if i have selected the current date, the result of comparision is false.

Regards,

Poojith MV

Former Member
0 Kudos

Hi Poojith,

I think the problem is caused by the fact that your start date represents the time the actual day started (so midnight), whereas your current date includes the actual time (and Date method before compares milliseconds). Please try substituting your check with the following version and let us know if this works as you want:


if(!sqlCurrDate.after(dtStartDate)) {
  ---messageManager.reportException("Start Date is before Today's Date...");
}

Cheers, harald

Answers (2)

Answers (2)

former_member214651
Active Contributor
0 Kudos

Problem solved!!!

Former Member
0 Kudos

Hi,

selecting date you choose also time - so you probably get midnight for today's date, and current time for System.currentTimeInMilis().

In this case, you check if today's midnight is before current date and time - which is true.

To solve your problem, you should reset time in current time, or compare tommorow's date and midnight.

Regards,

--

Przemysław

former_member214651
Active Contributor
0 Kudos

Hi,

Thanks for the reply. But i am unable to figure out from your reply. Kindly let me know if it is possible to get the date without time?

Regards,

Poojith MV