我正在使用jaspersoft的iReport,我想将new java.util.Date()
(即当前日期)转换为该日期之前1个月。我应该在文本字段表达式中写什么来实现这一点?
我正在使用jaspersoft的iReport,我想将new java.util.Date()
(即当前日期)转换为该日期之前1个月。我应该在文本字段表达式中写什么来实现这一点?
DateTime
对象上调用minusMonths
方法。<?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>
Java日期API因其笨拙而声名狼藉,有一个实用的第三方替代品。尝试导入http://joda-time.sourceforge.net/库。
查看此帖子:将JodaTime Instant增加若干天数
Calendar c=Calendar.getInstance();
c.setTime(myDate); //Yes, it is strange!!! But we don't really need this, for the getInstance() results in a current date.
c.add(Calendar.MONTH, -1);
在你的评论之后,我意识到我无法将其更改为单个表达式,因为add返回void而不是new Date()。对此感到抱歉...