cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for getting previous month and year

Former Member
0 Kudos

Hi All,

We have a requirement where we need to pick a file for previous month on first day of every new month, where file name is "YY_MMUSD.tex"

For ex. on 1/09/2012 we have to pick a file for month of august i.e. 12_08USD.tex.

But the problem is when its first month of the year..i.e. on 01/01.2013 we should pick the file for month of December'12  12_12USD.tex.

Could you please suggest us how this can be achieved.

Accepted Solutions (1)

Accepted Solutions (1)

robertot4s
Active Participant
0 Kudos

Hi,

You can use the class java.util.Calendar in your UDF, for example:

Calendar calendar = Calendar.getInstance();

calendar.set(2013,0,1);


calendar.add(Calendar.MONTH, -1);  //Month before

calendar.get(Calendar.MONTH);

calendar.get(Calendar.YEAR);

Be careful because the month number starts at 0.

Best regards,

Roberto

Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ayura,

                        You need scripts to pick up the file from source folder( say /usr/A). The script goes through list of all files in a directory. It checks each file name in the directory and compares with current date. If the file satifies the condition you stated it moves ( not copy ) the file to a destination folder (say /usr/B). PI server picks up file only from /usr/B.   How you should write this script depends on the operating system of the PI server. If its linux/Unix system you can use shell scripts. You can also use java code to do all jobs but the java code has to executed from a script. You can request BASIS team to schedule running of this script once a month.

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Thanks for your reply.

But source is a coorporate directory and we cannot run a script there.Hence we are using extended receiver determination and transferring only the required file to the target side.

In extended receiver determination we are having difficulty while asigning a condition to the service level node according to which it should pick only the file of previous month.

As for jan'13 it should pick only one file out of all and i.e of dec'12 .

Hence year should also be decremented.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ayura,

             Below UDF provides you the exact filename expected by the scenario.

public static String selectFile()

{

  String s="";

  try

  {

  

   java.text.DateFormat dateFormat ;

   java.util.Calendar calendar = java.util.Calendar.getInstance();

   //calendar.set(2013,0,1);

   //System.out.println("current calendar time = " +  dateFormat.format(calendar.getTime()));

   if(calendar.get(java.util.Calendar.DAY_OF_MONTH)==1)

   {

    calendar.add(java.util.Calendar.MONTH, -1);

    //System.out.println("calendar one month back = " + dateFormat.format(calendar.getTime()));

    dateFormat = new SimpleDateFormat("yy");

    String yy=dateFormat.format(calendar.getTime());

    dateFormat = new SimpleDateFormat("MM");

    String mm=dateFormat.format(calendar.getTime());

    s=yy+"_"+mm+"USD.tex";

    //System.out.println(s);

   }

  

  }

  catch(Exception e)

  {

   e.printStackTrace();

  }

  return s;

}

I have left some lines as comment (in green) you can use them to test the code in your system. line 09 I have set calender date to 1st day of the year.

In line 11 I am checking if the day is first day of the month or not. Thus the UDF will provide you with correct filename only on 1st of the month else it will provide blank values. the default value can be set on line 03.

Regards

Anupam

Former Member
0 Kudos

that is simple by using standard function like substring,subtract,fixed value....

for ex: have to refer below logic:

above one is example for how to handel month on ur requirement only...

Message was edited by: S Sankar

Former Member
0 Kudos

Hi Sankar,

Thanks for your reply.

But this is not working when the year will be changed for ex for 01/01/2013..in that case it will not work.

Also we have files from year 1998 present on server for each month .

And we need to pick files only from one month prior to current month.

Former Member
0 Kudos

before one logic for yyyy/mm/dd

but u have to try below one: for ex if ur request is 01/01/1998 then op is 97_12

Also we have files from year 1998 present on server for each month

datetrans: