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

Highlighted
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

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

All 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.

15TH OCTOBER, COUNTY HALL, LONDON

Join us at the Community Lounge.

Register Now

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog