Convert between LocalDate and ZonedDateTime

Learn to convert from LocalDate to ZonedDateTime and from ZonedDateTime to LocalDate in Java 8.

To restate, LocalDate represent a calendar date without time and timezone. ZonedDateTime instance contains both information.

ZonedDateTime = LocalDate + time + timezone

1. LocalDate to ZonedDateTime conversion

To convert a LocalDate instance to ZonedDateTime instance, we have two approaches.

1.1. LocalDate -> ZonedDateTime

If we only want to convert a local date in current timezone to localdate in different timezone i.e. only want to add zone information, then we can use LocalDate.atStartOfDay(zoneId) method.

LocalDate localDate = LocalDate.now();

ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("EST5EDT"));

System.out.println(zonedDateTime);

Program output.

2019-04-02T00:00-04:00[EST5EDT]

Read More : Date converion in ET timezone

1.2. LocalDate -> LocalDateTime -> ZonedDateTime

If we want to add both time and timezone information to a localdate, then we need to add both parts one by one to get to ZonedDateTime instance.

  • ZonedDateTime atStartOfDay()
  • ZonedDateTime atTime(LocalTime time)
  • ZonedDateTime atTime(int hour, int minutes)
  • ZonedDateTime atTime(int hour, int minutes, int seconds)
  • ZonedDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

Above methods will help in adding time parts to LocalDate. And then we can use LocalDateTime.atZone(ZoneId) method to add zone information.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        
        LocalDate localDate = LocalDate.now();  //local date

        LocalDateTime localDateTime = localDate.atTime(10, 45, 56);  //Add time information

        ZoneId zoneId = ZoneId.of("Asia/Kolkata"); // Zone information

        ZonedDateTime zdtAtAsia = localDateTime.atZone(zoneId); // add zone information

        ZonedDateTime zdtAtET = zdtAtAsia
                .withZoneSameInstant(ZoneId.of("America/New_York")); // Same time in ET timezone
        
        System.out.println(zdtAtAsia);
        System.out.println(zdtAtET);
    }
}

Program output.

2019-04-02T10:45:56+05:30[Asia/Kolkata]
2019-04-02T01:15:56-04:00[America/New_York]

2. ZonedDateTime to LocalDate conversion

To convert ZonedDateTime to LocalDate instance, use toLocalDate() method. It returns a LocalDate with the same year, month and day as given date-time.

import java.time.LocalDate;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        
        LocalDate localDate = zonedDateTime.toLocalDate();
        
        System.out.println(localDate);
    }
}

Program output.

2019-04-02

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.