cancel
Showing results for 
Search instead for 
Did you mean: 

UDF help required

Former Member
0 Kudos

Hi,

I need to check my input date format. We need to check if the input is of dd.MM.yyyy or dd/MM/yyyy then convert it into yyyy-MM-dd format.

And if some unknown format comes we need to pass the current system date.

Can you help me in this.

TIA

Accepted Solutions (0)

Answers (3)

Answers (3)

iaki_vila
Active Contributor
0 Kudos

Hi Prebhas,

Also, you can create  this UDF to check the format:

And finally the code:

public String validDate(String dateInput, String dateFormat, Container container) throws StreamTransformationException{  

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

    Date testDate = null;

    try

    {

      testDate = sdf.parse(dateInput);

    }

    catch (java.text.ParseException e)

    {

   return "0";

    }

    if (!sdf.format(testDate).equals(dateInput))

    {

    return "0";

    }

     // if we make it to here without getting an error it is assumed that

    // the date was a valid one and that it's in the proper format

   return "1";

}

And the how to use:

(1 = true, 0 = false)

Regards.

zameerf
Contributor
0 Kudos

Hi Prabhas,

>>need to check my input date format. We need to check if the input is of dd.MM.yyyy or dd/MM/yyyy then convert it into yyyy-MM-dd format.

If you are sure you would get only these two formats, then you can try the below logic. It is just a work around, not the exact way.

>>And if some unknown format comes we need to pass the current system date.

But say if you get some input like MM/dd/yyyy (07/10/2012), how would you particularly differentiate the day and month from the input? it can be assumed of format dd/MM/yyy as well right?

It is better to discuss with sender system and decide on one particular date format.

iaki_vila
Active Contributor
0 Kudos

Hi Prabhas,

You don't need an UDF. You can use standard fuctions:

Current date: to obtain the system date.

DateTrans: to transform the date

Regards

Former Member
0 Kudos

Thanks for the reply.. But for date trans function we always need to specify the input dateformat.

I dont have any control of what i am gonna receive.But looking at the examples i have been receiveing  constantly.

they are mostly of dd.mm.yyyy or dd/mm/yyyy.. So i need to validate the input format is one among the above mentioned n then i need to convert it.

If some other format comes pass sysdate

Ryan-Crosby
Active Contributor
0 Kudos

Hi Prabhas,

I would do it slightly differently only because SimpleDateFormat doesn't necessarily validate that the input date is a valid one.  I would have these four imports in the UDF:

import java.util.Date;

import java.util.Calendar;

import java.text.SimpleDateFormat;

import java.util.regex.*;

Then I would use a combination of regular expressions to make sure the input parameter matches the expected input format and also that numbers specified in the date are valid.  For instance if you got a value where 32.12.2011 was input and you parsed it with SimpleDateFormat it would return a valid date of 2012-01-01 in your expected format.  Here is the code below that I would use to check all contexts of the input date are valid:

The reason there is a call to Calendar getTime() within the try block is because with the combination of leniency set to false above it will throw the IllegalArgumentException in the case that the date is invalid.  That allows the regular expression to be much more generic and not have to validate days in a month or leap year for instance.

Regards,

Ryan Crosby