Here you can find the source of toSQLDate(java.util.Date date)
Parameter | Description |
---|---|
date | the value to be converted |
public static java.util.Date toSQLDate(java.util.Date date)
//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); } }