> For the complete documentation index, see [llms.txt](https://avbravo-2.gitbook.io/trucosjakartaee/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avbravo-2.gitbook.io/trucosjakartaee/reportes/pdfbox-reportes/formatear-fechas.md).

# Formatear Fechas

Formulario

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

En el controller

```java
  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;
    }
```

![](/files/-LoLwhnd0m1eePi965ah)

Invocarlo

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

Método

```java
 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();
        }
    }

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/reportes/pdfbox-reportes/formatear-fechas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
