Format Date

Highlighted
Seven Stars

Format Date

I am passing a date value to a global variable and then trying to print it to the screen using tJavaRow:

System.out.println(globalMap.get("MaxInsertDateTime"));

 

and this prints fine - here is the output:

2019-07-15T03:46:02

 

However what I would like to do is format the value to "YYYY-MM-DD" format. I tried multiple different ways to do it but no luck so far:

System.out.println(TalendDate.formatDate("YYYY-MM-DD",(globalMap.get("MaxInsertDateTime"))));

 

Error:

Detail Message: The method formatDate(String, Date) in the type TalendDate is not applicable for the arguments (String, Object)

 

I also tried this:

System.out.println((String)globalMap.get("MaxInsertDateTime"));

 

Error:

java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String

 

What am I doing wrong here?

 

My goal is to format the date as "YYYY-MM-DD" and then use it later in tMSSQLInput component in a where condition. 

Highlighted
Nine Stars

Re: Format Date

Hi,

 

The message is telling you what's wrong :

 

The method formatDate(String, Date) in the type TalendDate is not applicable for the arguments (String, Object)

Java doesn't know that your globalMap.get("MaxInsertDateTime") object is actually a date, so it takes it as an Object and cannot use formatDate on an Object.

 

 

You have to explicitly tell that globalMap.get("MaxInsertDateTime") is a Date (assuming it *is* a date of course).

 

If you setup your global variable correctly, this should work :

 

System.out.println(TalendDate.formatDate("yyyy-MM-dd",(Date)globalMap.get("MaxInsertDateTime")));

btw: Talend corrects the misuse of upper/lower case in the date format but I strongly suggest you to use the proper case (see SimpleDateFormat for the detail)

 

Regards

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog

Downloads and Trials

Test drive Talend's enterprise products.

Downloads