to java time Instant - Java java.time

Java examples for java.time:Instant

Description

to java time Instant

Demo Code

/*//from   www  . ja  v a 2  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.
 */
//package com.java2s;
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 Tutorials