Setting Time in Excel using POI

后端 未结 3 1247
谎友^
谎友^ 2021-02-09 18:40

I am trying to create an Excel Work sheet using POI api in Java. In that Excel Work Sheet I want to have a cell with TIME alone. By setting this we can include the cell in summa

3条回答
  •  不知归路
    2021-02-09 19:13

    You can use DateUtil.convertTime(String time) method where time is like "HH:MM" or "HH:MM:SS". This method will give you a double value which you can set as value in your cell (cell.setCellValue(double value)).

    But convertTime method doesn't work with hours > 23. If you need this opportunity, you can write your own convertTime method, somthing like this:

    double convertTime(long hours, long minutes, long seconds) {
       double totalSeconds = seconds + (minutes + (hours) * 60) * 60;
       return totalSeconds / (SECONDS_PER_DAY);
    }
    

    SECONDS_PER_DAY = 60 * 60 * 24. You can simply take it from DateUtil class.

    Remember, you must define your cell format properly that excel can put in hours more than 23.

提交回复
热议问题