PostgreSql中的timestamp用法

一 PostgreSQL 中的时间类型如下

NameStorage SizeDescriptionLow ValueHigh ValueResolution
timestamp [ (p) ] [ without time zone ]8 bytesboth date and time (no time zone)4713 BC294276 AD1 microsecond / 14 digits
timestamp [ (p) ] with time zone8 bytesboth date and time, with time zone4713 BC294276 AD1 microsecond / 14 digits
date4 bytesdate (no time of day)4713 BC5874897 AD1 day
time [ (p) ] [ without time zone ]8 bytestime of day (no date)00:00:0024:00:001 microsecond / 14 digits
time [ (p) ] with time zone12 bytestimes of day only, with time zone00:00:00+145924:00:00-14591 microsecond / 14 digits
interval [ fields ] [ (p) ]12 bytestime interval-178000000 years178000000 years1 microsecond / 14 digits

 
  备注:这里不准备详细介绍各种类型,请注意上面表格中的[ (p) ] ,这个是什么呢?这个是指时间的精度,
             time, timestamp, 和 interval 类型都可以指定精度,精度的取值范围是 0 到 6, 下面通过具体
             实验来体验下精度。
       
       

二 current_timestamp 实验
--2.1 查询  current_timestamp

 skytf=> select current_timestamp;
              now             
-------------------------------
 2012-06-07 14:00:02.412827+08
(1 row)        
   

 备注: current_timestamp 函数返回时间类型为 timestamp with  time zone,故返回结果后面包括时区 +08 ,
              以及精度 412827,那么如何去掉精度和时区呢?
 
       
--2.2 去掉精度

 skytf=> select current_timestamp(0);
      timestamptz      
------------------------
 2012-06-07 14:07:17+08
(1 row)
   


--2.3 去掉时区

 skytf=> select current_timestamp(0)::timestamp without time zone;
      timestamp     
---------------------
 2012-06-07 14:07:49
(1 row)
   


--2.4 也可以用 cast 函数类型转换

 skytf=> select cast (current_timestamp(0) as  timestamp without time zone);
      timestamp     
---------------------
 2012-06-07 14:14:55
(1 row)
   
                 
           
--2.5 了解 [p] 的含义

 skytf=> select current_timestamp(2)::timestamp without time zone;
       timestamp       
------------------------
 2012-06-07 14:15:42.64
(1 row)

skytf=> select current_timestamp(6)::timestamp without time zone;
         timestamp         
----------------------------
 2012-06-07 14:15:46.281422
(1 row)

   

   备注:可见 [p] 是指时间类型小数点后面的精度,如果 p 指定 2,则精度为2,如果 p 指定 6
              则精度为 6; 所以在定义表的时候就应该事先定义 timestamp 时间类型的精度。
        


三 创建表测试,定义时间类型精度为0

 skytf=> create table test_p (id int4 primary key, create_time  timestamp(0) without time zone);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_p_pkey" for table "test_p"
CREATE TABLE

skytf=> \d test_p
                   Table "skytf.test_p"
   Column    |              Type              | Modifiers
-------------+--------------------------------+-----------
 id          | integer                        | not null
 create_time | timestamp(0) without time zone |
Indexes:
    "test_p_pkey" PRIMARY KEY, btree (id)

skytf=> select current_timestamp;
              now             
-------------------------------
 2012-06-07 14:18:31.683105+08
(1 row)
                             
skytf=> insert into test_p values (1,current_timestamp);
INSERT 0 1

skytf=> select * from test_p;
 id |     create_time    
----+---------------------
  1 | 2012-06-07 14:19:02
(1 row)                    

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java,可以使用`java.sql.Timestamp`类来接收PostgreSQLtimestamp数据类型。`java.sql.Timestamp`是java.sql包的一个类,它表示SQL TIMESTAMP类型的值。 你可以使用`ResultSet`对象的`getTimestamp`方法来检索数据库timestamp值,并将其转换为`java.sql.Timestamp`类型。以下是一个示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; public class PostgresTimestampExample { public static void main(String[] args) { String url = "jdbc:postgresql://localhost:5432/mydatabase"; String user = "username"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT timestamp_column FROM mytable")) { while (rs.next()) { Timestamp timestamp = rs.getTimestamp("timestamp_column"); System.out.println(timestamp); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 在上述示例,我们通过执行SQL查询从PostgreSQL数据库检索了一个timestamp列。然后,使用`getTimestamp`方法将其转换为`java.sql.Timestamp`类型,并进行进一步处理。 请确保在使用`java.sql.Timestamp`之前导入正确的包(import语句): `import java.sql.Timestamp;`。 希望这个例子能够帮助你在Java接收和处理PostgreSQLtimestamp数据类型。如果有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值