ozlerhakan / poiji

:candy: A library converting XLS and XLSX files to a list of Java objects based on Apache POI

Home Page:https://ozlerhakan.github.io/poiji/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect date format

saladukha opened this issue · comments

In my Excel file, the date is in the format: 06/29/2021

When applying debug, I see the following information:

PoijiLogCellFormat log = new PoijiLogCellFormat ();
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings ()
                 .poijiLogCellFormat (log)
                 .build ();
Poiji.fromExcel (new File (xlsFileLocation), XlsFileDataDTO.class, options);
List <PoijiLogCellFormat.InternalCellFormat> formats = log.formats ();
PoijiLogCellFormat.InternalCellFormat cell10 = formats.get (20);
cell10.getFormatString (); // m/d/yy
cell10.getFormatIndex (); // 14

I applied the following number format:

PoijiNumberFormat numberFormat = new PoijiNumberFormat ();
numberFormat.putNumberFormat ((short) 14, "dd/MM/yyyy");
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings () .poijiLogCellFormat (log) .poijiNumberFormat (numberFormat)
                 .build ();

cell10.getFormatString (); // dd/MM/yyyy
cell10.getFormatIndex (); // 14

But my LocalDate shows up as "2021-06-29".

What must be done to display the LocalDate in the format: 29/06/2021?

Hi @saladukha ,

Could you share with me the case so I can try it

Hello @ozlerhakan ,

DTO

@Data
public class XlsFileDataDTO {

    @ExcelRow
    private int rowIndex;

    @ExcelCellName("C. id")
    private String bufferId;

    @ExcelCellName("sequenzaG")
    private String seqG;

    @ExcelCellName("sequenzaI")
    private String seqI;

    @ExcelCellName("motivoG")
    private String reasonG;

    @ExcelCellName("N")
    private String n;

    @ExcelCellName("dataN")
    private LocalDate dateGenotypeNgs;

    @ExcelCellName("varianteN")
    private String variantN;

    @ExcelCellName("tipoVarianteN")
    private String variantTypeSangerNgs;

    @ExcelCellName("GIS")
    private String gisaidNgs;

    private String laboratoryGenome;
}

method for extracting objects from Excel

private List<XlsFileDataDTO> getObjectsFromXls(String xlsFileLocation) {
        PoijiNumberFormat numberFormat = new PoijiNumberFormat();
        numberFormat.putNumberFormat((short) 14, "dd/MM/yyyy");
        PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().poijiNumberFormat(numberFormat).build();
        return Poiji.fromExcel(new File(xlsFileLocation), XlsFileDataDTO.class, options);
    }

file.xlsx

Thank you @saladukha , I will take a look at it

Hi @saladukha ,

Using LocalDate, all we do is that we convert correctly the string date to LocalDate. This does not mean that we expect the same format as we point. Using this format we only show the places where day, month and year sections are. LocalDate prints the date in ISO8601 format (yyyy-MM-dd) , so when you do LocalDate.now() you will see the same format of the current date.

You can convert the LocalDate field to String format using:

DateTimeFormatter myDateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
myLocalDate.format(myDateTimeFormatter);

Hello @ozlerhakan ,

Ok, thanks for the answer.
Since I also use the same data to send in the form of json to a third-party application, so I solved this problem with:
@JsonFormat (shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")