Skip to Content
avatar image
Former Member

Adding 12 business hours to the standard time

Hi All.

Iam currently implementing a rule, where the incoming date to XI is in the format.

YYYY-MM-DDT00:00:00Z

I want to output the date adding 12 business hours.

YYYY-MM-DDT12:00:00Z

IP: Eg: 2011-06-10T14:00:00

OP: 2011-06-11T02:00:00

Any thoughts on this please?

Thankyou.

Kiran

Edited by: kiran kumar on Jun 10, 2011 9:12 AM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Jun 10, 2011 at 07:41 AM

    Hello Use this UDF.

    here argument x;

    int h=x.getHours();

    x.incrementHours(12); if date does not increment then use

    x.incrementDate(1);

    return x;

    I have juss given the idea. Use it.

    Regards

    Pothana

    Edited by: Pothana Yadav on Jun 10, 2011 10:19 AM

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 10, 2011 at 11:24 AM

    Hi Kiran,

    I have assumed that the input date to the udf is valid. Say for example I assume that you are not going to receive date like this one "2001-11-31T14:01:55Z" which is invalid. Then the following UDF is sufficient for your requirement

     
    
    public class twelve_hour { 
    
    public static String udf (String s) 
    { 
    String t=null; 
    try 
    { 
    int yyyy,mm,dd,hh,l; 
    int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
    
    l=s.length(); 
    String r; 
    yyyy=new Integer(s.substring(0,4)).intValue(); 
    mm=new Integer(s.substring(5,7)).intValue(); 
    dd=new Integer(s.substring(8,10)).intValue(); 
    hh=new Integer(s.substring(11,13)).intValue(); 
    r=s.substring(13,l);//rest of string 
    
    if(hh+12<=23) 
    { 
    hh=hh+12; 
    t=s.substring(0,11)+hh+r; 
    return t; 
    } 
    if(mm==2) 
    { 
    if(dd==28) 
    { 
    if((yyyy%400==0)  || (yyyy%4==0 && yyyy%100!=0)) 
    { 
                dd=29; 
    } 
    else 
    { 
    mm++; 
    dd=1; 
    } 
    } 
    else 
    { 
    if(dd==29) 
    { 
    mm++; 
    dd=1; 
    } 
    else 
    { 
    dd=(dd+1); 
    } 
    } 
    } 
    else 
    { 
    dd=(dd+1)%days_in_month[mm]; 
    if(dd==1) 
    { 
          mm=(mm+1)%12; 
    } 
    if(mm==1) 
    { 
    yyyy++; 
    } 
    
    } 
    t=Integer.toString(yyyy)+"-"; 
    if(mm<10) 
    { 
    t+="0"; 
    } 
    t+=Integer.toString(mm)+"-"; 
    if(dd<10) 
    { 
    t+="0"; 
    } 
    t+=Integer.toString(dd)+"T"; 
    hh=(hh+12)%24; 
    if(hh<10) 
    { 
    t+="0"; 
    } 
    t+=Integer.toString(hh)+r; 
    } 
    catch(Exception e) 
    { 
    e.printStackTrace(); 
    } 
    return t; 
    } 
    
    public static void main(String[] args) { 
    
    String s[]= 
    { 
    "2011-06-10T12:00:00Z", 
    "2000-02-28T12:00:00Z", 
    "2001-02-28T12:00:00Z", 
    "2001-12-31T13:01:55Z", 
    "2001-07-31T14:01:55Z" 
    }; 
    int l=s.length,i; 
    for(i=0;i<l;++i) 
    { 
    System.out.println(s<i>+" :after 12 hours: "+udf(s<i>)); 
    } 
    } 
    
    } 
    

    Just you need to copy the code written within function udf and provide as input the date string. The output I received after running entire code is shown here below

    2011-06-10T12:00:00Z :after 12 hours: 2011-06-11T00:00:00Z

    2000-02-28T12:00:00Z :after 12 hours: 2000-02-29T00:00:00Z

    2001-02-28T12:00:00Z :after 12 hours: 2001-03-01T00:00:00Z

    2001-12-31T13:01:55Z :after 12 hours: 2002-01-01T01:01:55Z

    2001-07-31T14:01:55Z :after 12 hours: 2001-08-01T02:01:55Z

    This appears to be correct.

    Please test this from your end and revert. In case you feel that the input dates PI server is receiving may not be valid, then I need to upgrade this code so that target structure will receive error message if the date is wrong.

    Just after I saw Sunil's code, I feel his code is smarter and shorter.

    I was not sure if imports would work in PI thus I wrote it without imports.

    Hat's off Sunil for this nice code.

    regards

    Anupam

    Edited by: anupamsap on Jun 10, 2011 1:26 PM

    Edited by: anupamsap on Jun 10, 2011 3:35 PM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 10, 2011 at 12:29 PM

    Hi,

    I think you can easily achive this using the Graphical mapping using the SPlit by value concept, add 12 to the hrs and then concatenate the value and send this to OP.

    The other way around is using a UDF which will do the same thing, The UDF will split the value when it enounter "T" and then add 12hrs to the value and then concatenate.

    Hope this helps.

    Cheers,

    Jay

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 10, 2011 at 12:59 PM

    Hi Kiran,

    Why dont you try this simple udf ..

    import java.util.Calendar; java.text.ParseException; java.text.SimpleDateFormat;

    public static String addHours(String date) {
    int hrsToadd = 12;               
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    try {
    	cal.setTime(sdf.parse(date));
    	cal.add(Calendar.HOUR, hrsToadd);
    } catch (ParseException e) {
    	e.printStackTrace();
    }  
    return sdf.format(cal.getTime());
    }

    Hope this helps.

    Regards,

    Sunil Chandra

    Add comment
    10|10000 characters needed characters exceeded