Create Date in Excel File using Apache POI in Java


In this example I will show you how to create date and put it in an excel file using Apache POI in Java language. I will show you how to build the project using both maven and gradle build tools. I will also show you how to create date using Java 8 or prior to Java 8 on java.util.Date and java.util.Calendar.

The Apache POI is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2). You can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is the Java Excel solution (for Excel 97-2008).


Java 8+, Apache POI – 4.1.1/5.2.2, Gradle 5.6, Maven – 3.6.1 – 3.8.5

Project Setup

Create a standalone maven project or gradle based project in your favorite IDE or tool. The name or artifact id of the project is apache-poi-excel-date.

If you are creating maven based project then you can use the below pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">





If you are creating gradle based project then you can use below build.gradle script for creating date in excel file:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.1'
    //required only for jdk 9 or above

Create Date in Excel File

Now I will create date in excel file using Apache POI in Java technology. I will put four different kind of date values in excel file – default date, java.util.Date, java.util.Calendar and Java 8’s LocalDate & LocalDateTime.

public class ExcelDate {

	public static void main(String[] args) {
		final String fileName = "excel-date.xlsx";// "excel-date.xls";

	public static void createExcel(final String fileName) {
		// get the file extension
		String ext = ".xlsx";
		if (fileName != null) {
			int len = fileName.trim().lastIndexOf(".");
			ext = fileName.trim().substring(len);

		Workbook workbook = null;

		// based on file extension create Workbook object
		if (".xls".equalsIgnoreCase(ext)) {
			workbook = new HSSFWorkbook();
		} else if (".xlsx".equalsIgnoreCase(ext)) {
			workbook = new XSSFWorkbook();

		// create Sheet object
		// sheet name must not exceed 31 characters
		// the name must not contain 0x0000, 0x0003, colon(:), backslash(\),
		// asterisk(*), question mark(?), forward slash(/), opening square
		// bracket([), closing square bracket(])
		Sheet sheet = workbook.createSheet("my_sheet");

		// Create first row. Rows are 0 based.
		Row row = sheet.createRow((short) 0);

		// Create a cell
		// put a value in cell.
		// default Date
		row.createCell(0).setCellValue(new Date());

		// style Date
		CreationHelper createHelper = workbook.getCreationHelper();
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("MM/dd/yyyy hh:mm:ss"));

		Cell cell = row.createCell(1);
		cell.setCellValue(new Date());

		// set date as java.util.Calendar
		CellStyle cellStyle2 = workbook.createCellStyle();
		cellStyle2.setDataFormat(createHelper.createDataFormat().getFormat("MMM/dd/yyyy hh:mm:ss"));
		cell = row.createCell(2);

		// set date as Java 8
		CellStyle cellStyle3 = workbook.createCellStyle();
		cellStyle3.setDataFormat(createHelper.createDataFormat().getFormat("MMM/dd/yyyy hh:mm:ss"));
		cell = row.createCell(3);

		CellStyle cellStyle4 = workbook.createCellStyle();
		cell = row.createCell(4);

		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
			try {
			} catch (IOException e) {


Testing Date Creation in Excel Sheet

Run the above class, you will get excel-date.xlsx or excel-date.xls file created under the project root directory. Open the file you will get the output in sheet “my_sheet”.

create date in excel file using apache poi in java

Source Code


2 thoughts on “Create Date in Excel File using Apache POI in Java

Leave a Reply

Your email address will not be published. Required fields are marked *