Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Save $650 on Qlik Connect, Dec 1 - 7, our lowest price of the year. Register with code CYBERWEEK: Register
cancel
Showing results for 
Search instead for 
Did you mean: 
muralam
Creator
Creator

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

 

Labels (2)
1 Solution

Accepted Solutions
muralam
Creator
Creator
Author

@manodwhb, @rhall

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

View solution in original post

23 Replies
Anonymous
Not applicable

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

 

muralam
Creator
Creator
Author

i need result like dd-MM-yyyy formate

muralam
Creator
Creator
Author

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
Anonymous
Not applicable

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

 

 

manodwhb
Champion II
Champion II

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

Anonymous
Not applicable

@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));
        }
    }
manodwhb
Champion II
Champion II

yes @rhall,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.

muralam
Creator
Creator
Author

Yes both date columns are nullable column I am trying to compare null with date value
Anonymous
Not applicable

Can you send us the complete nullpointerexception error?