JDBC中时间的时间转换,时间戳的转换

 时间戳:单位为毫秒,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数

String sql = "select update_date from baidu_cloud.t_user";
ResultSet rs = JDBCTemplate.executeQuery(sql);
System.out.println("----------------------Date转String------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(date.getTime());//单位为毫秒,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
System.out.println(date);
System.out.println(date.getClass().getName());
System.out.println(sdf.format(date.getTime()));
System.out.println(sdf.format(date.getTime()).getClass().getName());
System.out.println(sdf.format(date));
System.out.println(sdf.format(date).getClass().getName());
System.out.println("-------------------Timestamp转String----------------------");
Timestamp timestamp = new Timestamp(date.getTime());
System.out.println(timestamp.getTime());
System.out.println(timestamp);
System.out.println(timestamp.getClass().getName());
System.out.println(sdf.format(timestamp));
System.out.println(sdf.format(timestamp).getClass().getName());
System.out.println("--------------时间差(及保留小数位数)------------------------");
List<Timestamp> list = new ArrayList<>();
if (rs != null) {
    while (rs.next()) {
        list.add(rs.getTimestamp("update_date"));
    }
}
System.out.println(list);
float f = list.get(1).getTime() - list.get(0).getTime();
System.out.println("时间相差:" + String.format("%.2f", f / 1000 / 60) + "分钟");
System.out.println("--------------String转Date和Timestamp---------------------");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf2.parse(" 2008-07-10 19:20:00 ");
Timestamp timestamp1 = new Timestamp(date1.getTime());
System.out.println(date1);
System.out.println(date1.getClass().getName());
System.out.println(timestamp1);
System.out.println(timestamp1.getClass().getName());

运行结果:

----------------------Date转String------------------------
1560329785874
Wed Jun 12 16:56:25 CST 2019
java.util.Date
2019-06-12 16:56:25
java.lang.String
2019-06-12 16:56:25
java.lang.String
-------------------Timestamp转String----------------------
1560329785874
2019-06-12 16:56:25.874
java.sql.Timestamp
2019-06-12 16:56:25
java.lang.String
--------------时间差(及保留小数位数)------------------------
[2019-06-12 13:22:33.0, 2019-06-12 14:47:58.0]
时间相差:85.42分钟
--------------String转Date和Timestamp---------------------
Thu Jul 10 19:20:00 CST 2008
java.util.Date
2008-07-10 19:20:00.0
java.sql.Timestamp

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值