13

Currently i have my code as

bean.setREPO_DATE(row.getCell(16).getDateCellValue());

it works fine if cell is formatted as date in excel.

However it also converts some integer or long like 1234 or 5699 to date. I know the reason behind this too.

However i want to apply a check before executing above line. Something like this

if(row.getCell(16).isOfDateFormat){
bean.setREPO_DATE(row.getCell(16).getDateCellValue());
} 

Please Guide me..

Thanks in Advance !

Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
  • 1
    possible duplicate of [XSSF POI is cell date](http://stackoverflow.com/questions/6677326/xssf-poi-is-cell-date) – Jayan Aug 19 '13 at 06:12
  • Sorry for that i have actually searched for it before posting the question – Abhishek Singh Aug 19 '13 at 06:18
  • @ Abhishek Singh : it was not exactly a duplicate(though you could the api in that answer. I removed my close vote) – Jayan Aug 19 '13 at 07:51

1 Answers1

24

Try this,

use import org.apache.poi.ss.usermodel.DateUtil;

if(DateUtil.isCellDateFormatted(cell))
   {
       cell.getDateCellValue();
   }
newuser
  • 8,338
  • 2
  • 25
  • 33
  • 1
    Thanks its working for me. can u please tell me how to check whether the value in cell is in percentage or normal number? – user3217843 Jul 22 '16 at 05:47
  • 3
    java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell if cell type is String – Se Song Sep 13 '19 at 10:11
  • 3
    You need to check if it's numeric before you check it is date. The only way for you to tell if a number is a date or a number is to look at its style. But you have to make sure it's a number first.From an answer to my similar [question]. Author: RealSkeptic. (stackoverflow.com/questions/60155480/…) . Someone can edit this so the word "question" points to my question. @SeSong – Kostas Thanasis Feb 10 '20 at 18:49