Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs’ (also known as Joda time APIs’) present in java.time package.
Java 7: Date, Calendar, TimeStamp present inside java.util package.
Java 8: Time Date APIs’ present inside the java.time package.
Different ways to Convert java.util.Date to java.time.LocalDate:
Using Instance and ZonedDateTime
Using Instant and Date.getTime()
Using ofInstant() and ZoneId.systemDefault()
Method 1: Using Instance and ZonedDateTime
Approach:
First we will convert the date object to an instant object.
Every instant object is associated with the zoneId so we need to give the zoneId.
At last we will convert it into LocalDate object.
Example:
In this program, we will convert the date object into instant using toInstant() method which returns instant object and takes no parameters.
Now we need to assign zoneId to it using atZone() method in this we need to pass the zone Id as a parameter for default we can use ZoneId.systemDefault() .
At last, we will convert it into a LocalDate object using the toLocalDate() method.
Syntax:
date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
Java
import java.io.*;
import java.time.*;
import java.util.Date;
class GFG {
public static void main(String[] args)
{
Date current = new Date();
LocalDate local = current.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
System.out.println(local);
}
}
Method 2: Using Instant and Date.getTime()
This approach is almost similar to the first one the only difference will be the way the instant object will be created.
Here we will use Instant.ofEpochMilli() method, it takes date as an argument.
After that again we will use atZone() method for adding the ZoneId followed by toLocalDate() method to get the LocalDate Object.
Syntax:
Instant.ofEpochMilli(date.getTime()) .atZone(ZoneId.systemDefault()).toLocalDate()
Java
import java.io.*;
import java.time.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Date current = new Date();
LocalDate local = Instant.ofEpochMilli(current.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDate();
System.out.println(local);
}
}
Method 3: Using ofInstant() and ZoneId.systemDefault()
It is the easiest approach we will use Java 9’s ofInstant() method of LocalDate class it takes two parameters.
A first parameter is an instant object of date and the second is ZoneId.
Here in the first argument, we will use toInstant() method to get the instant object and for the second argument we will use ZoneId.systemDefault() .
Syntax
LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());
Java
import java.io.*;
import java.time.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Date current = new Date();
LocalDate local = LocalDate.ofInstant(
current.toInstant(), ZoneId.systemDefault());
System.out.println(local);
}
}
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Last Updated :
07 Jan, 2021
Like Article
Save Article