Java Instant Create toInstant(BigDecimal value)

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

Description

to Instant

License

Apache License

Declaration

public static Instant toInstant(BigDecimal value) 

Method Source Code


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

import java.math.BigDecimal;

import java.time.Instant;

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

    public static Instant toInstant(BigDecimal value) {

        long seconds = value.longValue();
        int nanoseconds = extractNanosecondDecimal(value, seconds);
        return Instant.ofEpochSecond(seconds, nanoseconds);
    }

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

Related

  1. getInstant(final Calendar calendar)
  2. getInstant(LocalDate localDate)
  3. getInstantOfEpoch(long seconds)
  4. getThisMonthBeginInstant()
  5. instant(int _y, int _m, int _d, int _H, int _M, int _S)