Java Date to LocalDate asLocalDate(final Date date)

Here you can find the source of asLocalDate(final Date date)

Description

Converts a Date into a LocalDate using the default timezone.

License

Open Source License

Parameter

Parameter Description
date Date to convert

Return

an equivalent LocalDate or null if the supplied date was null

Declaration

public static LocalDate asLocalDate(final Date date) 

Method Source Code

//package com.java2s;
/*/*ww  w.  jav a2s .co  m*/
 * jGnash, a personal finance application
 * Copyright (C) 2001-2019 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.time.Instant;
import java.time.LocalDate;

import java.time.ZoneId;

import java.util.Date;

public class Main {
    /**
     * Converts a {@code Date} into a {@code LocalDate} using the default timezone.
     *
     * @param date {@code Date} to convert
     * @return an equivalent {@code LocalDate} or {@code null} if the supplied date was {@code null}
     */
    public static LocalDate asLocalDate(final Date date) {
        if (date != null) {
            return asLocalDate(date.getTime());
        }

        return null;
    }

    /**
     * Converts milliseconds from the epoch of 1970-01-01T00:00:00Z into a {@code LocalDate} using the default timezone.
     *
     * @param milli milliseconds from the epoch of 1970-01-01T00:00:00Z.
     * @return an equivalent {@code LocalDate} or {@code null} if the supplied date was {@code null}
     */
    public static LocalDate asLocalDate(final long milli) {
        return Instant.ofEpochMilli(milli).atZone(ZoneId.systemDefault()).toLocalDate();
    }
}

Related

  1. asLocalDate(Date date)
  2. dateToLocalDate(Date d)