Java Snippet regarding date

Hi everyone!

I wrote a Java Snippet, that should test if deliverydatereal is equal to 0002-11-30. If that´s the case then it should be changed to deliverydatepromised - 1 

If not then the real date should be returned.

I wrote the following code (I changed the code of IRIS from this forum a bit):

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date real = new Date();
Date prom = new Date();
 try{
        real = format.parse($deliverydatereal$);
        prom = format.parse($deliverydatepromised$);
          if (real.equals("0002-11-30")) {
          //subtract 1 day
          prom.setTime(prom.getTime()-1 * 24  * 60 * 60 * 1000); }
          else { prom = real; }
        }
catch(Exception e){}
return format.format(prom);

The problem is that only the old real dates are returned for all rows. There has to be something wrong with the if statement, but I can´t figure it out.

Thank you in advance
Best regards

 

I know what the problem is...

I try to compare a Date variable to a String variable with the equals method. That´s why the if statement is never true... Can I cast Date to String in my Java Node? Or is there a equals method that compares a date variable to this specific date format?

Thank you in advance

Hi,

you can use equals on dates as well. However you need to be careful because it also will check the time and the timezone to be equal. For this you generate a date with your wished date information (e.g. new Date(30,11,0002))

Or you compare the strings before converting to date?

Best, Iris