Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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
Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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

View solution in original post

3 Replies
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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);
Anonymous
Not applicable
Author

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