How to get last month end date in template

Hi Team,

I have a date merge field in my template, and I need to populate the last month end date in the field.

The last monthend date should be calculated from another date field from my the database.

Is there any methods to find last month end date in the template?

Thanks
Sundar.

@sundarrajan.r

Can you please provide more details about the date field from your database and how you are currently attempting to populate the merge field in your template?

Hi
I take publishedDate from my db and I have this merge field <<[lastMonthEnd]>> in my template.

I tried something like this in the template to get the last month end.

<<var[lastMonthEnd = publishedDate]>> << [lastMonthEnd.setDate(1)]>> <<[lastMonthEnd.setHours(-1)] >>

<<[lastMonthEnd]>> //here I am expecting the last month end date, but no luck.

@sundarrajan.r Unfortunately, your requirements are not clear enough. Could you please provide your sample data and the expected output? We will check and provide you more information.

Hi there

In simple words, I have a date (published date) in db (20/08/2024). In template I have a merge field <<[lastMonthendDate]>> and I want to show 31/07/2024 in that mergefield.

Hope this is clear.

@sundarrajan.r You can define an util method to get the last month date like this:

import java.util.Date;

public class MyUtil {
    public static Date GetMonthLastDate(Date d) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }
}

Then you can use this util method in your template:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("<<[MyUtil.GetMonthLastDate(lastMonthendDate)]>>");
        
ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(MyUtil.class);
engine.buildReport(doc, new Date(), "lastMonthendDate");

doc.save("C:\\Temp\\out.docx");

Thank you for your reply. I am aware of this. But I am looking for any options to handle this scenario within the template.

@sundarrajan.r Unfortunately, there is no way to achieve this without an utility method.