I am creating a report in iReport
The user enters a date eg. 2014-09-14
The report then needs to print 2014-09-01 and 2014-09-30
(\"First day of
I got it working by using some SQL in the query of the report and just printing that field
SQL looks like this
Select LAST_DAY('2014-09-14') lastDay,
DATE_ADD((DATE_ADD(LAST_DAY('2014-09-14') ,INTERVAL 1 DAY)) ,INTERVAL -1 MONTH) firstDay
Note: replace '2014-09-14'with input parameter on reprot eg. $P{dateEntered}
If you like to do this inside jasper report a good way to go is using variable
Example: lets assume the date is arriving as a parameter (if you like naturally it can be a field, you just need to switch how the variable is calculated).
The incoming date (I use directly the java.util.Calendar
as class and set a default value so that I can test in preview)
<parameter name="date" class="java.util.Calendar" isForPrompting="false">
<defaultValueExpression><![CDATA[new GregorianCalendar()]]></defaultValueExpression>
</parameter>
The variable definition, this is beginning of month, create another variable for end of month using getActualMaximum
instead of getActualMinimum
<variable name="beginningOfMonth" class="java.util.Calendar">
<initialValueExpression><![CDATA[new java.util.GregorianCalendar($P{date}.get(Calendar.YEAR),$P{date}.get(Calendar.MONTH) ,$P{date}.getActualMinimum(Calendar.DAY_OF_MONTH))]]></initialValueExpression>
</variable>
since we are using a parameter its enough to set initialValueExpression
if you are using a field you need to set the variableExpression
Thats it we have our date, format it as you like in the textField
Example
<textField>
<reportElement x="22" y="11" width="100" height="20" uuid="1a094181-bcf1-469c-a786-77a0b7a3d533"/>
<textFieldExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MM-dd").format($V{beginningOfMonth}.getTime())]]></textFieldExpression>
</textField>
Note: if you are using old version of jasper report (3.1 ecc) you need to set correct class on the different expression 's
Try this out:
Calendar abc = new GregorianCalendar();
System.out.println(abc.getActualMaximum(Calendar.DAY_OF_MONTH) + " and " + abc.getActualMinimum(Calendar.DAY_OF_MONTH));