on 11-08-2011 10:44 AM
Hi Group,
I am using the below UDF for returning multiple values by passing two string inputs,its working fine in eclips ,but in function library
to return multiple values we need to have result list for this I have to take inputs as queue instead of single value,when I am taking
inputs as queue parse method is not taking,please suggest.
package Sap_MyFunctions;
import java.util.*;
import java.io.*;
import java.text.*;
import com.sap.aii.mappingtool.tf3.rt.Container;
import com.sap.aii.mappingtool.tf3.rt.ResultList;
import com.sap.aii.mappingtool.tf3.rt.ResultListImpl;
public class Getdates {
public static void indate(String a,String b,ResultList result,Container container)throws Exception{
List <Date> dates = new ArrayList<Date>();
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = a;
String end_date = b;
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
long interval = 24*1000*60*60;
long endTime = endDate.getTime();
long curTime = startDate.getTime();
while (curTime <= endTime){
dates.add(new Date(curTime));
curTime+=interval;
}
for(int i=0;i<dates.size();i++){
Date iDate = (Date)dates.get(i);
String ds = formatter.format(iDate);
String ds1 = ds.substring(0, 10);
result.addValue(ds1);
//result.addContextChange();
//System.out.println("Dates are...." + ds1);
}
}
public static void main(String[] arg0)throws Exception{
String str_date1 = "2010-08-29 00:00:00";
String end_date1 = "2010-09-01 00:00:00";
ResultListImpl result = new ResultListImpl();
Container container = null;
Getdates getdates;
indate(str_date1,end_date1,result,container);
String output = result.toString();
System.out.println(output);
}
}
its solved myself,as I was giving input in the wrong format,after correcting it works fine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
Since using the context type of UDF automatically translates the parameters to
public static void indate(String[] a,String[] b,ResultList result,Container container)throws Exception{}
You will need to loop your code
for(int j=0; j<a.length; j++){
List <Date> dates = new ArrayList<Date>();
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = a[j];
String end_date = b[j];
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
long interval = 24*1000*60*60;
long endTime = endDate.getTime();
long curTime = startDate.getTime();
while (curTime <= endTime){
dates.add(new Date(curTime));
curTime+=interval;
}
for(int i=0;i<dates.size();i++){
Date iDate = (Date)dates.get(i);
String ds = formatter.format(iDate);
String ds1 = ds.substring(0, 10);
result.addValue(ds1);
//result.addContextChange();
//System.out.println("Dates are...." + ds1);
}
}
}
Hope this helps,
Mark
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Mark,
previously I tried this ,but I am getting the below error:
unreported exception java.text.ParseException; must be caught or declared to be thrown
Date startDate = formatter.parse(str_date);
unreported exception java.text.ParseException; must be caught or declared to be thrown
Date endDate = formatter.parse(end_date);
Regards,
Rajiv ^
Hello,
You need to enclose the
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
in a try-catch statement
try{
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
}
catch(java.text.ParseException e){
//Your exception here
}
Hope this helps,
Mark
In addition to Mark's suggestion you might have to use try catch block for the formatter class.
Example:
try{
for(int j=0; j<a.length; j++){
List <Date> dates = new ArrayList<Date>();
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = a[j];
String end_date = b[j];
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
long interval = 24*1000*60*60;
long endTime = endDate.getTime();
long curTime = startDate.getTime();
while (curTime <= endTime){
dates.add(new Date(curTime));
curTime+=interval;
}
for(int i=0;i<dates.size();i++){
Date iDate = (Date)dates.get(i);
String ds = formatter.format(iDate);
String ds1 = ds.substring(0, 10);
result.addValue(ds1);
//result.addContextChange();
//System.out.println("Dates are...." + ds1);
}
}
}catch(Exception ex){
}
Hi Rajiv,
I did some changes to the code you are using. Use advanced UDF of cache type "CONTEXT" and not "QUEUE".
public void indates(String[] a,String[] b,ResultList result,Container container){
try {
java.text.SimpleDateFormat formatter;
formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = a[0];
String end_date = b[0];
java.util.Date startDate = formatter.parse(str_date);
java.util.Date endDate = formatter.parse(end_date);
long interval = 24*1000*60*60;
long endTime = endDate.getTime();
long curTime = startDate.getTime();
while(curTime <= endTime)
{
String ds = formatter.format(new java.util.Date(curTime));
String ds1 = ds.substring(0, 10);
curTime+=interval;
result.addValue(ds1);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}//pls don't copy this last closing brace. This is already there in UDF by default.
No need of any import statement. I beleive you have only two inputs to the UDF thus I am considering only first two values of the arrays a and b within the UDF. In case your input is array of strings with multiple values of a and b , just modify the UDF as shown below
public void indates(String[] a,String[] b,ResultList result,Container container){
try {
java.text.SimpleDateFormat formatter;
formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=0;i<a.length;++i)
{
String str_date = a<i>;
String end_date = b<i>;
java.util.Date startDate = formatter.parse(str_date);
java.util.Date endDate = formatter.parse(end_date);
long interval = 24*1000*60*60;
long endTime = endDate.getTime();
long curTime = startDate.getTime();
while(curTime <= endTime)
{
String ds = formatter.format(new java.util.Date(curTime));
String ds1 = ds.substring(0, 10);
curTime+=interval;
result.addValue(ds1);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}//pls don't copy this last closing brace. This is already there in UDF by default.
Now just map this UDF to target field whose minimum occurence is 1 and max occurence is unbounded.
Values will get auto populated once you run this code. Also ensure that root nodes of source and target have been properly mapped.
Hope this solves your problem.
regards
Anupam
Edited by: anupamsap on Nov 9, 2011 3:52 PM
User | Count |
---|---|
92 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.