Java SQL Date From toSQLDate(java.util.Date date)

Here you can find the source of toSQLDate(java.util.Date date)

Description

Returns the specified object as an instance of an SQL-compatible subclass of java.util.Date : either java.sql.Date or java.sql.Timestamp , depending on the presence of the time part.

License

Apache License

Parameter

Parameter Description
date the value to be converted

Return

an SQL-compatible object

Declaration

public static java.util.Date toSQLDate(java.util.Date date) 

Method Source Code

//package com.java2s;
/*//from w  w w  . j a v  a  2  s.com
 * Copyright 2014-2015 Victor Osolovskiy, Sergey Navrotskiy
 *
 * 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.util.Calendar;

public class Main {
    /**
     * Returns the specified object as an instance of an SQL-compatible subclass of {@link java.util.Date}:
     * either {@link java.sql.Date} or {@link java.sql.Timestamp}, depending on the presence of the time part.
     *
     * @param date the value to be converted
     * @return an SQL-compatible object
     */
    public static java.util.Date toSQLDate(java.util.Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        long msec = cal.getTimeInMillis();
        boolean has_time_part = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) + cal.get(Calendar.SECOND)
                + cal.get(Calendar.MILLISECOND) != 0;
        return has_time_part ? (java.util.Date) new java.sql.Timestamp(msec) : new java.sql.Date(msec);
    }
}

Related

  1. toSqlDate(Date date)
  2. toSqlDate(Date date)
  3. toSqlDate(Date dt)
  4. toSqlDate(final Date date)
  5. toSqlDate(final String _text, final String _dateFormat)
  6. toSQLDate(java.util.Date date)
  7. toSqlDate(java.util.Date date)
  8. toSQLDate(Object object, Object oDefault)
  9. toSqlDate(String date)