'java.util.Date' represent date + time + timezone whereas 'java.time.LocalDate' represent only date portion.
Below snippet converts the Date to LocalDate.
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
LocalDate localDate = zonedDateTime.toLocalDate();
package com.sample.app;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class App {
public static void main(String args[]) throws InterruptedException {
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
LocalDate localDate = zonedDateTime.toLocalDate();
System.out.println("localDate : " + localDate);
}
}
Output
localDate : 2020-03-07
You may
like
No comments:
Post a Comment