Java
java.sql.Timestamp
LocalDate
java.time
Java 8

How to convert java.sql.Timestamp to LocalDate (Java 8 java.time)

Codemia Knowledge Hub ·

Use timestamp.toLocalDateTime().toLocalDate() if you just want the date part of the stored local timestamp. Use timestamp.toInstant().atZone(zoneId).toLocalDate() if the timestamp is a moment in time and you want the date in a particular zone. The two can give different dates, so pick the one that matches what the value means.

Drop the time with toLocalDateTime()

If the Timestamp represents a local database timestamp and you want its date portion, go through toLocalDateTime():

java
1import java.sql.Timestamp;
2import java.time.LocalDate;
3
4Timestamp timestamp = Timestamp.valueOf("2025-07-31 10:15:00");
5LocalDate date = timestamp.toLocalDateTime().toLocalDate();
6
7System.out.println(date);

This is usually the cleanest choice when you map a SQL TIMESTAMP column into local date logic without any time-zone conversion.

Go through Instant when the zone matters

If the timestamp is a moment on the timeline, convert it to Instant, apply a ZoneId, and take the LocalDate:

java
1import java.sql.Timestamp;
2import java.time.Instant;
3import java.time.LocalDate;
4import java.time.ZoneId;
5
6Timestamp timestamp = Timestamp.valueOf("2025-07-31 10:15:00");
7Instant instant = timestamp.toInstant();
8LocalDate date = instant.atZone(ZoneId.of("America/Toronto")).toLocalDate();
9
10System.out.println(date);

The zone matters because the same instant can fall on different calendar dates in different regions.

Which conversion to use

  • toLocalDateTime().toLocalDate() when you want the local date portion of the stored value.
  • toInstant().atZone(...).toLocalDate() when the timestamp is a real moment in time in a specific zone.

They are not always interchangeable. Near midnight, changing the zone can move the date backward or forward.

Reading the value from JDBC

The question often comes up when reading from a ResultSet:

java
Timestamp timestamp = resultSet.getTimestamp("created_at");
LocalDate date = timestamp.toLocalDateTime().toLocalDate();

That is convenient, but decide whether local timestamp semantics are right for that column. Some systems store UTC-oriented timestamps and expect application code to interpret them in a specific zone. Others treat the value as a plain local timestamp with no time-zone meaning. The right conversion depends on what the schema means, not on what compiles.

Handling null columns

For a nullable column, check for null before converting:

java
Timestamp timestamp = resultSet.getTimestamp("created_at");
LocalDate date = timestamp == null ? null : timestamp.toLocalDateTime().toLocalDate();

If null is not a valid domain value, turn it into an optional or a validation error early instead of letting it travel through the application.

Also ask whether the column should be a timestamp at all. If the business concept is date-only, a SQL DATE may give clearer code than converting a timestamp and discarding information.

Write tests around midnight boundaries. That is where the zone-sensitive path and the local timestamp path are most likely to diverge visibly.

Mistakes to avoid

  • Treating every timestamp-to-date conversion as a time-zone problem, which overcomplicates a plain local timestamp mapping.
  • Ignoring the zone when the timestamp really is an instant, which produces wrong dates around midnight and daylight-saving transitions.
  • Copying the first conversion snippet you find without checking what the column means.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design