Date to LocalDate - Java java.time

Java examples for java.time:LocalDate

Description

Date to LocalDate

Demo Code


//package com.java2s;
import java.time.*;
import java.util.Date;

public class Main {
    /**/*from w ww .ja v  a  2s  .c om*/
     * Calls {@link #asLocalDate(Date, ZoneId)} with the system default time zone.
     */
    public static LocalDate asLocalDate(Date date) {
        return asLocalDate(date, ZoneId.systemDefault());
    }

    /**
     * Creates {@link LocalDate} from {@code java.util.Date} or it's subclasses. Null-safe.
     */
    public static LocalDate asLocalDate(Date date, ZoneId zone) {
        if (date == null)
            return null;

        if (date instanceof java.sql.Date)
            return ((java.sql.Date) date).toLocalDate();
        else
            return Instant.ofEpochMilli(date.getTime()).atZone(zone)
                    .toLocalDate();
    }
}

Related Tutorials