Java Timestamp Convert To timestampToMicros(Timestamp timestamp)

Here you can find the source of timestampToMicros(Timestamp timestamp)

Description

Converts a Timestamp to microseconds since the Unix epoch (1970-01-01T00:00:00Z).

License

Apache License

Parameter

Parameter Description
timestamp the timestamp to convert to microseconds

Return

the microseconds since the Unix epoch

Declaration

public static long timestampToMicros(Timestamp timestamp) 

Method Source Code


//package com.java2s;
// Licensed to the Apache Software Foundation (ASF) under one

import java.sql.Timestamp;

public class Main {
    /**/*from ww  w .  jav a2 s.c om*/
     * Converts a {@link Timestamp} to microseconds since the Unix epoch (1970-01-01T00:00:00Z).
     *
     * Note: Timestamp instances with nanosecond precision are truncated to microseconds.
     *
     * @param timestamp the timestamp to convert to microseconds
     * @return the microseconds since the Unix epoch
     */
    public static long timestampToMicros(Timestamp timestamp) {
        // Number of whole milliseconds since the Unix epoch, in microseconds.
        long millis = timestamp.getTime() * 1000L;
        // Sub millisecond time since the Unix epoch, in microseconds.
        long micros = (timestamp.getNanos() % 1000000L) / 1000L;
        if (micros >= 0) {
            return millis + micros;
        } else {
            return millis + 1000000L + micros;
        }
    }
}

Related

  1. TimestampToDateStr(Timestamp tmp)
  2. timestampToDateString(Timestamp ts)
  3. timestampToDayNumber(long timestamp)
  4. timestampToInternal(java.sql.Timestamp ts)
  5. timestampToLong(Timestamp ts)
  6. timestampToParts(long time)
  7. timestampToPrettyString(long ts)
  8. timestampToSearchString(final Timestamp timestamp)
  9. timestampToString(long time)