Open In App

Different Ways to Convert java.util.Date to java.time.LocalDate in Java

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:

Example:

Syntax:

date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()




// 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);
    }
}

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.

Syntax:

Instant.ofEpochMilli(date.getTime()) .atZone(ZoneId.systemDefault()).toLocalDate()




// 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);
    }
}

Output
2020-12-21

Method 3: Using ofInstant() and ZoneId.systemDefault()

Syntax

LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());




// 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);
    }
}

Output
2020-12-21

Article Tags :