cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Userdefined Date Format function

Former Member
0 Kudos

Hi,

I am trying to write a User defined function for the addition of days in mapping editor. But the problem is that Java accepts only string format but not date format,

Can anybody help me out in solving this problem.

Accepted Solutions (0)

Answers (9)

Answers (9)

Former Member
0 Kudos

Thanka a lot

Former Member
0 Kudos

Hi all,

my problem has been solved anyway.

its like this:

I have taken my own date format in the form of string and the divided into differnt sunstrings based on ur format u can have positions for year,month and day .

then parse it using Simple Date format or Use GregorianCalendar to convert that to calendar format.

then using Calendar.add(CALENDAR_DAY_OF_YEAR,X), we can add "x" no. of days.

for Difference between two dates

Use

date1.getTime()-date2.getTime()

The code is as follows:

GregorianCalendar yourStaticDate = new GregorianCalendar(1864, Calendar.DECEMBER, 31);

Date d1=yourStaticDate.getTime();

int p=Integer.parseInt(a.substring(0,4));

int q=Integer.parseInt(a.substring(5,7));

int r=Integer.parseInt(a.substring(8,10));

q--;

Date d2= new GregorianCalendar(p,q,r).getTime();

long x=d2.getTime() -d1.getTime();

long y= (x/(10006060*24));

return y+"";

It really works.

Thanks and Regards,

Kalpana

Former Member
0 Kudos

Hi all,

My problem is yet to be solved.

I am trying out a mapping in which I could add some days to a given date which returns a date , and the other thing I want to Subtract two given dates which returns an integer.I have been trying out this using Simple date format but continuosly being encountered by errors.

Can u please help me in solving this.

henrique_pinto
Active Contributor
0 Kudos

kalpana,

my code above will help you doing the add part (you need some string treatment before the UDF or even inside it). The same function can be used to substract a defined number of days, if you use negative values for the number of days to add.

For subtracting two dates, you'll need to implement some logic.

Specifically, you'll need an iterative method which converges to the value you want.

You could, for example, use something like:

...
Calendar cal = new GregorianCalendar(a, b, c);
Calendar cal2 = new GregorianCalendar(d, e, f);
int inc = 100;
int dif = 0;
while (!cal2.equals(cal)) {
  if (cal2.before(cal)) {
    cal2.add(Calendar.DATE, inc);
    dif +=inc;
  } else {
    cal2.add(Calendar.DATE, -inc);
    dif -= inc;
  }
  inc = (inc + 1) / 2;
}
return dif;
...

In this case, I've used an algorithm which converges quadratically (instead of the linear convergence of a for method with increment of 1 at each iteration). Of course, it will be as faster as closer the initial guess for the difference (in this case, 100) is to the actual value. So you should provide a value around which you expects the difference to be (10, 100, 1000) etc.

One last thing I forgot to mention in my last post.

The month variables (b, e) represent the month and may assume the values {0, 11}. So, for example, if your month input is "1", it refers to february and not to january. Just subtract 1 for the month variables in the Calendar object instantiations to get the proper month.

Regards,

Henrique.

henrique_pinto
Active Contributor
0 Kudos

If I got it correct, you want to create a function that will receive a date and a number of days to be added and receive new date, is that so?

For example, input1 = 25/05/2007, input2 = 10 => output = 04/06/2007.

If that is it, try the following code:

...
SimpleDateFormat form = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = new GregorianCalendar(a, b, c);
cal.add(Calendar.DATE, d);
return(form.format(cal.getTime()));
...

where your inputs are like:

a = year

b = month

c = day

d = days to add.

Regards,

Henrique.

Former Member
0 Kudos

Kalpana,

Can you tell us what you are tring to do in the UDF? Also tell us your requirement?

---Satish

Former Member
0 Kudos

hi,

we have standard date function

DateTrans : Converts date format I to another date format

Use it other wise you can achieve it in UDF by converting the imput into date and format it and change to string assign to target

Regards

Chilla

Former Member
0 Kudos

Hi you can try this out also

java.util.Date d = new java.util.Date();

java.text.SimpleDateFormat f

= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

return f.format(d) ;

Mudit

Award points if it helps

Former Member
0 Kudos

Hi

If you want to convert string to date format, use the dateTransform function

check the following link

http://help.sap.com/saphelp_nw04/helpdata/en/43/c4cdfc334824478090739c04c4a249/content.htm

regards

krishna

award points if helpful

Former Member
0 Kudos

hi;

Can you be more clear on what date format you require and what date format you are getting in

Mudit