cancel
Showing results for 
Search instead for 
Did you mean: 

dates comparision

Former Member
0 Kudos

HI,

I want ot compare to dates and i need to find the first date.

for example : if i have 12/10/09 and 13/10/09 then i need to compare and find the first date.

I have used the below code

Date delivedate1 = null;
Date deliverydate2 =null;
if(delivertemp1 == null)
{
delivertemp1 = wdContext.nodetest().gettestElementAt(i).getdate();
k=i;
}
else
{     
deliverytemp2 = wdContext.nodetest().gettestElementAt(i).getdate();
}
if(deliverytemp2 != null && deliverytemp2.compareTo(delivertemp1)>0)
{
  delivertemp1 = deliverytemp2;
  k=i;
}

the code working but some times it showing first date and some times it showing last date when i am testing.

Can any onegive me suggestions on this.

Regards,

Suresh

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Suresh,

Check for the null values and then use this

if(delivedate1.before(deliverydate2))
{
your code goes here...	
}

Regards,

P188071

Former Member
0 Kudos

Hi,

I have used this condition also but its not working.

if(deliverytemp2 != null && delivertemp1.before(deliverytemp2)) 
{
				
  delivertemp1 = deliverytemp2;
  k=i;
}

Can you tell me solution fo this issue.

Regards,

Suresh

chander_kararia4
Contributor
0 Kudos

Hi Suresh,

The above posted methods are correct. You may try any of them.

Just set the Locale (Time zone) before you proceed with the date comparison. Specially if you are doing for western countries.

Better you convert both the dates in 1 format. This could be possible issue.

By the way, could you specify the dates on which you are getting wrong result.

Best Regards

Chander Kararia

Former Member
0 Kudos

HI Chander,

Thanks for reply,

Now its working fine, i am getting first date but i have another problem.

From FM i am getting 2 records i.e

ID    description   QTY  Date
1      test1             4      13/10/09
2      test2             2      12/10/09          

Here i am getting first date i.e 12/10/09 but the cooresponding 3values is not getting.its showing first 3 values i.e
1 test1  4   12/10/09 
but i want to display
 2   test2   2 12/10/09  ..Dates are coming prefectly but remaing r values are not getting,its showing first row 3 values.

I am using the below code

Date delivertemp1 =null;
Date deliverytemp2 = null;
int k=0;
IOrderListElement order = null;
for(int i=0; i < listOfItemsSelected.size(); i++)
{
Integer index = (Integer)listOfItemsSelected.get(i);
order = wdContext.createOrderListElement();
		
if(delivertemp1 == null)
{
delivertemp1 = wdContext.nodetest().getnodeElementAt(index.intValue()).getdate();
k=i;
}
else
{     
deliverytemp2 = wdContext.nodetest().getnodeElementAt(index.intValue()).getdate();
}
if(deliverytemp2 != null && deliverytemp2.before(delivertemp1))
{
  delivertemp1 = deliverytemp2;
  k=i;
}
						                     
}

order.settID(Long.valueOf(wdContext.nodetest().getIt_testElementAt(k).getID()).toString());
order.setDescription(wdContext.nodetest().getIt_testElementAt(k).getdesc());
order.setqtyt(wdContext.nodetest().gettestElementAt(k).getqty())
order.setDeliveryDate(delivertemp1);

Please let me know if nay changes has to be done in my code.

Regards,

Suresh

chander_kararia4
Contributor
0 Kudos

HI Suresh,

It is very easy. You have the table records. You are getting the value correctly for date.

- Take the index value (row value) from the table.

- get the data from that index value for each column.

Let me know if you require anything more explanatory

#Frankly, I do not got your code sample. You may put some comments there to make it easy. Sorry for that.

Best Regards

Chander Kararia

Former Member
0 Kudos

HI Chander,

now i used comments for code.please have a look

Date delivertemp1 =null;
Date deliverytemp2 = null;
int k=0;
IOrderListElement order = null;
for(int i=0; i < listOfItemsSelected.size(); i++)//listOfItemsSelected.size(); size of FM
{
Integer index = (Integer)listOfItemsSelected.get(i);//values getting 
order = wdContext.createOrderListElement();///created element to value node
		
if(delivertemp1 == null)
{
delivertemp1 = wdContext.nodetest().getnodeElementAt(index.intValue()).getdate(); //dates are gettign from this fm node
k=i;
}
else
{     
deliverytemp2 = wdContext.nodetest().getnodeElementAt(index.intValue()).getdate();////dates are gettign from this fm node
}
if(deliverytemp2 != null && deliverytemp2.before(delivertemp1))//condtionf or checking first date
{
  delivertemp1 = deliverytemp2;
  k=i;
}
						                     
}
//settigns valus to value node
 
order.settID(Long.valueOf(wdContext.nodetest().getIt_testElementAt(k).getID()).toString());
order.setDescription(wdContext.nodetest().getIt_testElementAt(k).getdesc());
order.setqtyt(wdContext.nodetest().gettestElementAt(k).getqty())
order.setDeliveryDate(delivertemp1);

Please let me know if you need any clarification

Regards,

Suresh

chander_kararia4
Contributor
0 Kudos

Hi Suresh,

See there are only 3 conditions which will come in picture.

1. date1 < date2 (you need date1 row index)

2. date1 > date2 (you need date2 row index)

3. date1 == date2 (Not explained by you what to do)

I tried to dry run your logic, here is what I understand from this. (Consideration: listOfItemsSelected is containing only list of DATES)

After first run, here are the values

i=0;

control will go to 1st IF block, there value will be assigned to deliverytemp1

k=0;

else block & 2nd If block will not run

---

After 2nd run, here are the values

i=1;

1st IF will not run, control willl go to else block, there value will be assigned to deliverytemp2

2nd IF block will check,

IF TRUE -> k=1;

IF FALSE-> NOT SPECIFIED (by default next time for loop will start & assign the value to deliverytemp2.....n so on )

Hope this is clear understanding?

-


Do 1 thing....in your comparison statement, you are not writing [true / false]

date2.before(date1)==true............like this

try this

Best Regards

Chander Kararia

former_member185086
Active Contributor
0 Kudos

Hi

Please have a look on my previous for similar requirement .

[Setting default dates |;

[fetching previous month's date |;

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

int year = 1999;
int month = 11;
int day1 = 10;
int day2 = 11;

cal1.set(year,month,day1);
cal2.set(year,month,day2);

String date1 = ""+year+"//"+month+"//"+day1;
String date2 = ""+year+"//"+month+"//"+day2;

if( cal2.equals(cal1) )
        System.out.println(date1+" is the same as " + date2);
if( cal2.after(cal1) )
        System.out.println(date2 + "is after "+date1);
else
        System.out.println(date2 + "is before "+date1);
To do so, you can use java.text.SimpleDateFormat to format string into a date format: 
 
SimpleDateFormat formatter= new SimpleDateFormat("ddMMyyyy HHmm");
String dateStr = null;    
try
{
     dateStr = formatter.parse(dateString);
}
catch (Exception e)
{
     //handle exception
}
Or, use java.util.Calendar class: 
 
java.util.Calendar cal1 = java.util.Calendar.getInstance();
java.util.Calendar cal2 = java.util.Calendar.getInstance();

int year = 1999;
int month = 11;
int day1 = 10;
int day2 = 11;

cal1.set(year,month,day1);
cal2.set(year,month,day2);

String date1 = ""+year+"/"+month+"/"+day1;
String date2 = ""+year+"/"+month+"/"+day2;

if( cal2.equals(cal1) )
        System.out.println(date1+" is the same as " + date2);
if( cal2.after(cal1) )
        System.out.println(date2 + " is after "+date1);
if (cal2.before(cal1))
        System.out.println(date2 + " is before "+date1);

BR

Satish Kumar

Edited by: satish jhariya on Oct 7, 2009 6:12 PM