java snippet check if date1.before(date2)

Dear helpers

my java snippets goal is to check if date1 was before, after or is equal to date2.
So far, I created the following code:

if ((date1.equals(date2))
&& (date1== null))
out_DL_equal_self = “1” ;
if ((date1.before(date2))
&& (date1== null))
out_self_be4_DL = “1” ;
if ((date1.after(date2))
&& (date1== null))
out_DL_b4_self = “1” ;

In order to make this work, I imported the following libraries:
import java.util.;
import java.text.
;

I’m looking forward to your answers! Many thanks in advance!

Unlike the before- and the after-method knime accepts the equal-method.
It displays the error that “the method before(java.time.LocalDate) is undefined for the type java.time.LocalDate”

As the above mentioned error message indicates, both date1 and date2 are dates and also formatted this way.

Hi,
comparison in Java are usually done with the method “compareTo” (e.g. date1.compareTo(date2) ). This method provides a result < 0, if date1 is less than date2, = 0, if date1 is equal to date2, and > 0 if date1 is greater than date2.
The important part when comapring two dates this way is, that they are both not null. Otherwise, you will get an error. Also note that in your caste ‘&&’ denotes the logical and so for

if ((date1.before(date2))
&& (date1== null))
out_self_be4_DL = “1” ;

to hold true, date1 has to be null AND before date2, which is not possible.
An easier solution would be to use the Date&Time Difference Node to compute the differences. In this case you simply have to ensure to choose the right ‘Base Column’ and the correct granulatiry (for dates only days should be enough). Afterwards you can use the Rule Engine Node for your prediction. Therefore you have to check if the output column of the Date&Time Difference Node is less than, equal to, or greater than 0. In the case that this output column contains a missing value, either date1 or date2 (or both) contain a missing value. The checking for missing values can also be part of your rule in the Rule Engine Node.

Cheers,
Moritz

compareTo works! Thank you!

In the end, the code looks like the following one:
if (date1==null)
out_DLclearing_only = 1;
if (date2==null)
out_selfclearing_only = 1;
if (date1.equals(date2))
out_DL_equal_self = 1 ;
if (date1.compareTo(date2) < 0 )
out_self_be4_DL = 1 ;
if (date1.compareTo(date2) > 0)
out_DL_b4_self = 1 ;

There still seems to be a problem in the code in the case that date1 or date2 is null, as you still make the comparison date1.equals(date2).
So to make it more robust I’d suggest to do something like this

if (date1 == null || date2 == null) { // date1 or date2, or both are null
  if (date1 == date2) // this checks if both are null
   out_DL_equal_self = 1 ;
  if (date1==null)
   out_DLclearing_only = 1;
  if (date2==null)
   out_selfclearing_only = 1;
} else { // neither date1 nor date2 is null
  if (date1.equals(date2))
   out_DL_equal_self = 1 ;
  if (date1.compareTo(date2) < 0 )
   out_self_be4_DL = 1 ;
  if (date1.compareTo(date2) > 0)
   out_DL_b4_self = 1 ;
}

(Note ‘//’ denotes a comment til the end of the line)
What this does is that it checks in the first if-statement if either date1 or date2, or both are null.
In the case that both are null out_DL_equal_self, out_DLclearing_only, out_selfclearing_only are set to 1 (in case you only want to set out_DL_equal_self to 1 you simply have to replace ‘if (date1==null)’ with 'else if (date1==null), and ‘if (date2==null)’ with ‘else if(date2==null)’.

Cheers,
Moritz

1 Like

You can do the same with native KNIME nodes :slight_smile:
It is easier and you don’t need to handle any problems with the code as null checkings.

Therefore use the Time difference node to calculate the distance between the two columns. Afterwards use a row filter to filter the differences below 0

Cheers. Iris

1 Like