0

I have been trying to convert excel date to Java date using POI , the file format is .xlsx. Date and Month is are correct when converted to java but year is displaying 1900 instead of 1968, tried all possible solution mentioned here but no luck, below is the code:

    DateFormat dateOfBirthformat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateOFBirth = null;
        if (DateUtil.isCellDateFormatted(row.getCell(13)) && DateUtil.isValidExcelDate(row.getCell(13).getNumericCellValue())) {
            dateOFBirth = row.getCell(13).getDateCellValue();

        }

        XMLGregorianCalendar dob = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateOfBirthformat.format(dateOFBirth));

        personalInfo.setDOB(dob);
a .s.
  • 79
  • 1
  • 13
  • Can you tell us in detail what you cell looks like in Excel, what `dateOFBirth` is after reading it from the cell and what `dob` is? I believe your best chance is if you can provide enough information that someone can reproduce the behaviour you see. – Ole V.V. Jun 04 '17 at 18:25
  • From Excel sheet DOB : 5/19/1970 dateOFBirth in java :Fri May 25 00:00:00 EST 1900 dob in java :1900-05-25 – a .s. Jun 04 '17 at 19:32
  • So apparently the month stays the same (5 or May), day of month goes from 19 to 25, and year from 1970 to 1900. I cannot explain. One thing to try would be to try with more dates and see of there seems to be a pattern. Another would be search the web for anomalies in the use of Apache POI for dates. – Ole V.V. Jun 04 '17 at 19:57
  • It seems you are not the first one having this problem. Take a look at [Apache POI Excel getDateCellValue() wrong year](https://stackoverflow.com/questions/16325983/apache-poi-excel-getdatecellvalue-wrong-year). I have not studied all the comments, see for yourself whether you can find something helpful there. Unfortunately there isn’t an answer. – Ole V.V. Jun 04 '17 at 20:09
  • @OleV.V. will try above link , thanks !! – a .s. Jun 04 '17 at 21:55
  • 2
    tried to debug and found out date format from excel sheet is _ m/d/yy_ in java even though in excel it is _mm/dd/yyyy_ ,Used following method to check date format : **row.getCell(13).getCellStyle().getDataFormatString()** – a .s. Jun 05 '17 at 00:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145845/discussion-between-ole-v-v-and-a-s). – Ole V.V. Jun 05 '17 at 06:53

0 Answers0