Changing Date by days

Hello KNIME community,

I have a date in a column that is of the format 2019-07-16 and need to update the date by 10 days. And it needs to the do the same to all the cell values in the column. Is there a node to do this ? or are there other ways to do this (Java snippet)?

First, I will have to check the value of a column. and if that check yields a value of TRUE then I have to increment the date column by 10 days else I have increment the value by 14 days.

I have tried a Java snippet with the following code in it but no luck.
// Your custom imports:
import java.io.StringReader;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input columns
/** Input column: “VEHICLE UNIT DESC” /
public String c_VEHICLEUNITDESC;
/
* Input column: “Day of Month of Last_IGN” /
public Integer c_DayofMonthofLast_IGN;
/
* Input column: “LAST IGN DATE_Arr[0]” */
public LocalDate c_LASTIGNDATE_Arr0;

// Fields for output columns
/** Output column: “Days_Till_Dormant” */
public LocalDate out_Days_Till_Dormant;

// Your custom variables:
public String UnitGen;
public boolean isFound;
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
UnitGen = c_VEHICLEUNITDESC;
DateFormat dateFormat = new SimpleDateFormat (“yyyy-MM-dd”);
Calendar cal= c_LASTIGNDATE_Arr0;
if(isFound=UnitGen.contains(“GEN 10”) == true)
{
out_Days_Till_Dormant =c_LASTIGNDATE_Arr0.add(Calendar.DATE,10);
}

1 Like

Use Column Expression with logic like below
if (field == constant) {plusTemporal(Date, 10)}
else {plusTemporal(Date, 14)}

2 Likes

I don’t see this Node. How do I get it?

Install it. From menu - Help/Install New Software. Or download/open any example with this node and you’ll be asked to install it.

Hi @Adiwakar -

Check out our page on the Hub about the Column Expressions node. That node has its own extension that you’ll need to download, as @izaychik63 mentioned. If you drag the node from the Hub page into your KNIME workflow, you’ll be prompted to download and install that extension. Then you just need to restart KNIME, and the node should be available to you.

We’ve been trying to make the extension installation process more user friendly, so feedback is welcome!

1 Like

@ScottF and @izaychik63 Thank you for your suggestions. I was able to find and install the node.

I was able to get to this point
a=column(“Gen Version”)
If (a = 11)
{column(“LAST IGN DATE_Arr[0]”)(Date,14)}
else
{column(“LAST IGN DATE_Arr[0]”)(Date, 10)};

But the node is telling me that it expects a operand in place of the else condition.
not sure what’s wrong here.

Hi,

You have to use double equal signs:
if (a == 11)

And I think you are missing something in the bodies:
{column(“LAST IGN DATE_Arr[0]”)(Date,14)}

{column(“LAST IGN DATE_Arr[0]”)(Date, 10)}

:blush:

@armingrudd thanks for helping. But there seems to be something that I am doing wrong. (a == 11) did not work. I got a null pointer exception.
So I have a column called Gen Version. If the value in that column is 11 then I want to take the date in column called “Date of last IGN” and increase it by 14. Else Increase it by 10.

This is what I tried. (Did not work. )
a=column(“Gen Version”)
if (a ==11)
{column(“Date of last IGN”)(Date,14)}
else
{column(“Date of last IGN”)(Date,10)}

I think this is what @izaychik63 suggested

if (a = 11) {plusTemporal(Date, 10)}
else {plusTemporal(Date, 14)}

But what is plusTemporal? and how do I specify the date from column Date of last IGN?

I have not worked with this function and also do not know what actually is going on here, but if the date column is “Date of last IGN” then the expression should be like this:

a=column(“Gen Version”)
if (a ==11)
plusTemporal(column("Date of last IGN"), periodOfDays(14))
else
plusTemporal(column("Date of last IGN"), periodOfDays(10))

:blush:

Edited: Dear @Adiwakar, you have to use the plusTemporal and also convert the number to a period object. The expression above now works fine. It is just an example, you may need to change the numbers to get your desired output.

1 Like

Hi there @Adiwakar,

I would use more KNIME approach in this matter cause there is Date&Time Shift node which does exactly this. All you need is a Rule Engine node with your logic that will create new column containing 14 or 10 after which you can apply above mentioned node.

Check it out here:
https://kni.me/w/cwLLTvC1X59BgfGZ

Br,
Ivan

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.