cancel
Showing results for 
Search instead for 
Did you mean: 

how to compare dates

Former Member
0 Kudos

Hi

i have two input fields with lable TO and FROM and i binded attributes of type date to these inputfilelds, and my requirement is when FROM date should always be greater than TO date,can u please help out in writing code.

Thanks

Raju

Accepted Solutions (0)

Answers (9)

Answers (9)

Former Member
0 Kudos

hi raju,

you just do the following code

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");

// Get Date 1

Date fromDate = df.parse(//get your corresponding date input value);

// Get Date 2

Date toDate = df.parse(//get your corresponding date input value);

if (fromDate.before(toDate))

//raise the message as you want

else

//raise the message as you want

with regards

shanto aloor

Former Member
0 Kudos

Hi Raju,

You can try the following code:

DateFormat df = new SimpleDateFormat("dd-mm-yyyy");

Date date1,date2; // java.util.Date;

date1 = wdContext.currentContextElement.get(From);

date2 = wdContext.currentContextElement.get(To);

// Get Date 1

Date d1 = df.parse(date1);

// Get Date 2

Date d2 = df.parse(date2);

String relation;

if (d1.equals(d2))

relation = "the same date as";

else if (d1.before(d2))

relation = "before";

else

relation = "after";

System.out.println(d1 + " is " + relation + ' ' + d2);

}

}

I hope this helps. If you need some more help, please get back, I will be happy to help you.

Cheers!!!

Umang

Former Member
0 Kudos

hi!

take two calendar

java.util.Calendar cal1 = java.util.Calendar.getInstance();

java.util.Calendar cal2 = java.util.Calendar.getInstance();

set the date using cal1.set and cal2.set method

if( cal2.equals(cal1) )

//your code

if( cal2.after(cal1) )

//your code

if (cal2.before(cal1))

//your code

hope this will help

thanks

vishal

Former Member
0 Kudos

Hi Raju

You can also go through the following link on how to create a simple input form with Error Validation.

[https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/60db527d-7be4-2a10-0283-e915cfb179a8]

Regards

Raghu

Former Member
0 Kudos

I guess your requirement would be FROM date should be smaller than TO date.

Assuming you have from and to dates in FROM and TO respectively.

From.after(TO) --> will return true is FROM is greater than TO else false.

Regards,

Murtuza

Former Member
0 Kudos

Hi ,

Use the below code to compare dates

if(  fromdate .after(todate) )
{

//your code

}  

Regards,

Sunitha Hari

Former Member
0 Kudos

Hi Raju,

As u said Todate and fromdate are your input fields, i think u want a validation to check the dates. So you can use Message Pool to validate the date field.

1)Under message pool you create Message key u2013 validate

Message type u2013 error

Message text as u2013 Enter valid input

2)Create a method called validate with two parameters todate and fromdate of date type

3)In Button action u have created write the following code

this.validate("TODATE");

this.validate("FROMDATE");

wdComponentAPI.getMessageManager().raisePendingException();

4)In Validate method u write the following code

IWDMessageManager ID = wdComponentAPI.getMessageManager();

//write code to get parameter values todate and fromdate

Int i=todate.compareTo(fromdate)

if(i<0)

{

ID.reportContextAttributeMessage(wdContext.nodeMSGNODE().currentMSGNODEElement(),info,IMessageMSG.VALIDATE,new Object[] {fromdate},true);

}

else

WdComponentAPI.getMessageManager().reportSuccess(u201Cvalid inputu201D);

Change the code according to your context. By this code a message will be displayed when the todate is greater than fromdate.

Regards

Raghu

Former Member
0 Kudos

Hi,

As Date class is implementing Comparable interface u can use "compareTo(Object)" method which returns integer value

Try this code.

Date from=get the From Date(from the attribute)

Date to=get the TO Date (from the attribute)

int i= from.compareTO(to);

if(i>0)

to date is bigger

else if(i<0)

from date is bigger

else

both r same.

Former Member
0 Kudos

Hi Raju,

To compare date you can use compareTo method like

TO.compareTo(FROM) returns value <0 if FROM is greater or

else returns >0 and returns 0 if both are equal.

Regards

Raghu

Former Member
0 Kudos

Hi raghu

can you please expalin it clearly with code.

Thanks

Raju