[resolved] Difference between two dates excluding weekends

One Star

[resolved] Difference between two dates excluding weekends

Hi,
I have a scenario where I have two dates in two columns(Create date and As of date) which comes in a file.
I have to find the number of working days in between these two dates where the count excludes the weekends(Saturday and Sunday).
Appreciate if I get the code to be written in Talend open Studio.
Thanks
Pradhab

Accepted Solutions
One Star

Re: [resolved] Difference between two dates excluding weekends

Hello,
Maybe you can create a routine and implement a method in this routine doing what you want.
An example of method could be :
public class NumberOfDays {
   static int day_of_week;
   public static Integer getNumberOfDaysWorked(java.util.Date beginDate,java.util.Date endDate){
  Integer 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;
  }
}

After that you call this method in a Tmap.
I hope it can help you.

All Replies
One Star

Re: [resolved] Difference between two dates excluding weekends

Hello,
Maybe you can create a routine and implement a method in this routine doing what you want.
An example of method could be :
public class NumberOfDays {
   static int day_of_week;
   public static Integer getNumberOfDaysWorked(java.util.Date beginDate,java.util.Date endDate){
  Integer 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;
  }
}

After that you call this method in a Tmap.
I hope it can help you.
One Star

Re: [resolved] Difference between two dates excluding weekends

Thanks you so much for the code. I created a routine and implemented the method in it and called the method in the tmap in Talend. It worked as expected.
Thanks
Pradhab
One Star

Re: [resolved] Difference between two dates excluding weekends

You are welcome. Smiley Wink
I think you can mark this post as 'resolved'.