help java.lang.String can not be cast to java.util.Date I do report with dates

1

I want to make a report with Java and iReport where the user provides the start and end dates of the output of products in a report dates provided from widgets DateTimes and pass them to a variable String and then that variable parses for that I recognize ireport but I have run into the error

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

Here I leave the code.

String fechaInicio = String.format("%04d-%02d-%02d", dtInicio.getYear(), dtInicio.getMonth() + 1,
                    dtInicio.getDay());
String fechaFin = String.format("%04d-%02d-%02d", dtFin.getYear(), dtFin.getMonth() + 1,
                    dtFin.getDay());        
Map parametro = new HashMap();
Map filtroFecha = new HashMap();

try{
    filtroFecha.put("nomArt", txtArticulo.getText());
    System.out.println(fechaInicio);
    filtroFecha.put("Fecha1", new SimpleDateFormat("yyyy/MM/dd").format(new java.text.SimpleDateFormat(fechaInicio).parse(fechaInicio)));
    filtroFecha.put("Fecha2", new SimpleDateFormat("yyyy/MM/dd").format(new java.text.SimpleDateFormat(fechaFin).parse(fechaFin)));
    JasperPrint jasperPrintWindow = JasperFillManager.fillReport(
                    "reporteFiltroFecha.jasper", filtroFecha,
                    Conexion.GetConnection());
    JasperViewer jasperViewer = new JasperViewer(jasperPrintWindow,false);
        jasperViewer.setVisible(true);
}
    
asked by Luis 17.08.2017 в 00:13
source

1 answer

0

The error tells you that you can not convert a string to an object of type date

Surely you have your parameter configured as Date

<parameter name="Fecha1" class="java.util.Date"/>
<parameter name="Fecha2" class="java.util.Date"/>

If so, you must change it to String

<parameter name="Fecha1" class="java.lang.String"/>
<parameter name="Fecha2" class="java.lang.String"/>

This can also be done from the designer without the need to edit the xml of *.jrxml

Recommendation

You can leave your parameter as Date and your textField if you have it as string

So that from java you send it directly and format it from the report

filtroFecha.put("Fecha1", fechaInicio));
filtroFecha.put("Fecha2", fechaFin));

And in your textField you can format it like this

<textField>
    <reportElement uuid="764fc239-848c-4329-9e5b-20323a4ced31" x="444" y="5" width="128" height="25"/>
    <textElement>
        <font size="7"/>
    </textElement>
    <textFieldExpression><![CDATA[! $P{FECHA_INICIO}.equals( $P{FECHA_FIN} ) ? new StringBuilder(new SimpleDateFormat("EEEE dd/MMMM/yyyy", $P{REPORT_LOCALE}).format($P{FECHA_INICIO}).toUpperCase() + "-" + new SimpleDateFormat("EEEE dd/MMMM/yyyy", $P{REPORT_LOCALE}).format($P{FECHA_FIN}).toUpperCase()) : new StringBuilder(new SimpleDateFormat("EEEE dd/MMMM/yyyy", $P{REPORT_LOCALE}).format($P{FECHA_INICIO}).toUpperCase())]]></textFieldExpression>
</textField>
    
answered by 17.08.2017 / 00:30
source