1

I have created several variables with time-expressions, to create time-based reports:

// First of month (Returns 01/09/2017)
 new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + MONTH(NOW( )) + "/" + YEAR(NOW())))

// First of previous month (Returns 01/08/2017)
 new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse("01/" + (MONTH(NOW( )) - 1) + "/" + YEAR(NOW())))

// Last of month (Returns 30/09/2017)
 new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH(NOW( )) + "/" + DAYSINMONTH(NOW())+ "/" + YEAR(NOW())))

I am now struggling with getting the last day of the previous month. I tried something like below, but of course this does not work properly, as not all months have the same amount of days.

// Last of previous month (Returns 30/08/2017)
 new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(
  (MONTH(NOW( )) - 1)
  + "/" +
  DAYSINMONTH(NOW())
  + "/" +
  YEAR(NOW())
 ))

Do you know of a way to retrieve the last (day) of the previous month?

reference: http://community.jaspersoft.com/questions/843248/last-day-current-month

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
user2663557
  • 35
  • 1
  • 7
  • I recently realized it by using a SQL case in the select statement. Still interested in clever solutions though. – user2663557 Sep 26 '17 at 12:06

1 Answers1

0

I would probably have solved this creating a static class in java with a method that takes the date as parameter and then use the Calendar api or similar to return to the desired date, this is how to do it in java Calendar - Get last day of previous month.

However, lets have fun and use only jasper-reports

Since we can't use Calendar api (it returns void) we need to fall back again on the org.apache.commons.lang.time.DateUtils as my friend Tunaki taught me.

Using DateUtils we can call first truncate with month (remove days) and then we addDays -1 (-1 day if we are at first date of month it will give us the last date of previous month)

DateUtils.addDays(DateUtils.truncate(myDate, Calendar.MONTH),-1);

jrxml example

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="DateUtil" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f288086f-db4e-451f-bf32-e1cce6311a27">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <import value="java.util.Calendar"/>
    <import value="org.apache.commons.lang.time.DateUtils"/>
    <parameter name="date" class="java.util.Date">
        <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="20" splitType="Stretch">
            <textField pattern="MM/dd/yyyy">
                <reportElement x="0" y="0" width="220" height="20" uuid="48878c52-5527-4784-a74a-e2d8df65cc55"/>
                <textFieldExpression><![CDATA[DateUtils.addDays(DateUtils.truncate($P{date}, Calendar.MONTH),-1)]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Output (the day I passed this answer)

result

Design note: You should use pattern to format the Date as I do in my example, do not use SimpleDateFormat in expression, using pattern will give you correct object if you export for example to excel.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109