One Star

Date comparison issue - Date.before and Date.after

Hi,
In a lookup expression I am using the date checking code :-
row4.END_TIME_DT != null && row2.date_2.before(row4.END_TIME_DT)
&& row4.START_TIME_DT != null && row2.date_2.after(row4.START_TIME_DT)
This works great if the row2.date_2 is after row4.START_TIME_DT but not if it is EQUAL to.
All dates are in mm/dd/yyyy hh:mm:ss format.
My issue is how do I test for row2.date_2 'equal to' or 'greater than' row4.START_TIME_DT in the expression??
One Star

Re: Date comparison issue - Date.before and Date.after

Hi,
Thius may not be pretty but it wqorks as a solution :-
row4.END_TIME_DT != null && (row2.date_2.before(row4.END_TIME_DT) || row2.date_2.equals(row4.END_TIME_DT))
&& row4.START_TIME_DT != null && (row2.date_2.after(row4.START_TIME_DT) || row2.date_2.equals(row4.START_TIME_DT))
One Star

Re: Date comparison issue - Date.before and Date.after

You can also use the method compareTo of the Date object.
For instance, if date1 = "01-JAN-2009" and date2 = "02-JAN-2009" then date1.compareTo(date2) will be equal to -1.
If date1 = "02-JAN-2009" and date2 = "02-JAN-2009" then date1.compareTo(date2) will be equal to 0.
If date1 = "03-JAN-2009" and date2 = "02-JAN-2009" then date1.compareTo(date2) will be equal to 1.
Then to get a test if a date is after or equal to another, you can test
`if (date1.compareTo(date2) >=0)`

Arnaud

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog