on 05-19-2014 5:42 PM
Hi,
I have to increment date field by 3.Date field is going to be my input from source str.
I have written UDF in which date picks up current date.Its working fine.below is my UDF.
//write your code here
String DATE_FORMAT = "yyyyMMdd";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DATE,3);
result.addValue(sdf.format(c1.getTime())+"");
Please let me know how to declare variable in this so that increment happens as per my source value
Hi Anitha,
Please try this UDF
public static String convertDate(String d)
{
String convertedDate="";
int increment=3;
java.text.DateFormat targetFormat = new java.text.SimpleDateFormat("yyyyMMdd");
java.util.Date date=null;
date=new java.util.Date();
try
{
date=targetFormat.parse(d);
long sec;
sec=date.getTime()+ increment*24*60*60*1000;
date.setTime(sec);
}
catch(Exception e)
{
e.printStackTrace();
}
convertedDate = targetFormat.format(date);
return convertedDate;
}
output
input => output
---------------------------------------
20141231 => 20150103
20140129 => 20140201
20140707 => 20140710
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anitha,
Try out the following before c1.add:
int year = Integer.parseInt(DATE_FORMAT.substring(0,4));
int month = Integer.parseInt(DATE_FORMAT.substring(4,6));
int day = Integer.parseInt(DATE_FORMAT.substring(6,8));
c1.set(year, month - 1, day);
The month value is decremented because it is zero based in the Java implementation. These lines should take care of your requirement.
Regards,
Ryan Crosby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
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.