- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a Date object to LocalDate object in java?
To convert a Date object to LocalDate object in Java −
Convert the obtained date object to an Instant object using the toInstant() method.
Instant instant = date.toInstant();
Create the ZonedDateTime object using the atZone() method of the Instant class.
ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
Finally, convert the ZonedDateTime object to the LocalDate object using the toLocalDate() method.
LocalDate givenDate = zone.toLocalDate();
Example
Following example accepts a name and Date of birth from the user in String formats and converts it to LocalDate object and prints it.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Scanner; public class DateToLocalDate { public static void main(String args[]) throws ParseException { //Reading name and date of birth from the user Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String name = sc.next(); System.out.println("Enter your date of birth (dd-MM-yyyy): "); String dob = sc.next(); //Converting String to Date SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Date date = formatter.parse(dob); //Converting obtained Date object to LocalDate object Instant instant = date.toInstant(); ZonedDateTime zone = instant.atZone(ZoneId.systemDefault()); LocalDate localDate = zone.toLocalDate(); System.out.println("Local format of the given date of birth String: "+localDate); } }
Output
Enter your name: Krishna Enter your date of birth (dd-MM-yyyy): 26-09-1989 Local format of the given date of birth String: 1989-09-26
- Related Articles
- How to convert a Python date string to a date object?
- How to convert a JavaScript date object to a string?
- How to convert a string in to date object in JavaScript?
- How to create date object in Java?
- How to convert a timestamp object in to Date in JDBC program?
- How to convert a Date object in to Timestamp in JDBC program?
- How to convert an integer into a date object in Python?
- Convert Java String Object to Boolean Object
- How to convert OpenCV Mat object to BufferedImage object using Java?
- How to convert InputStream object to a String in Java?
- How to convert a date object to string with format hh:mm:ss in JavaScript?
- Java Program to convert from a java.util.Date Object to a java.sql.Date Object
- How to convert a String into a Date object using JDBC API?
- How to convert ISO 8601 string to Date/time object in Android?
- How to convert a date object's content into json in JavaScript?

Advertisements