Six Stars

Compare two dates

hi Team,

 

i wanted to compare two date columns coming from two tables 

i used TalendDate.compareDate(date1,date2) != 0 as it is returning an integer value this is not expected result

getting null pointer exception in tmap, please help on it.

 

Regards,

Meena

 

1 ACCEPTED SOLUTION

Accepted Solutions
Six Stars

Re: Compare two dates

@manodwhb, @rhall_2_0

Thanks for your inputs my issue has been fixed, i am very thankful to you for giving me prompt response

row1.date1 == null && row2.date2 == null ? false :
(
row1.date1!= null && row2.date2!= null ?
(
TalendDate.compareDate(row1.date1,row2.date2)==0? false: true)
:true), i used this condition in tmap

This fixed my issue i compared with nulls too. 

once again Thank you all

 

Regards,

Meena

23 REPLIES
Fifteen Stars

Re: Compare two dates

What is the expected result? Can you show me the tMap settings?

 

Rilhia Solutions
Six Stars

Re: Compare two dates

i need result like dd-MM-yyyy formate

Six Stars

Re: Compare two dates

In tmap output side filter, i gave like
TalendDate.compareDate(row1.date1,row2.date2) != 0
if result is equal i should not load anything
if result not equal i should load row2.date2
date1 nulls are expected, am comparing null=date2, i feel that can be the reason am getting null pointer exception in tmap, but could not able to solve it its very urgent i need to complete this asap
Fifteen Stars

Re: Compare two dates

OK, in your tMap filter you need.....

TalendDate.compareDate(row1.date1,row2.date2) != 0

.....the nulls are handled by the routine method.

 

I'm guessing that your tMap output schema has the date column set to not nullable. I suspect that row2.date2 is null when it fails. However I will need to see the tMap config to tell if this is true

 

 

Rilhia Solutions
Twelve Stars

Re: Compare two dates

@muralam,first handale the nulls for bothe dates and comprare so that you will not get null pointer exception.

Manohar B
Fifteen Stars

Re: Compare two dates

@manodwhb nulls are handled by the method. 

 

    /**
     * compare two date
     *
     * @param date1 (first date)
     * @param date2 (second date)
     * @param pattern (compare specified part, example: "yyyy-MM-dd")
     * @return the result wheather two date is the same, if first one less than second one return number -1, equlas
     * return number 0, bigger than return number 1. (can compare partly)
     *
     * {talendTypes} Integer
     *
     * {Category} TalendDate
     *
     * {param} date(myDate) date1 : the first date to compare
     *
     * {param} date(myDate2) date2 : the second date to compare
     *
     * {param} String("yyyy-MM-dd") pattern : compare specified part
     *
     * {examples}
     *
     * ->> compareDate(2008/11/24 12:15:25, 2008/11/24 16:10:35) return -1
     *
     * ->> compareDate(2008/11/24 16:10:35, 2008/11/24 12:15:25) return 1
     *
     * ->> compareDate(2008/11/24 12:15:25, 2008/11/24 16:10:35,"yyyy/MM/dd") return 0 #
     */
    public static int compareDate(Date date1, Date date2, String pattern) {
        if (date1 == null && date2 == null) {
            return 0;
        } else if (date1 != null && date2 == null) {
            return 1;
        } else if (date1 == null && date2 != null) {
            return -1;
        }

        if (pattern != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String part1 = sdf.format(date1), part2 = sdf.format(date2);
            return (part1.compareTo(part2) >= 1 ? 1 : (part1.compareTo(part2) <= -1 ? -1 : 0));
        } else {
            long time1 = date1.getTime(), time2 = date2.getTime();
            return (time1 < time2 ? -1 : (time1 == time2 ? 0 : 1));
        }
    }
Rilhia Solutions
Twelve Stars

Re: Compare two dates

yes @rhall_2_0,then he shoul dnot get null pointer exception in tmap while comparing.it coud be other filed that he needs to handle nulls in tmap.

Manohar B
Six Stars

Re: Compare two dates

Yes both date columns are nullable column I am trying to compare null with date value
Fifteen Stars

Re: Compare two dates

Can you send us the complete nullpointerexception error?

Rilhia Solutions
Six Stars

Re: Compare two dates

Hi All,

Now am not getting the error null pointer exception, but am not able to compare date columns properly

i have written the logic as below, the attached screen shot, am comparing two tables on below date columns which can be null, if result set is false like null!=datevalue i have to update my target table with date

 

out.DEV_INT_BIRTH_DATE!= null && row1.DEV_INT_BIRTH_DATE != null ?(TalendDate.compareDate(out.DEV_INT_BIRTH_DATE,row1.DEV_INT_BIRTH_DATE)==0 ?false : true) :true 

 

Regards,

Meena

Six Stars

Re: Compare two dates

It depends on if it's a primitive type or not. You have to use Integer.equalsTo(Integer) if its an Integer type. "==" is for a primitive type of int.

Six Stars

Re: Compare two dates

Integer.intValue() == 0. In all these cases Integer is your comparison result.

Six Stars

Re: Compare two dates

But these are date columns I didn't get your input compare integer
Fifteen Stars

Re: Compare two dates

Can you show your whole tMap please? I believe your problem is with your configuration, but I can't tell unless I can see your whole tMap. By the way, there is no need for you to write the code you have included above. This code...

TalendDate.compareDate(out.DEV_INT_BIRTH_DATE,row1.DEV_INT_BIRTH_DATE)==0

....will return true if the columns match (even if both are null....which is possibly where your problem arrises) or false if they do not. Nulls are handled here. This is all you require for your logic as to whether an update is required. If you can read Java, take a look at the routine yourself. I have posted the code in a message above.

Rilhia Solutions
Six Stars

Re: Compare two dates

@rhall
yes i have to handle nulls, may be i have to compare
null=datevalue
null=null
null=empty, when i compare as these are nullable columns, nulls are expected, which i am not getting the result as expected,
my expected result i wanted to compare null=date load date value, null=null,null=emptyfield load nothing pass null, finally i wanted to pass data when there is a date value or else null, kindly help me out.
Six Stars

Re: Compare two dates

The same above mentioned scenario i am able to load data in case of strings where i have written a small routine to compare the required columns to find updates

 

@rhall_2_0, i have given complete tmap settings

 

Regards,

Meena

Six Stars

Re: Compare two dates

Hi,

 

Any update on the below request, as it is little urgent

 

Regards,

Meenas

Twelve Stars

Re: Compare two dates

@muralam,please findthe attahced smaple job to compare dates.

Manohar B
Fifteen Stars

Re: Compare two dates

OK, please consider this as FACT, not fake news. This method will work and take into consideration nulls as well as dates.....

TalendDate.compareDate(out.DEV_INT_BIRTH_DATE,row1.DEV_INT_BIRTH_DATE)==0

 So, if your values are null and null, it will return 0. If your dates are the same (down to the millisecond) it will return 0. If your dates are different or one is a null, then it will return either 1 or -1 depending on which date is null or greater.

 

If you are getting a nullpointerexception I guarantee that it is NOT to do with this method. This is just confusing the issue. 

 

Can you please take a screenshot of your whole tMap. NOT small sections. I need to see ALL of it. Can you also add a tLogRow before the tMap (for both inputs, Main and Lookup) and post the data here so that we can see what is going into your tMap? You will also get an error message with the nullpointerexception. Can you post the exact error.

 

 

Rilhia Solutions
Twelve Stars

Re: Compare two dates

Yes @rhall_2_0,that condition wil work as expected and i have designed the job based on that condition and shared with him.

Manohar B
Fifteen Stars

Re: Compare two dates

That's great, but we clearly do not know what the problem is. The information supplied has not been sufficient to identify whether the date comparison has been a red herring or not. My suspicion is that the output filter logic is flawed. But this can only be identified if we get the information. 

 

Rilhia Solutions
Six Stars

Re: Compare two dates

@manodwhb, @rhall_2_0

Thanks for your inputs my issue has been fixed, i am very thankful to you for giving me prompt response

row1.date1 == null && row2.date2 == null ? false :
(
row1.date1!= null && row2.date2!= null ?
(
TalendDate.compareDate(row1.date1,row2.date2)==0? false: true)
:true), i used this condition in tmap

This fixed my issue i compared with nulls too. 

once again Thank you all

 

Regards,

Meena

Fifteen Stars

Re: Compare two dates

All you need is this....

TalendDate.compareDate(row1.date1,row2.date2)==0? false : true

....this is redundant....

row1.date1 == null && row2.date2 == null ? false :
(
row1.date1!= null && row2.date2!= null ?
Rilhia Solutions