Java LocalTime asDate(LocalTime localTime)

Here you can find the source of asDate(LocalTime localTime)

Description

Obtains an instance of Date from a local time object.

License

Apache License

Parameter

Parameter Description
localTime the local time object, not null

Return

the date, not null

Declaration

public static Date asDate(LocalTime localTime) 

Method Source Code

//package com.java2s;
/*//  ww  w.ja  v a2 s . c  om
 * 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.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class Main {
    /**
     * Obtains an instance of {@code Date} from a local time object.
     * <p>
     * A {@code Date} object represents the current date with the time represented by {@code LocalTime}.
     *
     * @param localTime the local time object, not null
     * @return the date, not null
     */
    public static Date asDate(LocalTime localTime) {
        return asDate(localTime, getDefaultTimeZone());
    }

    /**
     * Obtains an instance of {@code Date} from a local time object.
     * <p>
     * A {@code Date} object represents date represented by {@code LocalDate}
     * with time represented by {@code LocalTime}.
     *
     * @param localTime the local time object, not null
     * @param localDate the local date object, not null
     * @return the date, not null
     */
    public static Date asDate(LocalTime localTime, LocalDate localDate) {
        return asDate(localTime, localDate, getDefaultTimeZone());
    }

    /**
     * Obtains an instance of {@code Date} from a local time object.
     * <p>
     * A {@code Date} object represents the current date with the time represented by {@code LocalTime}.
     *
     * @param localTime the local time object, not null
     * @param zoneId    the time zone id, not null
     * @return the date, not null
     */
    public static Date asDate(LocalTime localTime, ZoneId zoneId) {
        return asDate(localTime, LocalDate.now(), zoneId);
    }

    /**
     * Obtains an instance of {@code Date} from a local time object.
     * <p>
     * A {@code Date} object represents the date represented by {@code LocalDate}
     * with the time represented by {@code LocalTime}.
     *
     * @param localTime the local time object, not null
     * @param localDate the local date object, not null
     * @param zoneId    the time zone id, not null
     * @return the date, not null
     */
    public static Date asDate(LocalTime localTime, LocalDate localDate, ZoneId zoneId) {
        return Date.from(localTime.atDate(localDate).atZone(zoneId).toInstant());
    }

    /**
     * Obtains an instance of {@code Date} from a local date object.
     *
     * @param localDate the local date object, not null
     * @return the date, not null
     */
    public static Date asDate(LocalDate localDate) {
        return asDate(localDate, getDefaultTimeZone());
    }

    /**
     * Obtains an instance of {@code Date} from a local date object.
     *
     * @param localDate the local date object, not null
     * @param zoneId    the time zone id, not null
     * @return the date, not null
     */
    public static Date asDate(LocalDate localDate, ZoneId zoneId) {
        return Date.from(localDate.atStartOfDay(zoneId).toInstant());
    }

    /**
     * Obtains an instance of {@code Date} from a local date-time object.
     *
     * @param localDateTime the local date-time object, not null
     * @return the date, not null
     */
    public static Date asDate(LocalDateTime localDateTime) {
        return asDate(localDateTime, getDefaultTimeZone());
    }

    /**
     * Obtains an instance of {@code Date} from a local date-time object.
     *
     * @param localDateTime the local date-time object, not null
     * @param zoneId        the time zone id, not null
     * @return the date, not null
     */
    public static Date asDate(LocalDateTime localDateTime, ZoneId zoneId) {
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }

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

Related

  1. convertLocalTimeToString(LocalTime time)
  2. currentTimeIsBetween(LocalTime from, LocalTime to)
  3. deserializeLocalTime(String date)
  4. fastTimeWrite(LocalTime localTime)