code for creating Routine

Highlighted
Five Stars

code for creating Routine

Hi,

 

I have scenario where I have to get date difference between start date and end date, and there is a string column with Segment where I will get two values ex: xyz, zxy, and if the segment is xyz then the date difference should exclude sunday, saturday and if the segment value is zxy then it should exclude only sunday. Can any one help with the code in creating a routine for this


Accepted Solutions
Five Stars

Re: code for creating Routine

Below is the code for DateDiff excluding Saturday and Sunday, works fine for my requirement

 

public class days {

static int day_of_week;
public static int getNumberOfDaysWorked(java.util.Date beginDate,java.util.Date endDate){
int val = 0;
while (beginDate.before(endDate)){
day_of_week=TalendDate.getPartOfDate("DAY_OF_WEEK", beginDate);
if(day_of_week != 1 && day_of_week != 7){// 1 is for Sunday and 7 is for Saturday
val = val+1;
}
beginDate = TalendDate.addDate(beginDate, 1, "dd");
}
return val;
}
}

View solution in original post


All Replies
Employee

Re: code for creating Routine

Hi,

 

    You cannot calculate the week day date difference with standard Talend date functions. You will have to create a custom routine and do a routine call from other Talend components.

 

    There are lot of solutions to identify the weekday count in stack overflow and you can use one of the methods provided from there.

 

https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

Five Stars

Re: code for creating Routine

Hi,

 

Thanks for your time, But I have already created a routine for my requirement called from tmap and it works fine.

Employee

Re: code for creating Routine

Hi,

 

   If you could share the solution, it would be helpful for other Talend community members as a reference material.

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

Five Stars

Re: code for creating Routine

Below is the code for DateDiff excluding Saturday and Sunday, works fine for my requirement

 

public class days {

static int day_of_week;
public static int getNumberOfDaysWorked(java.util.Date beginDate,java.util.Date endDate){
int val = 0;
while (beginDate.before(endDate)){
day_of_week=TalendDate.getPartOfDate("DAY_OF_WEEK", beginDate);
if(day_of_week != 1 && day_of_week != 7){// 1 is for Sunday and 7 is for Saturday
val = val+1;
}
beginDate = TalendDate.addDate(beginDate, 1, "dd");
}
return val;
}
}

View solution in original post

Employee

Re: code for creating Routine

Hi,

 

    Appreciate for taking time to explain the details back to Talend community. Please also mark your post containing the solution as answer to the original query. This will help our community members during their search.

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 3

Read about some useful Context Variable ideas

Blog