Compare Dates

Six 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
Six 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
Six 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
Six 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
Six 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
Four Stars

Re: Compare Dates

Hi iam new to talend

 

i need to compare dates but iam getting error while using below qury in tmap

 

TalendDate.compareDate(load_src.acctr_eff_dt <= context.i_DATA_DATE && load_src.acctr_exp_dt >= context.i_DATA_DATE)?"Y":"N"

 

---- value of context.i_DATA_DATE=2018-12-31 

Employee

Re: Compare Dates

@shirisha

 

   Please try the below logic and I got the output as shown below.

 

image.png

image.png

 

The tMap expression used in the output column is as shown below.

 

(TalendDate.compareDate(row1.acctr_eff_dt,context.i_DATA_DATE)<=0 && TalendDate.compareDate(row1.acctr_exp_dt,context.i_DATA_DATE)>=0)?"Y":"N" 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

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

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog