One Star

compare 2 dates and sort them

Hello everyone ,
i'm trying to compare 2 dates "fact_date" and "discard_date" , the main topic is that the fact_date must became first (ex:fact-date:16/062016 then the discard_date 17/062016) , so first i made a var named comp and the type is int { TalendDate.compareDate(row1.Fact_Date,row1.Discard_date) } then i tried to ,based on those results, decide if i keep the first date in fact_date , and the second in discard_date or switch them (the second date in fact_date and the first one in discard_date) , there is the second part :
 i created a second var named keep typed Object
Var.comp > 0 ? fact_alim.date1=row1.Discard_date && fact_alim.date2=row1.Fact_Date : fact_alim.date1=row1.Fact_Date && fact_alim.date2=row1.Discard_date 
Multiple markers at this line
- Syntax error on token "=", <= expected
- The operator <= is undefined for the argument type(s) java.util.Date, 
java.util.Date
- Syntax error on token "=", <= expected
- The operator <= is undefined for the argument type(s) java.util.Date, 
java.util.Date
- Syntax error on token "=", <= expected
- The operator <= is undefined for the argument type(s) java.util.Date, 
java.util.Date
so i tried this syntax:
Var.comp > 0 ? fact_alim.date1==row1.Discard_date && fact_alim.date2==row1.Fact_Date : fact_alim.date1==row1.Fact_Date && fact_alim.date2==row1.Discard_date 
no error , but no results neither , so i changed the type of keep from object to int and changed it to :
Var.comp > 0 ? fact_alim.date1==row1.Discard_date && fact_alim.date2==row1.Fact_Date && Var.keep==1: fact_alim.date1==row1.Fact_Date && fact_alim.date2==row1.Discard_date && Var.keep==0 
and i got: Type mismatch: cannot convert from boolean to int
what should i do ?nothing works ..
6 REPLIES
One Star

Re: compare 2 dates and sort them

You do this in tMap. Can you put some screenshots?
Lin LIN
Consultant BI/Talend
Synaltic Group
Fifteen Stars

Re: compare 2 dates and sort them

You need to understand comparisons in Java. In your examples you are comparing objects rather than values. For example, consider an object a box that holds a value. You might have two boxes that are different that hold the same value. Comparing the boxes would be different to comparing the values they hold. 
Comparing dates adds another level of complexity because dates go down to milliseconds. A human will say that 13/01/2016 is the same as 13/01/2016. But a computer may well hold 13/01/2016 12:45:12.111 and 13/01?2016 12:34:13.222. These are not the same dates. So you need to make sure you know what level of date you want to compare to and then make adjustments to the dates to either wipe out other parts of the date (set them to 0) or only compare the parts of the date that need comparing. 
Take a look at the java.util.Date type for more information on this. If you are happy comparing the whole date (down to millisecond), take a look at this...http://www.tutorialspoint.com/java/util/date_compareto.htm
Rilhia Solutions
One Star

Re: compare 2 dates and sort them

@louislinlin yes i'm doing this in Tmap those screen will explain 
@rhall_2.0 in the screen i'm comparing the date as a "dd/MM/yyyy" format making all the date in this format also specifying it in the parameters of comparetodate
PS:FactureDacte=fact_Date
    FactureDateDecharge=discard_date
Fifteen Stars

Re: compare 2 dates and sort them

What are you getting with the compareDate method? You haven't said what is wrong with that. It should work. Comparing dates using "==" I would not expect to work as you wish.
Rilhia Solutions
One Star

Re: compare 2 dates and sort them

the compareDate method will return an int (-1,0,1) it depends of the date less equals or bigger , about comparing dates using "==" it's because of the : The operator <= is undefined for the argument type(s) java.util.Date, 
java.util.Date
- Syntax error on token "=", <= expected
when i used a "=" , what do i have to do ?
Fifteen Stars

Re: compare 2 dates and sort them

"==" checks for equality in the object. For example, is it the exact same object. Not the contents of the object. I explained this above. 
I don't understand what is wrong with the compareDate method. That will tell you if the date is less than (-1), equal to (0) or greater than (1). 
Rilhia Solutions