How to display date in HH:mm:ss format in JasperReports?

谁都会走 提交于 2020-01-10 02:51:12

问题


I am using following code to generate chart in JasperReports.

<xyLineChart> 
    <chart evaluationTime="Band"> 
        <reportElement x="0" y="0" width="555" height="500"/> 
    </chart> 
    <xyDataset> 
        <dataset incrementType="None"/> 
        <xySeries> 
            <seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression> 
            <xValueExpression><![CDATA[new Long($F{time}.getTime())]]></xValueExpression> 
            <yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
        </xySeries> 
    </xyDataset> 
    <linePlot> 
        <plot/> 
    </linePlot> 
</xyLineChart>

I am printing the date on the X-axis, but it is displaying in milliseconds. How do I display it in hh:mm:ss format?


回答1:


You can use following code in Java:

new SimpleDateFormat("MM-dd-yyyy HH:mm:ss z").format($V{VAR_DATE})

where $V{VAR_DATE} is the date variable to be converted into the format.




回答2:


Or you could just put the date variable in a text field and then, go the the properties view, and write this in the Pattern field: HH:mm:ss. It could be useful to also check the "Blank when null" checkbox




回答3:


The property you are looking for is the "time axis tick label mask". There is no "pattern" field for a time series chart.




回答4:


<xyLineChart> 
    <chart evaluationTime="Band"> 
        <reportElement x="0" y="0" width="555" height="500"/> 
    </chart> 
    <xyDataset> 
        <dataset incrementType="None"/> 
        <xySeries> 
            <seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression> 
            <xValueExpression><![CDATA[new Long($F{time}.getTime())]]></xValueExpression> 
            <yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
        </xySeries> 
    </xyDataset> 
    <linePlot> 
        <plot/> 
    </linePlot> 
</xyLineChart>

public static final String DATE_TIME_FORMAT ="yyyy-dd-MM'T'HH:mm:ss.SSS";
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT); df.format($V{VAR_DATE})




回答5:


Specify the "Time Period" as "minute" under the 'details' section of the chart details.

timePeriod="Minute"

From JasperReports Ultimate Guide: Time Period Expression

This expression returns a java.util.Date value from which the engine will extract the corresponding time period depending on the value set for the timePeriod attribute mentioned earlier in the Time Series dataset. For instance, if the chart is about yearly data, the engine will extract only the year from the date value, or if you are gathering monthly data, the engine will use only the month value from the date object returned by this expression.




回答6:


Simply, You can add in pattern property of the text field of date. the field should be in java.sql.Date format
You can write in pattern HH:mm:ss



来源:https://stackoverflow.com/questions/2643988/how-to-display-date-in-hhmmss-format-in-jasperreports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!