Converts the specified LocalDate object to Date. - Java java.time

Java examples for java.time:LocalDate

Description

Converts the specified LocalDate object to Date.

Demo Code


//package com.java2s;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

import java.time.ZoneId;

import java.util.Date;

public class Main {
    /**/* www . j  a  v  a 2 s.  c o  m*/
     * Converts the specified LocalDate object to Date.
     *
     * @param date LocalDate object containing the date (JSR 310)
     * @return the created Date with time fraction of 00:00:00
     */
    public static Date localDateToDate(final LocalDate date) {
        return localDateTimeToDate(date.atStartOfDay());
    }

    /**
     * Converts the specified LocalDateTime object to Date.
     *
     * @param dateTime LocalDateTime object containing the date and time (JSR 310)
     * @return the created Date
     */
    public static Date localDateTimeToDate(final LocalDateTime dateTime) {
        final Instant instant = dateTime.atZone(ZoneId.systemDefault())
                .toInstant();
        return Date.from(instant);
    }
}

Related Tutorials