How to convert Joda LocalDate to java.util.Date ?

How to convert Joda LocalDate to java.util.Date ?

Asked on December 26, 2018 in Java.
Add Comment


  • 5 Answer(s)

    In JodaTime :
    Here is the code for convert JodaTime’s org.joda.time.LocalDate to java.util.Date, do the code below,

    Date date = localDate.toDateTimeAtStartOfDay().toDate();
    
    

    The conversion of JodaTime’s org.joda.time.LocalDateTime to java.util.Date, do this.

    Date date = localDateTime.toDate();
    
    

    In JavaTime:

    If we want to convert Java8’s java.time.LocalDate to java.util.Date, use this code.

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

    To convert Java8’s java.time.LocalDateTime to java.util.Date, try to use this,

    Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    
    

    We may convined shorten with LocalDateTime#toInstant(ZoneOffset), but there is  not a direct API to obtain the system default zone offset here.

    To convert Java8’s java.time.ZonedDateTime to java.util.Date, do

    Date date = Date.from(zonedDateTime.toInstant());
    
    
    Answered on December 26, 2018.
    Add Comment

    We have to use timezone, by using the time zone one can get the solution easily like this,

    LocalDate date = ...
    Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( );
    
    Answered on December 26, 2018.
    Add Comment

    The LocalDate has a toDate() method since version 2.0 java,

    Date date = localDate.toDate();
    
    

    But incase we have the version 1.5 – 2.0, try to use this code:

    Date date = localDate.toDateTimeAtStartOfDay().toDate();
    
    

    Use this code in older versions, it should works.

    Date date = localDate.toDateMidnight().toDate();
    
    
    Answered on December 26, 2018.
    Add Comment

    java.util.Date and vice versa. In the first part of the code snippet we convert LocalDate to Date and back to LocalDateobject. 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);
        }
    }
    Answered on January 16, 2019.
    Add Comment

    Since 2.0 version LocalDate has a toDate() method

    Date date = localDate.toDate();

    If using version 1.5 – 2.0 use:

    Date date = localDate.toDateTimeAtStartOfDay().toDate();

    On older versions you are left with:

    Date date = localDate.toDateMidnight().toDate();
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

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