Apache POI Time Cell

I need to determine if the cell format is Date, Time or Datetime.
My code is:

        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (DateUtil.isCellDateFormatted(cell)) {
                if (   ???   )
                    return "Time";
                else if (   ???   )
                    return "Date";
                else
                    return "Datetime";
            }
        }

I am using ApachePOI 3.6

+3
source share
2 answers

I do not see a built-in way to do this easily in POI. First, there is no clear distinction between “Time,” “Date,” and “Date-Time.” Values ​​are simply stored as numbers, and a format is used to display them.

, , (HSSFCellStyle), , style.getDataFormatString(), style.getDataFormat(), , "", Date "" Datetime " , . int, .

, , , "[$ -F400] h: mm: ss\AM/PM" int 166.

Excel Date / Time Format

, , "Datetime", Excel, , .

, , , .

0

, , , - , , . style.getDataFormatString() style.getDataFormat(), , . :

private boolean dateIncludesTime() // this method only tries to detect does time part exist, but it is not sure it will succeeed
{
    Date javaDate= (Date)getValue();
    if (javaDate.getHours() > 0 || javaDate.getMinutes() > 0) 
        return true;

    int formatID = apacheCell.getCellStyle().getDataFormat();
    if(formatID >= 18 && formatID <=22) // https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
        return true;

    dateFormat = apacheCell.getCellStyle().getDataFormatString();
    if(dateFormat.toLowerCase().contains("h")) // format mentions hours
        return true;

    return false;
}

:

input 03/21/2015 21:30 getDataFormatString() MM/DD/YYID = 165

input 03/21/2016 getDataFormatString() MM/DD/YYID = 165

input 2017-04-21 10:20 getDataFormatString() YYYY-MM-DD formatID = 166

input 2017-05-21 getDataFormatString() YYYY-MM-DD formatID = 166

0

Source: https://habr.com/ru/post/1749401/


All Articles