Skip to Content
avatar image
Former Member

How to skip weekend dates

Hi all,

In one of my MM, I am getting a date in MM/DD/YY format (after some validations) and then the requirement says, if this date is any of the weekday, then pass the same. But if the date is a weekend (SAT and SUN) then get the date of following Monday.

Ultimately the target date should be a weekday.

Can you help me with some UDF example.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Dec 05, 2008 at 12:40 PM

    if possible, can you send me the UDF code using those functions.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Murly,

      You can try with this function.

      import java.text.SimpleDateFormat;

      import java.util.Calendar;

      import java.util.Date;

      public String validateDate(String date){

      try{

      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");

      SimpleDateFormat sdfDayName = new SimpleDateFormat("EEEE");

      Calendar ca = Calendar.getInstance();

      ca.setTime(sdf.parse(date));

      if (sdfDayName.format(ca.getTime()).equals("Saturday") || sdfDayName.format(ca.getTime()).equals("Sunday")){

      ca.add(Calendar.DATE,1);

      }

      return sdf.format(ca.getTime());

      }catch(Exception ex){

      System.out.println(ex.toString());

      }

      return date;

      }

      I hope this helps you.

      BRegards

      Ivan

  • Dec 05, 2008 at 12:32 PM

    To clarify a bit on above reply, there are certain java classes which could be used to access the date related operations.

    java.util.Calendar;java.text.SimpleDateFormat;java.text.DateFormat;

    From there you may perform operations like accessing day for any date. Then using standard function, you may check whether the day is Sunday or Saturday. If it is, then pass monday.

    Regards,

    Prateek

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 06, 2008 at 05:05 AM

    Hi murli,

    Try this it will work, while mapping to target mao it with SplitbyValue function;

    Source -> udf -> splitbyvalue - >target

    import : java.text.SimpleDateFormat;java.util.Calendar;java.lang.;java.util.GregorianCalendar;java.util.;java.text.*;java.util.Date;java.text.DateFormat;

    int co = a[0].length();

    String[] expDate = new String[co];

    Date[] dat = new Date[co];

    Date[] bar = new Date[co];

    int[] st = new int[co];;

    try

    {

    for(int i=0;i<co;i++)

    {

    dat<i> = new SimpleDateFormat("dd/MM/yyyy").parse(a<i>);

    Calendar cal = new GregorianCalendar();

    cal.setTime(dat<i>);

    st<i> = cal.get(Calendar.DAY_OF_WEEK);

    if((st<i> == 2) || (st<i> == 3) || (st<i> == 4) || (st<i> == 5) || (st<i> == 6))

    {

    bar<i> = cal.getTime();

    expDate<i> = new SimpleDateFormat("MM/dd/yyyy").format(bar<i>);

    result.addValue(expDate<i>);

    }

    else if((st<i> == 1) && (st<i> == 7))

    {

    result.addValue(ResultList.SUPPRESS);

    }

    }

    }

    catch(Exception ex)

    {

    System.out.println(ex);

    }

    regards,

    Murugavel

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 05, 2008 at 12:13 PM

    Use the DAY_OF_WEEK functionality of Calendar class.

    Add comment
    10|10000 characters needed characters exceeded

  • Dec 05, 2008 at 01:55 PM

    Hi,

    Please see the code

    String sDate = "12/07/08";
    		
    			SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yy");
    			Date dt = sd.parse(sDate);
    
    			Calendar cal = new GregorianCalendar();
    			cal.setTime(dt);
    			
    			int day = dt.getDay();
    			
    			if (day == 0 )
    			cal.add(dt.getDate(), +1);		
    
    			if(day == 6)
    			cal.add(dt.getDate(), +2);
    				
    			System.out.println("Date is "+ sd.format(cal.getTime()));
    			
    			 	

    Add comment
    10|10000 characters needed characters exceeded