cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to compare the dates with the input date

Former Member
0 Kudos

Hi,

Help me in writing an UDF to compare the input date with start date and end date.

I had written following UDF, but it is not working. we are on XI 3.0

try {
                      
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date sd = df.parse(StartDate);
            Date id = df.parse(InputDate);
            Date ed = df.parse(EndDate);
 
            if ((sd.before(id) || (sd.equals(id))) && (ed.after(id)) || (ed.equals(id))) {
          return true;                
            }
else{
                return false;
            }
        }     catch (Exception e){
       e.printStackTrace();
	}

Thanks

Srinivas

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Thanks for all your answers,

I did it with standard function 'compare dates'.

Thanks

Srinivas

Answers (2)

Answers (2)

sugata_bagchi2
Active Contributor
0 Kudos

Hi Srinivas,

have you checked the graphical mapping tool for date? probably there you will easily get it done and you can also create your date pattern there. along with this use the if-else it will work.

you will have the datecompare, datebefore, dateafter , currentdate functions there.

Thanks

Sugata B Majumder

Former Member
0 Kudos

What do you mean by start day and end date? Can you provide some example of your requirement.

Former Member
0 Kudos

I need to check the input date is with in the range of 2 dates (start date and end date) in the payload.

Thnaks

Srinivas

anupam_ghosh2
Active Contributor
0 Kudos

Hello Srinivas,

did you import this two classes in your UDF ? java.text.* and java.util.*

In case you did but the program is still not working I just wrote a code without imports. See if this might help. I have tested this with all type of data please test again from your end. I have assumed that if three dates are equal the function return true. I am not sure whether you wanted to exclude the boundary dates. In case you want to exclude those dates just replace '<=' with '<' symbol. The code could have been smarter but in short time I thought this might help.

public class DateRange {

static boolean compareDate(String startDate,String inputDate,String endDate)

{

int j,k,l;

String s[],i[],e[];

s=startDate.split("-", 3);

i=inputDate.split("-", 3);

e=endDate.split("-", 3);

j=s[0].compareTo(i[0]);

k=i[0].compareTo(e[0]);

l=s[0].compareTo(e[0]);

if(l>0|| j>0 || k>0)

{

return false;

}

if(j<=0 && k<0)

{

return true;

}

j=s[1].compareTo(i[1]);

k=i[1].compareTo(e[1]);

l=s[1].compareTo(e[1]);

if(l>0|| j>0 || k>0)

{

return false;

}

if(j<=0 && k<0)

{

return true;

}

j=s[2].compareTo(i[2]);

k=i[2].compareTo(e[2]);

l=s[2].compareTo(e[2]);

if(l>0|| j>0 || k>0)

{

return false;

}

if(j<=0 && k<=0)

{

return true;

}

return false;

}

public static void main(String[] args) {

String startDate="1992-04-03";

String inputDate="1992-02-03";

String endDate="1992-02-03";

System.out.println(compareDate(startDate,inputDate,endDate));

}

}

Plese let me know if this works.

regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Srinivas,

unfortunately the 2nd line within compareDate function is not printed properly.

it should be String s[],i[],e[]; In case if this also misprints empty third brackets must follow s and i just like e.

Regards

Anupam

Former Member
0 Kudos

I need to check the input date is with in the range of 2 dates (start date and end date) in the payload.

Thnaks

Srinivas

Have you tried with std date functions.

I think if change the date format in yyyymmdd and use the "greater" and "less" function along with "IF" then it should work. Just try it and us know.

For example: IF "input date" is greater than "start date" and less than "end date" then map it. And while mapping the input date you can change back the format according to your need by using DateTrans function.