BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – Convert LocalDate to ZonedDateTime

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial, we will see how to convert LocalDate to ZonedDateTime. LocalDate represents the date without time and zone information, ZonedDateTime represents the date with time and zone. To convert the LocalDate to ZonedDateTime, we must add the time and zone id with the local date.

Example 1: Converting the LocalDate given in String format to the ZonedDateTime

In this example, we have date in the string format, we are first parsing the given date in LocalDate and then converting the LocalDate to ZonedDateTime using the atStartOfDay() method.

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

public class Example {
   public static void main(String[] args) {
	  //Parsing the given string into a LocalDate
      LocalDate localDate = LocalDate.parse("2017-07-22");
      
      //Displaying the LocalDate
      System.out.println("LocalDate is: "+localDate);  
      
      /* Converting the LocalDate to ZonedDateTime by using the 
       * default zone id and appending the midnight time and default
       * zone id to the local date using atStartOfDay() method
       */
      ZonedDateTime zonedDateTime = 
    		  localDate.atStartOfDay(ZoneId.systemDefault());
      
      //Displaying the ZonedDateTime
      System.out.println("ZoneDateTime is: "+zonedDateTime);  
   }
}

Output:

LocalDate is: 2017-07-22
ZoneDateTime is: 2017-07-22T00:00+05:30[Asia/Kolkata]

Example 2: LocalDate to ZonedDateTime

In the first example we have the date as String. In this example we have the day, month and year information, using which we are creating an instance of LocalDate and then appending the time and zone id with it using the atStartOfDay() method, the same way that we have seen in the above program.

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

public class Example {
   public static void main(String[] args) {
	  //Converting the given year, month, date to LocalDate
      LocalDate localDate = LocalDate.of(2017, 07, 22);
      
      //printing the local date 
      System.out.println("LocalDate is: "+localDate);  
      
      /* Converting the LocalDate to ZonedDateTime the same way that
       * we have seen in the above example
       */
      ZonedDateTime zonedDateTime = 
    		  localDate.atStartOfDay(ZoneId.systemDefault());
      
      //Displaying the output ZonedDateTime
      System.out.println("ZoneDateTime is: "+zonedDateTime);  
   }
}

Output:

LocalDate is: 2017-07-22
ZoneDateTime is: 2017-07-22T00:00+05:30[Asia/Kolkata]

Related Posts:

  1. Java LocalDate
  2. Java – Convert LocalDate to Date
  3. Java – Convert Date to LocalDate
  4. Java LocalDate – adjustInto() example

Top Related Articles:

  1. Java LocalDateTime
  2. Java – Date Format to display the Day of the week
  3. Java DateTimeFormatter
  4. Java LocalDate – adjustInto() method example
  5. How to Call a Method in Java

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap