Compare Dates in RunIf condition

Six Stars

Compare Dates in RunIf condition

Hi -

 

I have a vendor that does not allow us to move or remove files from their file location once we process them.  I don't make the rules people.

 

I need to basically only process files that were created after the last time I loaded a file into my db.  I'm storing the last load date in a global variable globalMap.get("lastDate") from the database.  Then I'm iterating through file properties and storing the mtime from tFileProperties in global variable globalMap.get("fileDate") with this tJavaRow:

globalMap.put("fileDate", TalendDate.formatDate("MM-dd-YYYY HH:mm:ss",new java.util.Date(input_row.mtime)));

 

I want to have a flow to "do nothing" if the file date is prior to last load date and then a flow to process the file if the file date is > last load date.

 

I can't get the conditions in the Run If links to function properly.

 

This is the syntax I'm using on the conditions for the 2 flows:

 

((Date)globalMap.get("fileDate")).compareTo(((Date)globalMap.get("lastDate")))<=0

((Date)globalMap.get("fileDate")).compareTo(((Date)globalMap.get("lastDate")))>0

 

I get the following error:

Exception in component tJavaRow_1
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

 

However - everything works fine if I disable the 2 flows so I know the problem isn't in the tJavaRow - it's in the Run If links.

 

Any thoughts on what I'm doing wrong?

 

tJava_Error.PNG

Thanks,
Darin

 


Accepted Solutions
Community Manager

Re: Compare Dates in RunIf condition

The error message tells you (nearly) everything you need to know....

 

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

 

You are setting the "fileDate" globalMap to a String when you use formatDate. Why are you doing this? You do not need to format a date to compare it. Dates are stored as numbers, formatting them is ONLY for representing them to humans. If you are wanting to compare them, don't worry about the format and just compare them.


All Replies
Community Manager

Re: Compare Dates in RunIf condition

The error message tells you (nearly) everything you need to know....

 

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

 

You are setting the "fileDate" globalMap to a String when you use formatDate. Why are you doing this? You do not need to format a date to compare it. Dates are stored as numbers, formatting them is ONLY for representing them to humans. If you are wanting to compare them, don't worry about the format and just compare them.

Six Stars

Re: Compare Dates in RunIf condition

Thank you for the clarification.

 

Clearly I wouldn't have done something incorrectly had I known that I was doing so...that's kind of the point of this forum, no?  I now have a better understanding of the usage of globalMap, formatDate because of this error.  Where I come from - that's learning and that, I assume, is the purpose of this community.  

 

Thank you for your time.

 

Community Manager

Re: Compare Dates in RunIf condition

I just reread my post. I think I came across a little harsher than I had intended when I wrote it. Sorry if you got the wrong impression and that my written English came across a little curt. #RememberNotToPostOnHereWhenReallyTired 

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Why Companies Move to the Cloud: 7 Success Stories

Learn how and why companies are moving to the Cloud

Read Now