how to format date field from 32/01/2014 will be parsed to 1.Feb 2014 (01/02/2014) correctly in knime

i have used java code in java snippet node but i am getting null values…How to resolve this?

Check below code for reference:

import java.util.;
import java.text.
;

public class DateFormat1
{
public DateFormat1() throws Exception
{
String dateString = “32/01/2014”;

SimpleDateFormat dateFormat = new SimpleDateFormat(“dd/MM/yyyy”);
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
}

public static void main(String argv) throws Exception
{
new DateFormat1();
}
}

@Prasanthsk,

The String To Date&Time node works really well for applications like this. I’m not sure if you have to keep it in a Java Snippet but I would recommend that you check out that node. https://hub.knime.com/knime/nodes/String_to_DateTime*sLTXYsycpYMwt_yW

2 Likes

@TardisPilot,
But got the below error when using “String To Date/time” node

Execute failed: Failed to parse date in row 'Row0: Text ‘32-01-2008’ could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32

You’ll need to use a “lenient” mode to parse such dates. I think the String to Date&Time node has no such option. So instead you’ll have to go back to using a Java Snippet node and enable the “lenient” flag for the DateFormat:

https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#setLenient-boolean-

public void setLenient(boolean lenient)

Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object’s format. With strict parsing, inputs must match this object’s format.

– Philipp

@qqilihq,
I am new to coding, Can u please point out in my code where can i put that lenient method.

After you create the formatter instance:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(true);
1 Like

@Prasanthsk

I think you may just have the node configured incorrectly. I have attached a workflow sample and a screenshot below.
KNIME_project4.knwf (11.2 KB)

@TardisPilot,
if we give input date value as 32/01/2014 ,it will result as execute failed…

@qqilihq,

When using java snippet , i am getting null values in resulted date column .Check below code for reference

You don’t need to declare a class or main method in the Java Snippet node. Just write the code after at the // expression start comment.

– Philipp

@qqilihq
Still getting null values, do i need to change anything in the code. How do i point out my “convertedDate” variable to new date column(date-mod)

Don’t redeclare convertedDate in the try scope – simply remove Date.

when removing date for convertedDate ,code shows error. Check in below image


Can u paste what modifications required in above code? i am unable to figured it out.

What’s the error message? Is an output variable with that name declared? (screenshot above suggests so)

Error in line 42: convertedDate cannot be resolved to a variable

This is the error i got.image
Is an output variable with that name declared?: i tried changing with different names also but it doesn’t work.

See the list at the end of the dialog, the variable name is given in the column “Java Field”.

1 Like

It got worked when i Changed “convertedDate” as JavaField value.

1 Like

You’re welcome.

1 Like

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