Hive中日期与时间戳转换

1.时间戳转成日期

select distinct  from_unixtime(1441565203,'yyyy/MM/dd HH:mm:ss') from test_date; 

2.日期转成时间戳

select distinct unix_timestamp('20111207 13:01:03') from test_date; // 默认格式为“yyyy-MM-dd HH:mm:ss“

select distinct unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from test_date;   

3.yyyymmdd和yyyy-mm-dd日期之间的切换

方法1: from_unixtime+ unix_timestamp
--20181205转成2018-12-05 
select from_unixtime(unix_timestamp('20181205','yyyymmdd'),'yyyy-mm-dd') from dual;

--2018-12-05转成20181205
select from_unixtime(unix_timestamp('2018-12-05','yyyy-mm-dd'),'yyyymmdd') from dual;

方法2: substr + concat
--20181205转成2018-12-05 
select concat(substr('20181205',1,4),'-',substr('20181205',5,2),'-',substr('20181205',7,2)) from dual;

--2018-12-05转成20181205
select concat(substr('2018-12-05',1,4),substr('2018-12-05',6,2),substr('2018-12-05',9,2)) from dual;

1.值得注意的是,时间戳有可能是毫秒级的,然后这时候直接使用from_unixtime(1441565203,'yyyy/MM/dd HH:mm:ss')的话就会得到很奇怪的日期了,这时候要这样from_unixtime(cast(151331629920/1000 as int)),同样的,时间转成毫秒级的时间戳也要乘以1000,如:unix_timestamp('2018-12-18 00:38:50')*1000

2.如何区分时间戳是秒级还是毫秒级呢?一般来说,常见的时间戳是10位数的,13位数的时间戳就是毫秒级的

 

附:在线时间戳与时间之间的转化:https://tool.lu/timestamp/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值