b>j)΄!Pԫ&;"kB޶}pSVT(wę!j x;-m@JnQ+պכ7MajfJͱ4jѲ撆RxZMz7vIW/dٞТזcZM~ji ߒsQzԠDW3Den"M+/B:-uIJ7j委9p='mANޭ=/B:-n&nUfqxZM~c Ϲ+,&ᾺܢF[(1*" ϒ"Jԧ<;b" "jܢF[x ,!q қ*]/؝27SMcs"ޭDQ/应ܢF_! :s" 7`F+SVTn"IJnQ/应B 4 wD"IJ׭-`S9DrjiEJ߅gJ应矁[xZM~n"IB؃!'Тѕ+(mIKʭ/|ϐܢF[xZMzG %嬩/c[[ Java 8 – Convert LocalDate and LocalDateTime to Date - Mkyong.com

Java 8 – Convert LocalDate and LocalDateTime to Date

A Java example to convert Java 8 java.time.LocalDate and java.time.LocalDateTime back to the classic java.uti.Date.

JavaDateExample.java

package com.mkyong.time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class JavaDateExample {

    public static void main(String[] args) {

        // LocalDate -> Date
        LocalDate localDate = LocalDate.of(2020, 2, 20);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        // LocalDateTime -> Date
        LocalDateTime localDateTime = LocalDateTime.of(2020,2,20,21,46,31);
        Date date2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        // ZonedDateTime -> Date
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        Date date3 = Date.from(zonedDateTime.toInstant());

    }

}

Output


Thu Feb 20 00:00:00 MYT 2020
Thu Feb 20 21:46:31 MYT 2020
Thu Feb 20 21:46:31 MYT 2020

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments