Java Timestamp Create toTimestamp(BigDecimal value)

Here you can find the source of toTimestamp(BigDecimal value)

Description

to Timestamp

License

Apache License

Declaration

public static Timestamp toTimestamp(BigDecimal value) 

Method Source Code


//package com.java2s;
/*/*from ww  w  . ja  v  a2  s. com*/
 * 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.
 */

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

  1. toSQLTimestamp(Date date)
  2. toSQLTimestamp(DateTime dt)
  3. toSqlTimestamp(java.util.Date date)
  4. toSQLTimestamp(Object object, Object oDefault)
  5. toSQLTimestamp(String time)
  6. toTimestamp(Date date)
  7. toTimestamp(Date date)
  8. toTimestamp(Date date)
  9. toTimeStamp(Date date, String time)