Convert Date to LocalDate in Java
Problem
A java.util.Date is really an instant on the timeline; turning it into a calendar date requires choosing a time zone.
Convert a Date representing 20:00 UTC on 9 March 2026 into a LocalDate in Kolkata and in UTC, then convert the LocalDate back to a Date.
Java Program
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class ConvertDateToLocalDate {
public static void main(String[] args) {
ZoneId zone = ZoneId.of("Asia/Kolkata"); // a Date is an instant; the zone decides the calendar date
Date legacy = Date.from(Instant.parse("2026-03-09T20:00:00Z"));
LocalDate local = legacy.toInstant().atZone(zone).toLocalDate();
System.out.println("LocalDate in Kolkata: " + local);
System.out.println("LocalDate in UTC: " + legacy.toInstant().atZone(ZoneId.of("UTC")).toLocalDate());
Date back = Date.from(local.atStartOfDay(zone).toInstant()); // LocalDate -> Date (midnight in that zone)
System.out.println("Back to Date (epoch ms): " + back.getTime());
}
}Output
Core Logic
The bridge between the old and new APIs is Instant: go from the Date to an Instant, then attach a zone to get a local date.
- 1
legacy.toInstant()gives the moment the Date represents. - 2
atZone(zone).toLocalDate()reads the calendar date in that zone. - 3
local.atStartOfDay(zone).toInstant()picks midnight in that zone for the reverse direction. - 4
Date.from(instant)wraps it back into a legacy Date.
Key Point: Converting with the wrong zone silently shifts dates by a day near midnight — always state the zone explicitly.
Key Concepts
Approach 2: java.sql.Date
import java.time.LocalDate;
public class ConvertDateToLocalDateSql {
public static void main(String[] args) {
LocalDate day = LocalDate.of(2026, 3, 9);
java.sql.Date sqlDate = java.sql.Date.valueOf(day); // for JDBC code that still uses java.sql.Date
LocalDate back = sqlDate.toLocalDate();
System.out.println("java.sql.Date: " + sqlDate);
System.out.println("Back to LocalDate: " + back);
System.out.println("Round trip equal: " + day.equals(back));
}
}
Output
Core Logic
java.sql.Date represents a date without a time, so it converts to and from LocalDate directly.
- 1
java.sql.Date.valueOf(day)creates the SQL date. - 2
sqlDate.toLocalDate()converts it back. - 3The round trip gives an equal
LocalDate.
Key Point: Modern JDBC drivers accept LocalDate directly via setObject, so the conversion is only needed for older code.
Key Concepts
Interview Questions
Why was java.util.Date replaced?
It's mutable, its months start at 0, its year counts from 1900, and it mixes an instant with time-zone-dependent display — all fixed by java.time.
How do you convert a Calendar to LocalDateTime?
LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).