采用"new java.util.Date()"并在1个月之前完成

pre*_*ose 5 java jasper-reports ireport

我正在使用jaspersoft的iReport,我希望new java.util.Date()将该日期(也就是当前日期)变为该日期之前的1个月.我在文本字段表达式中写什么来实现这一点?

Ale*_*x K 12

您可以使用Joda-Time Java API.minusMonthsDateTime对象上调用方法.

jrxml文件示例:

<?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="joda_sample" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <import value="org.joda.time.DateTime"/>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="109" y="23" width="175" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["Current date: " + new SimpleDateFormat("dd.MM.yyyy").format(new Date())]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="336" y="23" width="200" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["Current date minus one month: " + DateTime.now().minusMonths(1).toString("dd.MM.yyyy")]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

结果将是:

结果在iReport中

注意: 不要忘记将Joda-Time库添加到类路径中(在我的情况下,我将库添加到 iReport的类路径中).