Java SQL Date Convert toInt(java.util.Date v)

Here you can find the source of toInt(java.util.Date v)

Description

Converts the Java type used for UDF parameters of SQL DATE type ( java.sql.Date ) to internal representation (int).

License

Apache License

Declaration

public static int toInt(java.util.Date v) 

Method Source Code


//package com.java2s;
/*// www  . j  a v  a 2s  . co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.   See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.Timestamp;

import java.util.TimeZone;

public class Main {
    private static final TimeZone LOCAL_TZ = TimeZone.getDefault();
    /**
     * The number of milliseconds in a day.
     *
     * <p>This is the modulo 'mask' used when converting
     * TIMESTAMP values to DATE and TIME values.
     */
    private static final long MILLIS_PER_DAY = 86400000L;

    /** Converts the Java type used for UDF parameters of SQL TIME type
     * ({@link java.sql.Time}) to internal representation (int).
     *
     * <p>Converse of {@link #internalToTime(int)}. */
    public static int toInt(java.sql.Time v) {
        return (int) (toLong(v) % MILLIS_PER_DAY);
    }

    /** Converts the Java type used for UDF parameters of SQL DATE type
     * ({@link java.sql.Date}) to internal representation (int).
     *
     * <p>Converse of {@link #internalToDate(int)}. */
    public static int toInt(java.util.Date v) {
        return toInt(v, LOCAL_TZ);
    }

    public static int toInt(java.util.Date v, TimeZone timeZone) {
        return (int) (toLong(v, timeZone) / MILLIS_PER_DAY);
    }

    public static long toLong(java.util.Date v) {
        return toLong(v, LOCAL_TZ);
    }

    /** Converts the Java type used for UDF parameters of SQL TIMESTAMP type
     * ({@link java.sql.Timestamp}) to internal representation (long).
     *
     * <p>Converse of {@link #internalToTimestamp(long)}. */
    public static long toLong(Timestamp v) {
        return toLong(v, LOCAL_TZ);
    }

    public static long toLong(java.util.Date v, TimeZone timeZone) {
        long time = v.getTime();
        return time + (long) timeZone.getOffset(time);
    }
}

Related

  1. toCalender(final Date aDate)
  2. todaySqlDate()
  3. toDBDate(String str)
  4. toEnglishDate(String stringDate)
  5. toGMT(final Date ts)
  6. toIntDate(Object object, int iDefault)
  7. toJavaDate(java.sql.Date date)
  8. toJavaDate(java.sql.Date sqlDate)
  9. toLong(java.util.Date v)