Related Articles

Related Articles

Different Ways to Convert java.util.Date to java.time.LocalDate in Java
  • Last Updated : 07 Jan, 2021

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:

  1. Using Instance and ZonedDateTime
  2. Using Instant and Date.getTime()
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert java.util.Date to
// java.time.LocalDate
//  Using Instance and ZonedDateTime
  
import java.io.*;
import java.time.*;
import java.util.Date;
  
class GFG {
    public static void main(String[] args)
    {
  
        // first create date object
        Date current = new Date();
  
        // first convert the date object to instant using
        // toInstant() then add zoneId using .atZone() at
        // last convert into localDate using toLocalDate()
  
        LocalDate local = current.toInstant()
                  .atZone(ZoneId.systemDefault())
                  .toLocalDate();
  
        // printing the local date object
        System.out.println(local);
    }
}

chevron_right


Output

2020-12-21

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert java.util.Date to
// java.time.LocalDate
// Using Instant and Date.getTime()
  
import java.io.*;
import java.time.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // first create date object
        Date current = new Date();
  
        // first create instant object using
        // Instant.ofEpochMilli(date.getTime()) than add
        // zoneId using .atZone() at last convert into
        // localDate using toLocalDate()
        LocalDate local = Instant.ofEpochMilli(current.getTime())
                  .atZone(ZoneId.systemDefault())
                  .toLocalDate();
  
        // printing the local date object
        System.out.println(local);
    }
}

chevron_right


Output

2020-12-21

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert java.util.Date to
// java.time.LocalDate
//  Using ofInstant() and ZoneId.systemDefault()
  
import java.io.*;
import java.time.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // first create date object
        Date current = new Date();
  
        // use ofInstant method of LocalDate class
        // pass instant object and zone id as parameters
  
        LocalDate local = LocalDate.ofInstant(
            current.toInstant(), ZoneId.systemDefault());
  
        // printing the local date object
        System.out.println(local);
    }
}

chevron_right


Output

2020-12-21

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :