Compare Dates

Four Stars

Compare Dates

Hello,
how do I compare two dates?
I would like something like this in my tMap:
TalendDate.compareDate(TalendDate.getDate("dd-MM-yyyy"), row1.service_date, "dd-MM-yyyy") > 0 ? TalendDate.getDate("dd-MM-yyyy") : row1.service_date

where row1.service_date is a date, and I save the result to columns of type date.
But I still writes error "Exception in thread" main "java.lang.Error: unresolved compilation problems"
Community Manager

Re: Compare Dates

The method TalendDate.getDate("dd-MM-yyyy") returns a string, not a Date. Try this expression:
TalendDate.compareDate(TalendDate.getCurrentDate(), row1.service_date)>0?TalendDate.getCurrentDate(): sOrders.serviced_at


Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Four Stars

Re: Compare Dates

I'm constantly throwing error:
 
Four Stars

Re: Compare Dates

Hi,
If you test in tMap for your incoming data, then this error may come... whether same error appears after executing complete job?
Vaibhav
Four Stars

Re: Compare Dates

TalendDate.compareDate(TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")), row1.service_date, "yyyy-MM-dd") > 0 ? TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")) : row1.service_date 

When I run it out of tMap, then is not error.
But comparison is not correct. I have always found it assigns the current date and not older.
Four Stars

Re: Compare Dates

Hi,
I have checked with fixedflowinput-->tJavaRow and date set to 2014-06-01 and 2014-08-01 with following code
System.out.println(TalendDate.compareDate(TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")), input_row.service_date, "yyyy-MM-dd") > 0 ? TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")) : input_row.service_date);
This works... i get expected results with those values...
Thanks
Vaibhav
Four Stars

Re: Compare Dates

My date in the stream is in the format DD-MM-YYYY, but for comparison, I set the pattern "yyyy-MM-dd" 
I do not know why that compares first at the level of the day, then months, and finally to year. . 
How should I have a set pattern in the stream compared to me right?
Or what could be wrong?
Four Stars

Re: Compare Dates

You will have to stick to the incoming date format.... for comparison, talend will not care what format you have... Set any standard format and you get proper results... Metadata must match.
Vaibhav
Four Stars

Re: Compare Dates

When I have everything in the same format "dd-MM-yyyy", as well as to compare it first to the day level, at the level of the month, and eventually to the level a year. For example, the date1 is 15-05-2010 and date2 is 15-01-2014, as earlier is evaluated date2.
Format "dd-MM-yyyy" for stream I got like a "Retrieve schema" from database connection.
Community Manager

Re: Compare Dates

TalendDate.compareDate(TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")), row1.service_date, "yyyy-MM-dd") > 0 ? TalendDate.parseDate("yyyy-MM-dd", TalendDate.getDate("yyyy-MM-dd")) : row1.service_date 

When I run it out of tMap, then is not error.
But comparison is not correct. I have always found it assigns the current date and not older.

TalendDate.getDate() returns the current date, do there exists service_date in your input data bigger than current date? if not, it is normal with this expression, it always output the current date.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business