Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
So currently I have a date that I ingested from a filename and it has a format of "dd-MM-yyyy HH:mm" and the HH:mm is currently 00:00. Is it possible that it iterates per 5 minutes 00:00, 00:05 up to 23:55?
This will do it....
public class DateIteration {
private static Date date;
private static int initialDay;
public static Date iterateDates(Date sourceDate, int minutesToAdd) {
if(date==null) {
date = sourceDate;
initialDay = routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate);
}else{ //New section
if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){
date = routines.TalendDate.addDate(date,-1,"dd");
}
}
date = routines.TalendDate.addDate(date, 5, "mm");
/* Code moved to the "else" of the first if condition
if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){
date = routines.TalendDate.addDate(date,-1,"dd");
} */
return date;
}
}
I simply moved the if condition which deals with the amendment of the day in the date to the "else" of the first if condition. What this does is leave the very first time the day ticks over to the next day. Then the next time the method is called it checks the original day against the current value's day. When they do not match, it automatically removes a day.
You will need to ensure that all dates supplied have 00:00:00.000 as the time. If not, you will need to add something to this to ensure that the time is changed to 00:00:00.000.
Hi @JOHN ZACHARY ABELLA,
Yes, this is possible using a routine. You create the routine (basically this is a Java class) and call the method that you have created to do this in the tMap for every row.
An example routine for this can be seen below.....
public class DateIteration {
private static Date date;
public static Date iterateDates(Date sourceDate, int minutesToAdd) {
if(date==null) {
date = sourceDate;
}
date = routines.TalendDate.addDate(date, 5, "mm");
return date;
}
}
You'd call this in your tMap like this.....
routines.DateIteration.iterateDates({Your Date Column}, 5)
Where I have added {Your Date Column} you would place your date column there.
Thank you so much!. It works perfectly 🙂
Glad to be able to help 🙂
Hi rhall, I overlooked the data in my desired output table. So for my output table right now using your solution it goes 05-11- 2021 23:55 then 06-11-2021 00:00. instead of 05-11-2021 00:00. Would it be possible if only the timestamp iterates while the initial date remains static? So after reaching 00:00 again, the date should still be 05-11-2021
This is a quick fix to the routine I sent you. It might not be perfect, but should work for you....
public class DateIteration {
private static Date date;
public static Date iterateDates(Date sourceDate, int minutesToAdd) {
if(date==null) {
date = sourceDate;
}
date = routines.TalendDate.addDate(date, 5, "mm");
if(routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate)!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){
date = routines.TalendDate.addDate(date,-1,"dd");
}
return date;
}
}
Thanks again! It works. May I ask the logic behind the code?
OK, so the first time this method is called in a Job, it will set the "date" static variable. If it has already been set, the date being passed in ("sourceDate") does nothing to the "date" variable. Then we add 5 minutes to the "date" variable. This means that every time this method is called, 5 minutes will be added to the "date" variable which is maintained as a static variable. This was the logic of the original code I sent.
The next bit was a bit of a quick fix to keep it to the same day. This is an ideal solution, but works in your scenario. To make sure that the day remains the same, we use the "sourceDate" param value and check its day of the week against the updated "date" variable's day of the week. If they are different, we subtract 1 day from the updated "date" value.
As I said, this is not perfect. If you send through a different date in subsequent calls, it will cause 1 day to be removed from the output date. As such, I have updated this below to give a better solution in case your date does change. The "initialDay" variable will now store the very first day of the week supplied during the run of the job.
public class DateIteration {
private static Date date;
private static int initialDay;
public static Date iterateDates(Date sourceDate, int minutesToAdd) {
if(date==null) {
date = sourceDate;
initialDay = routines.TalendDate.getPartOfDate("DAY_OF_WEEK", sourceDate);
}
date = routines.TalendDate.addDate(date, 5, "mm");
if(initialDay!=routines.TalendDate.getPartOfDate("DAY_OF_WEEK", date)){
date = routines.TalendDate.addDate(date,-1,"dd");
}
return date;
}
}
Oh thank you so much, really appreciate all the help. It is much clearer now 🙂
Hi rhall, I just did UAT for my job and the desired output for this row should be
static like what I mentioned previously except for the last timestamp so once it reaches 00:00 again the date should be date + 1. For ex.
Date
05 - 11 - 2021 00:05
05 - 11 - 2021 00:10
05 - 11 - 2021 00:15
...
05 - 11 - 2021 23:55
06 - 11- 2021 00:00
05 - 11 - 2021 00:05
Am currently trying to experiment with the routine but am struggling 😞 .
Is there a quick fix for this?