I have a workflow where two simultaneous files are being read, and in each node stream, I have two Java Snippet nodes that correctly pull the workflow execution time.
I am trying to replace both Java Snippet nodes with a single Java Edit Variable node (that I can later call on as a variable) to further rationalize the workflow since the timestamps are going to be the same.
Attached code and workflow.
// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;
// Your custom imports:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZoneId;
// imports for input and output fields
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for output flow variables
/** Output flow variable: "testdate" */
public String out_testdate;
// Your custom variables:
private Date systemTime;
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
try {
// Use the system time for all rows
LocalDateTime localDateTime = LocalDateTime.ofInstant(systemTime.toInstant(), ZoneId.of("America/New_York"));
// Convert LocalDateTime to string format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
out_testdate = localDateTime.format(formatter);
// Set the flow variable "testdate" with the formatted date value
flowVariables.put("testdate", out_testdate);
// Print or use the formatted date as needed
System.out.println(out_testdate);
} catch (Exception e) {
// Handle exceptions (print or log the error, set a default value, etc.)
e.printStackTrace();
}
// expression end
}
}
Current error in this code is:
Error in line 47: flowVariables cannot be resolved
If I try:
setFlowVariable("testdate", out_testdate);
Then I get:
Error in line 47: The method setFlowVariable(java.lang.String, java.lang.String) is undefined for the type JSnippet
Many thanks in advance
Java Edit Variable.knwf (24.2 KB)