Java SQL LocalDate asLocalDate(Date date)

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

Description

Obtains an instance of LocalDate from a date object.

License

Apache License

Parameter

Parameter Description
date the date object to convert, not null

Return

the local date, not null

Declaration

public static LocalDate asLocalDate(Date date) 

Method Source Code

//package com.java2s;
/*/*  w  ww .  j  a v  a 2 s  .  co m*/
 * Copyright (c) 2008-2018 Haulmont.
 *
 * 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.time.ZoneId;
import java.util.Date;

public class Main {
    /**
     * Obtains an instance of {@code LocalDate} from a date object.
     * <p>
     * If the date object is an instance of {@link java.sql.Date}, then {@link java.sql.Date#toLocalDate()} is used.
     * Otherwise {@code LocalDate} is obtained by converting {@code Instant} at {@link ZoneId#systemDefault()}
     * to {@code LocalDate}.
     *
     * @param date the date object to convert, not null
     * @return the local date, not null
     */
    public static LocalDate asLocalDate(Date date) {
        return asLocalDate(date, getDefaultTimeZone());
    }

    /**
     * Obtains an instance of {@code LocalDate} from a date object.
     * <p>
     * If the date object is an instance of {@link java.sql.Date}, then {@link java.sql.Date#toLocalDate()} is used.
     * Otherwise {@code LocalDate} is obtained by converting {@code Instant} at {@link ZoneId#systemDefault()}
     * to {@code LocalDate}.
     *
     * @param date   the date object to convert, not null
     * @param zoneId the time zone id, not null
     * @return the local date, not null
     */
    public static LocalDate asLocalDate(Date date, ZoneId zoneId) {
        return date instanceof java.sql.Date ? ((java.sql.Date) date).toLocalDate()
                : date.toInstant().atZone(zoneId).toLocalDate();
    }

    private static ZoneId getDefaultTimeZone() {
        // TODO: gg, return proper ZoneId
        return ZoneId.systemDefault();
    }
}

Related

  1. add(Date awal, int i)
  2. addDays(final Date date, final int daysToAdd)
  3. asLocalDate(Date date)
  4. asLocalDate(java.util.Date date, ZoneId zone)
  5. asUtilDate(Object date)
  6. asUtilDate(Object date, ZoneId zone)