如何获取特定月份和年份的开始日期和结束日期
我正在 iReport
用户输入日期,例如2014-09-14
The user enters a date eg. 2014-09-14
然后该报告需要打印2014-09-01和2014-09-30
The report then needs to print 2014-09-01 and 2014-09-30
(所选月份和年份的第一天"和所选月份和年份的最后一天")
("First day of selected month and year" and "Last day of selected month and year")
我一直在努力使其与之合作
I've been trying to get it to work with
new java.util.Date(new java.util.GregorianCalendar($P{ds_endDate}.getYear(),$P{ds_endDate}.getMonth(),1).getTimeInMillis())
// end result must be java.util.Date
但是没有运气
如果您想在jasper报告中执行此操作,最好的方法是使用variable
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).
传入日期(我直接使用java.util.Calendar
作为类并设置默认值,以便可以在预览中进行测试)
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>
变量定义,这是月初,请使用getActualMaximum
而不是getActualMinimum
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>
由于我们使用的参数足以设置initialValueExpression
,如果您使用的是字段,则需要设置variableExpression
since we are using a parameter its enough to set initialValueExpression
if you are using a field you need to set the variableExpression
就这样,我们有我们的日期,请根据需要在textField
Thats it we have our date, format it as you like in the textField
示例
<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>
注意:如果您使用的是旧版的jasper report(3.1 ecc),则需要在不同的表达式s上设置正确的类
Note: if you are using old version of jasper report (3.1 ecc) you need to set correct class on the different expression 's