取“new java.util.Date()”并在1个月前完成 [英] taking "new java.util.Date()" and making it 1 month prior

查看:109
本文介绍了取“new java.util.Date()”并在1个月前完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using jaspersoft's iReport and I want to turn the new java.util.Date() (which is the current date) into 1 month prior from that date. What do I write in the text field expression to achieve this?

推荐答案

您可以使用Joda-Time Java API。致电 minusMonths 方法noreferrer> DateTime object。

You can use Joda-Time Java API. Call the minusMonths method on a DateTime object.

jrxml文件样本:

The jrxml file sample:

<?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>

结果将是:

< img src =https://i.stack.imgur.com/iz3Od.pngalt =iReport中的结果>

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

这篇关于取“new java.util.Date()”并在1个月前完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆