Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Mayot
Contributor III
Contributor III

Syntax error while trying to calculate a date

Hello,

 

I'm trying to extract dates by inserting them into variables context. Everything works as long as it's just insertion.

But I would like to make a calculation: remove 3 months to the date and store it in another variable with tJavaRow and tDBOracleInput (Date pattern "dd/MM/yyyy")

 

my tJavaRow :

context.MaxDateCreation = input_row.MAX_DATECREATION;
context.MaxDateSoldeCompta = input_row.MAX_DATESOLDECOMPTA;
context.MaxDateSoldeCompta_3M = TalendDate.addDate("context.MaxDateSoldeCompta","dd/MM/yyyy",-3,"MM");
System.out.println(context.MaxDateCreation);
System.out.println(context.MaxDateSoldeCompta);
System.out.println(context.MaxDateSoldeCompta_3M);

But I get this error : Error : type mismatch: cannot convert from String to Date

 

I tried this :

TalendDate.addDate(TalendDate.parseDate("context.MaxDateSoldeCompta","dd/MM/yyyy"),"dd/MM/yyyy",-3,"MM");

Error : The method addDate(String, String, int, String) in the type TalendDate is not applicable for the arguments (Date, String, int, String)

 

So I tried this

TalendDate.addDate(TalendDate.parseDate("context.MaxDateSoldeCompta","dd/MM/yyyy").toString(),"dd/MM/yyyy",-3,"MM");

But I fall back on the initial error 😕

 

So my last try was this :

context.MaxDateSoldeCompta_3M = TalendDate.parseDate(TalendDate.addDate("context.MaxDateSoldeCompta","dd/MM/yyyy",-3,"MM"),"dd/MM/yyyy");

Exception in component tJavaRow_1
java.lang.RuntimeException: dd/MM/yyyy can't support the date!

 

My output of my others variables without calculation:

Output 
Wed Aug 01 01:44:43 CEST 2018
Tue Jul 31 00:00:00 CEST 2018

Datatype of my variables is 'Date' (without others specification).

 

What is the correct synthax to get what I want ?

 

Thanks 0683p000009MACn.png

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

Hi,

 

       My first assumption is that you are having the context variable context.MaxDateSoldeCompta in date format.  In that case, you will have to use the expression as below to get the 3 month older date.

 

TalendDate.addDate(context.input_date,-3,"MM") 

 

 

0683p000009Lz4E.png

 

 

Warm Regards,

 

Nikhil Thampi

View solution in original post

6 Replies
Anonymous
Not applicable

You're passing a string literal to the addDate method. You should remove the double quotes.

context.MaxDateSoldeCompta_3M = TalendDate.addDate(context.MaxDateSoldeCompta,"dd/MM/yyyy",-3,"MM");
Anonymous
Not applicable

Hi,

 

     You re giving the context variable itself in double quotes as "context.MaxDateSoldeCompta". So instead of consumed as a variable, it is consumed as a String.

 

Warm Regards,

 

Nikhil Thampi

Mayot
Contributor III
Contributor III
Author

When I remove the quote "", I had the 2nd error :

Error : The method addDate(String, String, int, String) in the type TalendDate is not applicable for the arguments (Date, String, int, String)

 

I can't see where the problem comes from, other than the format of the date

 

Wed Aug 01 01:44:43 CEST 2018   

This kind of output look very special, however in my DB it display like that "dd/MM/yy"

 

I also tried - instead of /, yy instead of yyyy but always same issue 0683p000009MPcz.png

Anonymous
Not applicable

Hi,

 

       My first assumption is that you are having the context variable context.MaxDateSoldeCompta in date format.  In that case, you will have to use the expression as below to get the 3 month older date.

 

TalendDate.addDate(context.input_date,-3,"MM") 

 

 

0683p000009Lz4E.png

 

 

Warm Regards,

 

Nikhil Thampi

Mayot
Contributor III
Contributor III
Author

OMG it was so simple. And when I think I was trying to convert my Date format into a String

 

Thanks 0683p000009MACn.png

Anonymous
Not applicable

Hi,

 

     No problem at all. Happy to help.

 

     Enjoy Talend 🙂

 

Warm Regards,

 

Nikhil Thampi