[resolved] Get last and first day of previous month using TalendDate.getDate
Hi there,
Me again.
How can I get the last and first day of the previous month using TalendDate.getDate? I've searched hi and lo and cannot find any suggestions.
This is very easy to do in sql using dateadd and datediff on the current date but I don't know if this is possible with Talend.
Thanks
Hi
This is also very easy in Talend with built-in functions: TalendDate.getFirstDayOfMonth(Date date) and TalendDate.getLastDayOfMonth(Date date), for example:
on tJava:
//TalendDate.getCurrentDate() returns the current date.
//TalendDate.addDate(TalendDate.getCurrentDate(),"-1","MM") returns last month.
java.util.Date firstDay=TalendDate.getFirstDayOfMonth(TalendDate.addDate(TalendDate.getCurrentDate(),"-1","MM"));
Hi
This is also very easy in Talend with built-in functions: TalendDate.getFirstDayOfMonth(Date date) and TalendDate.getLastDayOfMonth(Date date), for example:
on tJava:
//TalendDate.getCurrentDate() returns the current date.
//TalendDate.addDate(TalendDate.getCurrentDate(),"-1","MM") returns last month.
java.util.Date firstDay=TalendDate.getFirstDayOfMonth(TalendDate.addDate(TalendDate.getCurrentDate(),"-1","MM"));
Thanks Shong!
Although there was one slight typo in your solution I had to remove the double quotes around the -1 as it was giving a string error.
Also, I added a second line to assign it to a context variable that I need to use later on.
java.util.Date firstDay=TalendDate.getFirstDayOfMonth(TalendDate.addDate(TalendDate.getCurrentDate(),-1,"MM"));
context.start_date=TalendDate.formatDate("dd-MM-yyyy",firstDay);