Timestamp

概述

A thin wrapper around <code>java.util.Date</code> that allows the JDBC API to identify this as an SQL <code>TIMESTAMP</code> value.
It adds the ability to hold the SQL <code>TIMESTAMP</code> fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds.
A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values.

Timestamp是java.util.Date的wrapper,允许JDBC API识别作为一个SQL的时间戳值;

Timestamp提供了持有纳秒值的能力;

 

This type is a composite of a <code>java.util.Date</code> and a separate nanoseconds value.
Only integral seconds are stored in the <code>java.util.Date</code> component.
The fractional seconds - the nanos - are separate.
The <code>Timestamp.equals(Object)</code> method never returns <code>true</code> when passed an object that isn't an instance of <code>java.sql.Timestamp</code>, because the nanos component of a date is unknown.
As a result, the <code>Timestamp.equals(Object)</code> method is not symmetric with respect to the <code>java.util.Date.equals(Object)</code> method.
Also, the <code>hashCode</code> method uses the underlying <code>java.util.Date</code> implementation and therefore does not include nanos in its computation.

Timestamp是Date和纳秒值的组合

整数秒 存在Date中,纳秒是分离的;

Timestamp.equals(Object)时,如果参数不是Timestamp的实例,永远不会返回true,因为没有纳秒值;

hashcode方法使用的是Date的,不包含纳秒;

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Timestamp extends java.util.Date {
 
        private int nanos;
         
        public Timestamp(long time) {
            super((time/1000)*1000);
            nanos = (int)((time%1000) * 1000000);
            if (nanos < 0) {
                nanos = 1000000000 + nanos;
                super.setTime(((time/1000)-1)*1000);
            }
        }
 
        // Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this <code>Timestamp</code> object.
        public long getTime() {
            long time = super.getTime();
            return (time + (nanos / 1000000));
        }
 
        // Gets this <code>Timestamp</code> object's <code>nanos</code> value.
        public int getNanos() {
            return nanos;
        }
    }

  

 

posted on   anpeiyong  阅读(50)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示