Java Programs›Date & Time›Convert Date to LocalDate

Convert Date to LocalDate in Java

intermediate·  Date & Time  ·  Date & Time

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.

Input
2026-03-09T20:00:00Z
Output
Kolkata 2026-03-10, UTC 2026-03-09

Java Program

Java
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

LocalDate in Kolkata: 2026-03-10 LocalDate in UTC: 2026-03-09 Back to Date (epoch ms): 1773081000000

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.

How It Works
  1. 1legacy.toInstant() gives the moment the Date represents.
  2. 2atZone(zone).toLocalDate() reads the calendar date in that zone.
  3. 3local.atStartOfDay(zone).toInstant() picks midnight in that zone for the reverse direction.
  4. 4Date.from(instant) wraps it back into a legacy Date.
20:00 UTC is already 01:30 the next day in Kolkata, so the two zones give different dates.
💡

Key Point: Converting with the wrong zone silently shifts dates by a day near midnight — always state the zone explicitly.

Key Concepts

Date.toInstant()atZone()toLocalDate()Date.from()

Approach 2: java.sql.Date

Java
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

java.sql.Date: 2026-03-09 Back to LocalDate: 2026-03-09 Round trip equal: true

Core Logic

java.sql.Date represents a date without a time, so it converts to and from LocalDate directly.

How It Works
  1. 1java.sql.Date.valueOf(day) creates the SQL date.
  2. 2sqlDate.toLocalDate() converts it back.
  3. 3The round trip gives an equal LocalDate.
2026-03-09 survives the round trip unchanged.
💡

Key Point: Modern JDBC drivers accept LocalDate directly via setObject, so the conversion is only needed for older code.

Key Concepts

java.sql.Date.valueOf()toLocalDate()

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()).

Learn the Concept

Related Programs