Convert java.time.LocalDate into java.util.Date type

Convert java.time.LocalDate into java.util.Date type

Asked on December 20, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Try this solution:

    Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    

    Consider this  date choicer uses the system default timezone to convert dates into strings.

    Answered on December 20, 2018.
    Add Comment

    Here use efficiency  class  to convert the current  java.time classes to java.util.Date objects:

    import java.time.Instant;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.util.Date;
     
    public class DateUtils {
     
        public static Date asDate(LocalDate localDate) {
            return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        }
     
        public static Date asDate(LocalDateTime localDateTime) {
            return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
     
        public static LocalDate asLocalDate(Date date) {
            return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        }
     
        public static LocalDateTime asLocalDateTime(Date date) {
            return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
    }
    
    Answered on December 20, 2018.
    Add Comment

    Using  java.sql.Date.valueOf() method :

    Date date = java.sql.Date.valueOf(localDate);
    
    Answered on December 20, 2018.
    Add Comment

    convert java.time.LocalDate to java.util.Date and vice versa. In the first part of the code snippet we convert LocalDateto Date and back to LocalDate object. On the second part we convert LocalDateTime to Date and back to LocalDateTime object.

    package org.kodejava.example.datetime;
    
    import java.time.*;
    import java.util.Date;
    
    public class LocalDateToDate {
        public static void main(String[] args) {
            // Convert java.time.LocalDate to java.util.Date and back to
            // java.time.LocalDate
            LocalDate localDate = LocalDate.now();
            System.out.println("LocalDate = " + localDate);
    
            Date date1 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            System.out.println("Date      = " + date1);
    
            localDate = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            System.out.println("LocalDate = " + localDate);
            System.out.println();
    
            // Convert java.time.LocalDateTime to java.util.Date and back to
            // java.time.LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println("LocalDateTime = " + localDateTime);
    
            Date date2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            System.out.println("Date          = " + date2);
    
            localDateTime = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            System.out.println("LocalDateTime = " + localDateTime);
        }
    }
    

    The result of the code snippet:

    LocalDate = 2017-02-14
    Date      = Tue Feb 14 00:00:00 WITA 2017
    LocalDate = 2017-02-14
    
    LocalDateTime = 2017-02-14T10:34:49.562
    Date          = Tue Feb 14 10:34:49 WITA 2017
    LocalDateTime = 2017-02-14T10:34:49.562
    Answered on January 17, 2019.
    Add Comment

    LocalDateTime instance – we can similarly use an intermediary ZonedDateTime, and then using the toLocalDateTime() API.

    Just like before, we can use two possible solutions of getting an Instant object from java.util.Date:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
        return dateToConvert.toInstant()
          .atZone(ZoneId.systemDefault())
          .toLocalDateTime();
    }
    public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
        return Instant.ofEpochMilli(dateToConvert.getTime())
          .atZone(ZoneId.systemDefault())
          .toLocalDateTime();
    }
    Answered on January 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.