on 09-20-2009 2:48 PM
Hi SDNers,
I have a requirement of adding two dates.
I have two input fields which contains date as input and mapped to a single output field.
The requirment for me is to add the two dates (from input) and pass to the output.
Can anybody help me getting this logic through UDF.
Thanks in Advance.
Regards
Jayaram.G
Hi try this code,
int date1 = 20091023;// first argumnet in UDF
int date2 = 11;//Second Argumnet in UDF
Date date = null;
String date1String = "" + date1;
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
date = df.parse(date1String);
} catch (ParseException pe) {
pe.printStackTrace();
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, date2);
String result = cal.getTime().toString();
return result;
I tested this code in eclipse working fine.
may be some small modifications required..
cheers,
Raj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI jaya,
Can you plz elaborate ur requirement.
I am not understanding what do u mean by two dates in a Input field and how do u want to handle in the output field.
Do u want to concatenate the dates in one single field or what?
Regards,
Rahul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rahul,
Thanks for your quick response.
Sorry the requirement that I had given previously is a little bit confusing.
My requirment is, in my input I have two fields, first field contains date(yyyymmdd) and second field contains days.
I have to add the second field days to the first field date and populate output.
Example: In my first field the input date coming in yyyymmdd format is 20090920 and in the second field the input is 15 (days).
The output that needs to be populated is 20091005.
Regards,
Jayaram.G
You can do this using Java Calender in your UDF...
Find code snippet at
http://snippets.dzone.com/posts/show/3966
http://www.rgagnon.com/javadetails/java-0101.html
-Siva Maranani
Hi,
Try this new code,previous code what i have given its give in string format,below code will give integer format.
import below packages in UDF
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
int date1 = 20091023;//FIRST ARGUMENT
int date2 = 11;/SECOND ARGUMENT
Date date = null;
final String date1String = "" + date1;
final DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
date = df.parse(date1String);
} catch (ParseException pe) {
pe.printStackTrace();
}
final GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, date2);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
final String strDate = dateFormat.format(cal.getTime());
int resultDate = new Integer(strDate).intValue();
return resultDate;
it works fine..
Regards,
Raj
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.