Java Edit Variable to output Date String

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.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
        } catch (Exception e) {
            // Handle exceptions (print or log the error, set a default value, etc.)

// 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)

@qdmt there currently is no flow variable type for date and time variables. You will have to convert to string and then back if you want:

1 Like

I’m trying to output a date in string format within the java node.

Hey qdmt,

two things to fix in the code, then it’ll work:

  1. Initialize the systemTime variable.
  2. Remove flowVariables.put - it’s sufficient to assign the local variable.

Here’s the gist of the code - more is not needed:

// imports
import java.time.*;
import java.time.format.*;

// expression
var localDateTime = LocalDateTime.ofInstant(, ZoneId.of("America/New_York"));
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
out_testdate = localDateTime.format(formatter);            



@qdmt I can offer up these examples:

1 Like

Super thanks @qqilihq - that did work!

Many thanks as well @mlauber71 - my initial solution used the Java Snippet, but I was trying to opt for a variable-based approach so I can use one node as opposed to two (i.e., in my case, there are two data streams in the workflow that require workflow time stamping). Though, I am realizing now I could potentially just use the Java Snippet’s variable output port to feed a second stream.

Cheers both - if I could mark both as a solution, I would :stuck_out_tongue:

1 Like

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