One Star

[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
1 ACCEPTED SOLUTION

Accepted Solutions
Community Manager

Re: [resolved] Get last and first day of previous month using TalendDate.getDate

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"));

Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
3 REPLIES
Community Manager

Re: [resolved] Get last and first day of previous month using TalendDate.getDate

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"));

Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Get last and first day of previous month using TalendDate.getDate

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);
One Star

Re: [resolved] Get last and first day of previous month using TalendDate.getDate

This discussion has helped me lot. Thanks to Shong and Facoda.