ADVERTISEMENT
ADVERTISEMENT

Java program to create a LocalDate object from the object of Date class

Learn how to create a LocalDate object from the object of Date class in Java?
Submitted by Nidhi, on April 01, 2022

Problem Solution:

Create a LocalDate object from the object of the Date class and print the result.

Program/Source Code:

The source code to create a LocalDate object from the object of the Date class is given below. The given program is compiled and executed successfully.

// Java program to convert Date object 
// into LocalDate

import java.io.*;
import java.time.*;
import java.util.Date;

class Main {
  public static void main(String args[]) {
    Date date = new Date();

    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

    System.out.println("Local date is: " + localDate);
  }
}

Output:

Day  : 1
Month: APRIL
Year : 2022

Explanation:

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created an object of the Date class. Then we created the object of LocalDate class using Date class object using toInstant(), atZone(), and toLocalDate() methods. After that, we printed the result.

Java Date and Time Programs »



Related Programs

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.