if (today.compareTo(istoday)==0)...

thekingishere
Kilo Contributor

In SN's Learning Path "New to ServiceNow" > Module "Server Side Scripting" > Lesson "Exercise: Create a Business Rule", at the bottom of the page, the exercise provides this code:

Screen Shot 2017-06-20 at 6.10.22 AM.png

Shouldn't if(today.compareTo(istoday)==1) be used instead of   if(today.compareTo(istoday)==0), since 1 is "true" and 0 is "false"?

4 REPLIES 4

Siddartha Gudim
Tera Guru

CompareTo is a java function which returns 0 when both parameters are same.



Please find below the sample code which illustrate



public class Test {

   
public static void main(String args[]) {
   
Integer x = 5;
   
   
System.out.println(x.compareTo(3));
   
System.out.println(x.compareTo(5));
   
System.out.println(x.compareTo(8));    
   
}
}



Output would be



1


0


-1



hence if the both parameters are equal the function returns "0"




Please like or Mark correct based on the impact of the response.


Sid,



Yea, I see this now.  



Why does the System.out.println(x.compareTo(8) return a -1?   If 0 is true, 1 is false, what is -1 in this situation?



xjava.PNG


Hi,



Just to complement Siddartha answer,


It does not return 0 if equal and 1 if not equal, but


- If the Integer is equal to the argument returns 0.


- If the Integer is less than the argument returns -1.


- If the Integer is greater than the argument returns 1.



Hope it helps,


Ginés.


Hey Sid,

What if instead of if(today.compareTo(istoday)==0) I used:

if(today === istoday) {...}

When I use this if statement, it messes things up, and the error message does not appear on the form when I select the same date as 'u_when_needed'.  How have I confirmed this assumption?  By debugging the business rule.  The debugger shows that both var's formats are exactly the same, so my if statement should work but it doesn't. 

Can you help?