Loop through start date to end date

Loop through start date to end date
5 (100%) 2 votes

Loop Start Date through End Date using tLoop

This post I will describe you how to loop through start date to end date. For that we will use tLoop component which will give us two loop options first one is “for loop” and second one is “ while loop”.

Write down below code in tJava.

java.util.Date start_date=TalendDate.parseDate(“yyyy-MM-dd”, “2015-01-01″);

java.util.Date end_date=TalendDate.parseDate(“yyyy-MM-dd”, “2015-01-10″);

long l=TalendDate.diffDate(end_date, start_date);

context.Days=l;

code look likes as follows.

loop through start and end date
loop through start and end date

In above line of you can see we have parse two dates first one is start_date and second one is end_date.

Then we have calculated number of days using TalendDate.diffDate() method it will return number of days in long data type that is stored in variable “l” then this being assigned to “context.days” context variable.

Drop tLoop component next to the tJava and link with “OnsubJobOk” trigger then configure tLoop as follows.

Loop trough start and end date
Loop trough start and end date

Add tJava component next to tLoop and link with “iterate” flow. tLoop as two global variables which can be used in flow  for calculation or manipulation.

Here are those variables.

CURRENT_VALUE

((Integer)globalMap.get(“tLoop_1_CURRENT_VALUE”))((Integer)globalMap.get(“tLoop_1_CURRENT_ITERATION”))

CURRENT_ITERATION

((Integer)globalMap.get(“tLoop_1_CURRENT_ITERATION”))

We will use CURRENT_VALUE to get the day from start day through end date. To print each day on console we will use add date method from TalendDate routine. See the below code, wherein we are adding current value from flow to the start_date to increment start date by one day.

TalendDate.addDate(TalendDate.parseDate(“yyyy-MM-dd”,”2015-01-01″),+((Integer)globalMap.get(“tLoop_1_CURRENT_VALUE”)),”dd”);

After job run you will see below output on console.

Complete job design with output
Complete job design with output

About Umesh

I am Software consultant with approx 7 years of experience mainly in Business Intelligence and data warehousing assignments using Talend. Writing is not my passion but i am doing it to help others. if you have any special case where you want me to demonstrate then please post me.

Leave a Reply

Your email address will not be published. Required fields are marked *