0

I am unable to parse a German date (dd.MM.yyyy) from an excel sheet using Poi. In fact, reading the date cell returns a (d/M/yyyy) pattern String date, although the excel cell format is (Date dd.MM.yyyy) as it is shown in the following screenshot:

enter image description here

Source code :

DataFormatter formatter = new DataFormatter();
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy");
String dateString = formatter.formatCellValue(row.getCell(11)).trim();//date="1/3/2018"
Date dateStart = dateFormatter.parse(dateString);//throws a java.text.ParseException

P.S : A simple solution to circumvent this problem would be using a SimpleDateFormat("d/M/yyyy") but I would like to fix this problem and understand the reason behind this unexpected behavior.

ecdhe
  • 421
  • 4
  • 17
  • 3
    For the default date format (number format id 14) there is not a format string. See https://stackoverflow.com/questions/34900605/excell-cell-style-issue/34902174#34902174. – Axel Richter Mar 01 '18 at 10:25
  • If you open that same Excel file on a computer with the locale set to US-English, does the date magically (automatically) change to the other format? – Gagravarr Mar 01 '18 at 10:26
  • 2
    Why using the `SimpleDateFormat` at all? Why not getting the `Date` directly from the cell using `row.getCell(11).getDateCellValue()`? – Axel Richter Mar 01 '18 at 10:54
  • @AxelRichter : Your solution is right and without any further complications. Thanks. – ecdhe Mar 01 '18 at 15:04
  • @Gagravarr : I cannot do that given I have only one laptop with german locale. – ecdhe Mar 01 '18 at 15:06

0 Answers0