Java SQL Time extractTime(Date date)

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

Description

Returns a date with "zero" date.

License

Apache License

Parameter

Parameter Description
date the date object, not null

Return

the date, not null

Declaration

public static Date extractTime(Date date) 

Method Source Code


//package com.java2s;
/*//from  w ww.  jav a  2 s  . c o 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.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class Main {
    /**
     * Returns a date with "zero" date.
     *
     * @param date the date object, not null
     * @return the date, not null
     */
    public static Date extractTime(Date date) {
        return extractTime(date, getDefaultTimeZone());
    }

    /**
     * Returns a date with "zero" date.
     *
     * @param date   the date object, not null
     * @param zoneId the time zone id, not null
     * @return the date, not null
     */
    public static Date extractTime(Date date, ZoneId zoneId) {
        return asDate(asLocalTime(date, zoneId), zoneId);
    }

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

    /**
     * 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());
    }

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

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

Related

  1. dateTimeToStr(java.util.Date date)
  2. daytime()
  3. deserializeSqlTime(String text)
  4. double2Time(Number zahl)
  5. equalsTime(Time time1, Time time2)
  6. getCalTime(Time startTime, Calendar cal)
  7. getClassForSqlType(String sqlType, Class dateTimeClass)
  8. getClosestIDByTime(Connection con, String table, long time)
  9. getDateTimeValue(ResultSet result, String strField, String strDateFormat)