cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for Date Funtions

Former Member
0 Kudos

Hi all,

I need help for one mapping concept i.e.

we will get souce side one date (DD/MM/YYY) for that we hv to add some days and generate target date .

so, can any one tell me how to do this.....with udf...

Thanks & Regards,

Mahesh.

Accepted Solutions (1)

Accepted Solutions (1)

aashish_sinha
Active Contributor
0 Kudos

Hi,

I think you need a simple udf like this:

where "a" is the input parameter that should match the date format of the dateFormat object.

imports: java.util.Calendar;java.text.SimpleDateFormat;java.text.DateFormat;

public String addOneDay(String a,Container container){

String notifyDates = a;

Calendar calendar = Calendar.getInstance ( ) ;

DateFormat dateFormat = new SimpleDateFormat ( "yyyy-MM-dd" ) ;

try {

Date date = dateFormat.parse ( notifyDates ) ;

calendar.setTime ( date ) ;

calendar.add ( Calendar.DATE, 1 ) ; // Add here that how many days u wanna add

return dateFormat.format ( calendar.getTime ( ) ) ;

} catch ( Exception e ) {

return "error";

}

}

regards

Aashish Sinha

PS : reward points if helpful

former_member238432
Discoverer
0 Kudos

Very Helpful:)

Regards,

Subhashini

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Raj,

i was tryed ur 2 solutions.

in 1st solution whatever date i will provide as soruce its giving 20081026 like this

for 2nd solution, i got this error.

Source code has syntax error: /usr/sap/XID/DVEBMGS01/j2ee/cluster/server0/./temp/classpath_resolver/Map34838900155211dd806d00093d14d008/source/com/sap/xi/tf/_MM_IT0009_.java:69: cannot resolve symbol symbol : variable date location: class com.sap.xi.tf._MM_IT0009_ int ip_date = Integer.parseInt(date); ^ 1 error

plz help me to resolve this.

Thanks & Regards

Former Member
0 Kudos

Mahesh,

Raj's UDF adds a specified number of days from the current date. So in your case , you have to initialize the Calendar object with the Date fields[year,month and day] that you are passing as input. Believe this is possible using the Calendar.set() method.Do check.

Thanks,

Anand

Former Member
0 Kudos

Hi Mahesh,

1) inputValue is your DATE value I consider it for number of values.It works for single value also.

2) Do mapping as follows,

inputValue>RemoveContext> UDF>SplitByValue>TargetField

Following the code for UDF.

String[] splitCodes = null;

for(int i = 0; i < inputValue.length; i++)

{

if( !(inputValue<i>.equals("")))

{

splitCodes = iDVal<i>.split("/");

int posVal = 0;

if(posVal < splitCodes.length)

splitCodes[posVal] = splitCodes[posVal] + "10"

result.addValue(splitCodes[posVal]+ "/" + splitCodes[1] + "/" + splitCodes[2]);

}

}

Instead of 10 you can use data which you want to add as days.

Regards ,

Rohit.

Reward points if helpful.

Former Member
0 Kudos

Hi Mahesh,

you can customize following UDFs according to your requirement.

//Add current date with required days(180) and convert the date output to string format and return it   

// Declarations 
int date, 
     days_added=180, 
     day, 
     month, 
     year; 

Calendar cal; 

// Get current date 
cal = Calendar.getInstance(); 

// Add required days to current date 
cal.add(Calendar.DATE, days_added); 

// Convert date format to string format 
day = cal.get(Calendar.DATE); 
month = cal.get(Calendar.MONTH)+ 1; 
year = cal.get(Calendar.YEAR); 

date = year * 10000 + month * 100 + day; 

return (""+date);

or

string in YYYYMMDD as input and similar format string in output 



// Declarations 
int ip_date = Integer.parseInt(date); 
int days_added=180; 
Calendar cal; 

// Read day, month and year from input string 
int day = ip_date % 100; 
int month = (ip_date/100) % 100 - 1; 
int year = ip_date / 10000; 

// Convert the above to date format and add the no of days 
cal = Calendar.getInstance(); 
cal.set(year, month, day); 

cal.add(Calendar.DATE, days_added); 

day = cal.get(Calendar.DATE); 
month = cal.get(Calendar.MONTH)+ 1; 
year = cal.get(Calendar.YEAR); 
ip_date = year * 10000 + month * 100 + day; 
return (""+ip_date); 

Hope it solves your problem

Regards

Raj