to sql Timestamp - Java java.sql

Java examples for java.sql:Timestamp

Description

to sql Timestamp

Demo Code

/*/*from w  w w.  j av a2s. co  m*/
 * Copyright 2013 FasterXML.com
 *
 * 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.
 */
//package com.java2s;
import java.math.BigDecimal;
import java.sql.Timestamp;

public class Main {
    private static final BigDecimal ONE_BILLION = new BigDecimal(
            1000000000L);

    public static Timestamp toTimestamp(BigDecimal value) {

        long seconds = value.longValue();
        int nanoseconds = extractNanosecondDecimal(value, seconds);

        Timestamp ts = new Timestamp(seconds * 1000);
        ts.setNanos(nanoseconds);
        return ts;
    }

    public static int extractNanosecondDecimal(BigDecimal value,
            long integer) {
        return value.subtract(new BigDecimal(integer))
                .multiply(ONE_BILLION).intValue();
    }
}

Related Tutorials