Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello,
I am trying to convert the string value "2019-11-11" to Date format using the following expression:
row9.Date!=null && !row9.Date.equals(" ") ? TalendDate.parseDate("EEE MMM dd hh:mm:ss:zzz yyyy",row9.Date) : null
My target date format is "EEE MMM dd hh:mm:ss:zzz yyyy".
Can anyone tell me what i am doing wrong.
So is the target data type a string also? So first you have to convert to a date type with:
TalendDate.parseDate("yyyy-MM-dd",row9.Date)
And take that result and convert it to your out format. Do that with the 'formatDate' function:
TalendDate.formatDate("EEE MMM dd hh:mm:ss:zzz yyyy",TalendDate.parseDate("yyyy-MM-dd",row9.Date))
The target data type is date with format EEE MMM dd hh:mm:ss:zzz yyyy
@lliMy target data type is date in format "EEE MMM dd hh:mm:ss:zzz yyyy". When i try to use the expression you provided it throws a compilation error cannot convert from string to date.
I am using the following expression in my tmap:
row4.date!=null && !row4.date.equals(" ") ? TalendDate.formatDate("EEE MMM dd hh:mm:ss:zzz yyyy",TalendDate.parseDate("yyyy-MM-dd",row4.date)) : null
I tested this and it will work if the in data type is a String, and the out data type is a String. But that must not be the case.
If the in value or the out value, or both are date data types then you will get the error you mentioned.
What the expression currently does is convert a String to a Date, and then to a String again, in the format you specify.
@lli Thank you.
Unfortunately, i have convert the string to date and the expression obviosuly doen't seem to work.
row4.date!=null && !row4.date.equals(" ") ? TalendDate.formatDate("EEE MMM dd hh:mm:ss:zzz yyyy",TalendDate.parseDate("yyyy-MM-dd",row4.date)) : null
Anyone has idea on how to fix this?
Can someone please take a look and suggest me the solution?
Thanks again.
You are wrong. The code works. Test it as follows. Create a job with one tJava component.
In the tjava put your code:
String in = "2019-11-11";
String out = in!=null && !in.equals(" ") ? TalendDate.formatDate("EEE MMM dd hh:mm:ss:zzz yyyy",TalendDate.parseDate("yyyy-MM-dd",in)) : null;
System.out.println(out);
The output of the job is:
[statistics] connecting to socket on port 3897
[statistics] connected
Mon Nov 11 12:00:00:CST 2019
[statistics] disconnected
The problem is not with this code, it's with your schema.
EDIT: You need to adjust your schema to match your code OR adjust your code to match your schema. All the data types must match for this field, through all the steps of your job unless you specifically convert it to another data type at some point.