Six Stars

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 Smiley Happy

1 ACCEPTED SOLUTION

Accepted Solutions
Employee

Re: Syntax error while trying to calculate a date

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

 

 

image.png

 

 

Warm Regards,

 

Nikhil Thampi

6 REPLIES
Seven Stars

Re: Syntax error while trying to calculate a date

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");
Employee

Re: Syntax error while trying to calculate a date

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

Six Stars

Re: Syntax error while trying to calculate a date

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 Smiley Sad

Employee

Re: Syntax error while trying to calculate a date

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

 

 

image.png

 

 

Warm Regards,

 

Nikhil Thampi

Six Stars

Re: Syntax error while trying to calculate a date

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

 

Thanks Smiley Happy

Employee

Re: Syntax error while trying to calculate a date

Hi,

 

     No problem at all. Happy to help.

 

     Enjoy Talend :-)

 

Warm Regards,

 

Nikhil Thampi