Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

When we’re working with dates in Apache POI, we want to make sure that they’re formatted correctly.

Fortunately, it’s easy to set the date format using Apache POI. In this tutorial, we’ll show how to define a custom DataFormat for a date as CellStyle with Apache POI and how to use existing DataFormats.

2. Starting Point

Our starting point will be a new XSSFWorkbook, an XSSFCell, and an already-created CellStyle:

XSSFWorkbook wb = new XSSFWorkbook();
CellStyle cellStyle = wb.createCellStyle();
wb.createSheet();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFCell dateCell = sheet.createRow(0).createCell(0);
dateCell.setCellValue(new Date());

Since we haven’t set up our wished DataFormat yet, our Date would have been converted to a number and will be displayed as:

44898,9262857176

This way of representation is badly readable for us humans. Therefore, in the following, we’ll look at how we can create a better visualization by formatting.

2. Create a Custom DataFormat

First, we need to create a new CreationHelper. With the CreationHelper, we can create a new DataFormat with a specific Format. This DataFormat is internally stored and gets referenced by a short. We’ve to add it to the CellStyle itself and apply the CellStyle to the Cell:

CreationHelper createHelper = wb.getCreationHelper();
short format = createHelper.createDataFormat().getFormat("m.d.yy h:mm");
cellStyle.setDataFormat(format);
dateCell.setCellStyle(cellStyle);

After we’ve set up this custom CellStyle, our date will be formatted:

02.12.2022 21:30

However, if we create a new custom DataFormat, we should always remember that an Excel workbook supports a maximum number of 65.000 cell styles. So we should always reuse existing cell styles and apply them to multiple cells whenever possible.

3. Use a Default DataFormat

As we learned, Apache POI uses shorts to link to different DataFormats. Excel already has a lot of builtin DataFormats which we can use by calling them directly by their short:

cellStyle.setDataFormat((short) 14);
dateCell.setCellStyle(cellStyle);

Afterward, we can get the DataFormat in a String representation with the following line of code:

cellStyle.getDataFormatString();

In our example, we’d get the following:

m/d/yy

The most common DataFormats are:

Short Value Format
14 m/d/yy
15 d-mmm-yy
16 d-mmm
17 mmm-yy
18 h:mm AM/PM
19 h:mm:ss AM/PM
20 h:mm
21 h:mm:ss
22 m/d/yy h:mm

We should always use one of those data formats if it fits our needs because Excel will display them as one of their formats and not a custom one. This will also trigger Excel to use a localized visualization of the format.

4. Conclusion

As we can see, setting the date format using Apache POI is quick and easy but also necessary to visualize dates in a human-readable way. We should try it next time we work with dates in our spreadsheets.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW