Simple Date Arithmetic

I need to do some simple date arithmetic.  I need to add "X" days to a timestamp.  It seems like the easiest way to accomplish this would be to use a Python or Java snippet. 

First, I've tried to use a Python script.  But, the import in the script fails on the code: "from datetime import date" with a "datetime" not recognized error. 

Next, I tried doing this with a java snippet.  However, the java snippet seems to treat the timestamp column as a string, not a datetime cell object, and I can't get access to the calendar clone I need.

Any suggestions on how to best accomplish this simple task?

Thanks!

I'm going to answer my own post here . . . Looking back it seems like that questions about date arithmetic get asked from time to time and there are seldom any posts describing how to do it easily.  So here is how I solved the problem.

In the end I used a Python script node.  As I noted in the problem statement I wasn't able to load the Python datetime module to do the arithmetic but. . . these seem to actually be Jython nodes . . . so I was able to load the java calendar class (even though I could never load it from a java snippet node . . . go figure).  Using the Calendar class allows me to do all the date minipulation I need to do.

Another problem is that the Python node does not allow you to append a "date" format column.  One work around would be to write the final date out as a formatted string.  Instead, I appended a date column before the Python node and then modified the values.

Hope this is useful for someone:)

Hi Lyle,

here is additionally a way to do it with the Java Snippet.

You can use the DateandTimeCell directly with this code. Anyway the output is a string and has therefore to be transformed to the with the String to Date/Time node afterwards.

DateFormat format =
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date dt = new Date();
try{
 dt = format.parse($Date and time$);
//add 5 days
dt.setTime(dt.getTime()+5 * 24  * 60 * 60 * 1000);
}
catch(Exception e){}
return format.format(dt);

 

One last comment, based on which type of DateAndTimeCell you are using (e.g. only time and ms, or only day) you have to change the dateformat.

Here's the list of the possible formats.

  /** Formatter if only date is available. */
   DateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");

    /** Formatter if only date and time are available. */
   DateFormat FORMAT_DATE_AND_TIME = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    /** Formatter if date, time and ms are available. */
   DateFormat FORMAT_DATE_AND_TIME_AND_MS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");

    /** Formatter if only time is available. */
     DateFormat FORMAT_TIME =  new SimpleDateFormat("HH:mm:ss");

    /** Formatter if only time and ms are available. */
   DateFormat FORMAT_TIME_AND_MS =   new SimpleDateFormat("HH:mm:ss.S");

 

Best, Iris