cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to increment date

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (3)

Answers (3)

anupam_ghosh2
Active Contributor
0 Kudos

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

Ryan-Crosby
Active Contributor
0 Kudos

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

iaki_vila
Active Contributor
0 Kudos

Hi Anitha,

Have you checked this thread ?

Regards.

Former Member
0 Kudos

yes,i have checked but they have using current date.i want to declare in variable increment value coming from source