Apache POI to Create Excel Date Time Cell in m/d/yy h:mm:ss Format

Java Code Examples for

  • org.apache.poi.ss.usermodel.Cell.createCell()

The example below to create a new Excel file with one worksheet and a date time cell in m/d/yy h:mm:ss format using Apache POI library.

package simplesolution.dev;

import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

public class CreateHelperCreateDataFormatDateTimeExample {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Simple Solution");
        Row row = sheet.createRow(0);

        CreationHelper creationHelper = workbook.getCreationHelper();
        CellStyle cellStyle = workbook.createCellStyle();
        short dateTimeFormat = creationHelper.createDataFormat().getFormat("m/d/yy h:mm:ss");
        Cell cell = row.createCell(0);
        cell.setCellValue(new Date());

        String fileName = "D:\\Data\\sample.xlsx";
        try(OutputStream outputStream = new FileOutputStream(fileName)) {
        } catch (IOException ex) {

The output Excel file as below:

Apache POI to Create Excel Date Time Cell

Happy Coding 😊

Writing Excel File Using Apache POI Library in Java

Apache POI to Create Excel Text Cell

Apache POI Create new Excel sheet