if (today.compareTo(istoday)==0)...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 06:16 AM
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:
Shouldn't if(today.compareTo(istoday)==1) be used instead of if(today.compareTo(istoday)==0), since 1 is "true" and 0 is "false"?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 06:25 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 08:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 08:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2018 10:21 AM
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?