cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to change the date format to required format

former_member387652
Participant
0 Kudos

Hi Experts,

The requirement is input will be an array of dates which might be of any format like DD/MM/YYYY ,  YYYY/MM/DD , DD-MM-YY,  YY-MM-DD , etc something like these. Now I would like to write an UDF which accepts the input  array of dates with whatever may be the input format & convert it to a format DD-MM-YYYY. Please help me to achieve the same. Thanks!

Rgds,

Naveen.

Accepted Solutions (1)

Accepted Solutions (1)

apu_das2
Active Contributor
0 Kudos

Hi Naveen,

If you are not able to force the business to stick with one date format to send then try to find out what are the different date formats they are going to send and add validation for each date format in your UDF.

Though this is not a good design as whenever new date format will come apart from those you have covered in your UDF its going to give you an error.

UDF will be something like -

Suppose business is going to send these 4 date formats - DD/MM/YYYY ,  YYYY/MM/DD , DD-MM-YY,  YY-MM-DD

So first check the pattern-

String returnDate = null;

if(date.indexOf("/")!=-1)    //date is your input date in UDF

   {

       String arr = date.split("/");

       if(arr[0].length = 2)

           returnDate = arr[0] + "-" + arr[1] + "-" + arr[2];

      

      else if(arr[0].length = 4)

           returnDate = arr[2] + "-" + arr[1] + "-" + arr[0];

  }


Using this you will not be able to differentiate the difference between DD-MM-YY and  YY-MM-DD. So fot these 2 your business need to send complete YYYY .

Thanks,

Apu



Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

                  Please ask the business to define the format of the date else it is very difficult to understand from digits whether that is day , month or year.

Secondly are these dates related to current system date.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Actually there are different senders & each sender is represented by a folder in File system. No, these dates are not related to current system date. These dates will always be earlier/past dates which are lesser than current date.

Thanks,

Naveen.

anupam_ghosh2
Active Contributor
0 Kudos

So you mean format of date sent by each sender will differ from each other. That is format of date is fixed for a specific sender but depend on who is sending the date. In such case can you get format of date for each sender.

Regards

Anupam

former_member387652
Participant
0 Kudos

Hi Anupam,

Yes exactly, the date format is different from each other & is fixed for each sender. If I get to know the format of each sender then can I code an UDF to solve this?

Thanks,

Anupam.

former_member387652
Participant
0 Kudos

Dear Anupam,

As Inaki suggested it difficult to interpret the date until the pattern is known.So calendar setting need to done in the actual source system.

Rgds,

Nithin.

former_member190293
Active Contributor
0 Kudos

Hi Naveen!

Yes, if you know the date format for each sender and can identify sender in your mapping - you will be able to format date.

Regards, Evgeniy.

anupam_ghosh2
Active Contributor
0 Kudos

yes then definitely issue can be resolved.

Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Naveen,

Sender knows the format of the date it is going to send to PI.

Then you need an UDF to change the format of date when you are sending to receiver.

Regards

Anupam

former_member387652
Participant
0 Kudos

Dear Anupam,

Yes right. Thanks for ur time!

-Naveen.

iaki_vila
Active Contributor
0 Kudos

Hi Naveen,

You can do it with the DateTrans function. Check this links:

Regards.

former_member387652
Participant
0 Kudos

Hi Inaki,

Thanks for your time! Actually the input date format is not constant but it will be dynamic.I mean the input format might be anything it doesn't remain constant or expected. Since the source of the data is from different senders in which all of them might not follow the same date format and it might keep changing. I believe in date transform function we could only change to required format only if we know the input date format right? Thanks!

Rgds,

Naveen.

iaki_vila
Active Contributor
0 Kudos

Hi Naveen,

Do you have the pattern in another field?, i mean if you receive the data 10/01/2016 how can assure if you have the day one of October or the ten day of January?

Regards.

former_member387652
Participant
0 Kudos

Hi Inaki,

Input file does not contain pattern field. Yes you are right.

Thanks,

Naveen.