Formatear Fechas

Formulario

<p:commandButton action="#{programacionVechicularController.imprimir()}" value="Reporte"
                        icon="icon-report" ajax="false">
</p:commandButton>

En el controller

  public String showDate(Date date) {
        String h = "";
        try {
            h = DateUtil.dateFormatToString(date, "dd/MM/yyyy");
        } catch (Exception e) {
            JsfUtil.errorMessage("showDate() " + e.getLocalizedMessage());
        }
        return h;
    }
    
     public String showHour(Date date) {
        String h = "";
        try {
            h = DateUtil.hourFromDateToString(date);
        } catch (Exception e) {
            JsfUtil.errorMessage("showHour() " + e.getLocalizedMessage());
        }
        return h;
    }

Invocarlo

String fechaPartida = showDate(pv.getFechahorasalida()) +" "+showHour(pv.getFechahorasalida());

Método

 public void imprimir() {

        //com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4.rotate());
        com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4.rotate());
        //  com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.EXECUTIVE.rotate());

//        com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.LEGAL.rotate());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, baos);
            //METADATA

            document.open();

            Paragraph p = new Paragraph("PROGRAMACION DE FLOTA VEHICULAR",
                    FontFactory.getFont("arial", // fuente
                            12, // tamaño
                            Font.BOLD));
            p.setAlignment(Element.ALIGN_CENTER);
            document.add(p);
            //document.add(new Paragraph(" PROGRAMACION DE FLOTA VEHICULAR \n"));

            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy '-' hh:mm");
            Date currentDate = new Date();
            String date = formatter.format(currentDate);

            Paragraph titleDate = new Paragraph("Fecha: " + date,
                    FontFactory.getFont("arial", // fuente
                            10, // tamaño
                            Font.BOLD));
            titleDate.setAlignment(Element.ALIGN_RIGHT);
            document.add(titleDate);
            document.add(new Paragraph("\n"));

            PdfPTable table = new PdfPTable(8);

//            table.setTotalWidth(new float[]{90, 75, 90, 85, 225, 72, 110});
            table.setTotalWidth(new float[]{85, 65, 85, 80, 85, 220, 72, 105});

            table.setLockedWidth(true);

            PdfPCell cell = new PdfPCell(new Paragraph("Partida", FontFactory.getFont("arial", // fuente
                    12, // tamaño
                    Font.BOLD)));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell.setColspan(1);
            table.addCell(cell);

            table.addCell("Dia");
            table.addCell("Regreso");
            table.addCell("Unidad");
            table.addCell("Solicitado");
            table.addCell("Mision");

            table.addCell("Conductor");

            table.addCell("Vehiculo");

            for (ProgramacionVehicular pv : programacionVehicular) {
                // String fechaPartida = formatter.format(pv.getFechahorasalida());
                String fechaPartida = showDate(pv.getFechahorasalida()) + " " + showHour(pv.getFechahorasalida());
                String fechaRegreso = showDate(pv.getFechahoraregreso()) + " " + showHour(pv.getFechahoraregreso());
                String fechaSolicitado = showDate(pv.getFechasolicitud()) + " " + showHour(pv.getFechasolicitud());

                PdfPCell cellFechaPartida = new PdfPCell(new Paragraph(fechaPartida, FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellFechaPartida);

                PdfPCell cellNombredia = new PdfPCell(new Paragraph(pv.getNombredia(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellNombredia);

                PdfPCell cellFechaRegreso = new PdfPCell(new Paragraph(fechaRegreso, FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellFechaRegreso);

                PdfPCell cellUnidad = new PdfPCell(new Paragraph(pv.getUnidad(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellUnidad);
                PdfPCell cellSolicitado = new PdfPCell(new Paragraph(fechaSolicitado, FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellSolicitado);

//                  PdfPCell cellFechaSolicitado= new PdfPCell(new Paragraph(fechaSolicitado, FontFactory.getFont("arial", 9, Font.NORMAL)));
//                table.addCell(cellFechaSolicitado);
//                
                PdfPCell cellMision = new PdfPCell(new Paragraph(pv.getMision(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellMision);
//                table.addCell(pv.getMision());

                PdfPCell cellConductor = new PdfPCell(new Paragraph(pv.getConductor(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellConductor);

                PdfPCell cellVehiculo = new PdfPCell(new Paragraph(pv.getMarca() + " " + pv.getModelo() + " PLACA:" + pv.getPlaca(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(cellVehiculo);

            }
            document.add(table);
        } catch (Exception ex) {
            System.out.println("Error " + ex.getMessage());
        }
        document.close();
        FacesContext context = FacesContext.getCurrentInstance();
        Object response = context.getExternalContext().getResponse();
        if (response instanceof HttpServletResponse) {
            HttpServletResponse hsr = (HttpServletResponse) response;
            hsr.setContentType("application/pdf");
            hsr.setHeader("Contentdisposition", "attachment;filename=report.pdf");
            //        hsr.setHeader("Content-disposition", "attachment");
            hsr.setContentLength(baos.size());
            try {
                ServletOutputStream out = hsr.getOutputStream();
                baos.writeTo(out);
                out.flush();
            } catch (IOException ex) {
                System.out.println("Error:  " + ex.getMessage());
            }
            context.responseComplete();
        }
    }

Last updated