Java LocalDate to fromLocalDate(LocalDate date, TimeZone timeZone)

Here you can find the source of fromLocalDate(LocalDate date, TimeZone timeZone)

Description

Convert a LocalDate value into a Date , using default calendar.

License

Apache License

Parameter

Parameter Description
date Date to convert (may be null)
timeZone The time zone to use

Return

Converted , or null if given date was null

Declaration

public static Date fromLocalDate(LocalDate date, TimeZone timeZone) 

Method Source Code

//package com.java2s;
/*/*from ww w.  j a v  a 2  s . co m*/
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.time.LocalDate;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Convert a {@link LocalDate} value into a {@link Date}, using default calendar and default time zone.
     * @param date Date to convert (may be null)
     * @return Converted {@link Date}, or <code>null</code> if given date was <code>null</code>
     */
    public static Date fromLocalDate(LocalDate date) {
        return fromLocalDate(date, null);
    }

    /**
     * Convert a {@link LocalDate} value into a {@link Date}, using default calendar.
     * @param date Date to convert (may be null)
     * @param timeZone The time zone to use
     * @return Converted {@link Date}, or <code>null</code> if given date was <code>null</code>
     */
    public static Date fromLocalDate(LocalDate date, TimeZone timeZone) {
        if (date != null) {
            final Calendar c = Calendar.getInstance();
            if (timeZone != null) {
                c.setTimeZone(timeZone);
            }
            c.set(Calendar.YEAR, date.getYear());
            c.set(Calendar.MONTH, date.getMonthValue() - 1);
            c.set(Calendar.DAY_OF_MONTH, date.getDayOfMonth());
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            return c.getTime();
        }
        return null;
    }
}

Related

  1. from(LocalDate localDate, LocalTime localTime)
  2. from(LocalDate localDate, LocalTime localTime, ZoneOffset zoneOffset)
  3. fromLocalDate(LocalDate date)
  4. toBadDate(LocalDate localDate, ZoneId timezone)
  5. toCalendar(LocalDate localDate)
  6. toDBString(LocalDate date)