Java Timestamp Convert To toDecimal(Timestamp instant)

Here you can find the source of toDecimal(Timestamp instant)

Description

to Decimal

License

Apache License

Declaration

public static BigDecimal toDecimal(Timestamp instant) 

Method Source Code


//package com.java2s;
/*//from  www .  j  ava  2s  .  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.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;

public class Main {
    private static final char[] ZEROES = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0' };

    public static BigDecimal toDecimal(Duration instant) {
        return new BigDecimal(toDecimal(instant.getSeconds(), instant.getNano()));
    }

    public static BigDecimal toDecimal(Timestamp instant) {
        long millis = instant.getTime();
        long secs = millis / 1000;
        return new BigDecimal(toDecimal(secs, instant.getNanos()));
    }

    public static BigDecimal toDecimal(Instant instant) {
        return new BigDecimal(toDecimal(instant.getEpochSecond(), instant.getNano()));
    }

    public static String toDecimal(long seconds, int nanoseconds) {
        StringBuilder string = new StringBuilder(Integer.toString(nanoseconds));
        if (string.length() < 9)
            string.insert(0, ZEROES, 0, 9 - string.length());
        return seconds + "." + string;
    }
}

Related

  1. toCalendar(Timestamp stamp)
  2. toCalendar(Timestamp timestamp)
  3. toDate(java.sql.Timestamp timestamp)
  4. toDate(Timestamp timestamp)
  5. toDateTime(Timestamp value)
  6. toEnglishDateTime(java.sql.Timestamp ts)
  7. toEnglishHours(java.sql.Timestamp ts)
  8. toEPICSTime(java.sql.Timestamp timestamp)
  9. toFormatedString(final Timestamp ts, final String timeZoneId)