Hive常用时间函数小结

–日期 转换 时间戳
select unix_timestamp() --1640140182 bigint 当前时间戳

select unix_timestamp(‘2021-12-22 10:29:42’,‘yyyy-MM-dd hh:mm:ss’) --1640140182 bigint
select unix_timestamp(‘2021-12-22 10:29:42’);–1640140182 bigint 满足’yyyy-MM-dd hh:mm:ss’格式的,一个参数就可以
select unix_timestamp(‘2021-12-22’,‘yyyy-MM-dd’) – 1640102400 big int 不满足时间格式的,要指定格式

– 时间戳 转换 日期 返回 string 类型的时间
select from_unixtime(unix_timestamp(),‘yyyy-MM-dd hh:mm:ss’) as dt – string
select from_unixtime(1640140182,‘yyyy-MM-dd hh:mm:ss’) as date_time – string ‘2021-12-22 10:29:42’
select from_unixtime(1640140182,‘yyyy-MM-dd’) as dt – string ‘2021-12-22’
–特殊:如果unixtime为13位的,需要先转成10位
select from_unixtime(cast(1640140182488/1000 as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’
select from_unixtime(cast(substr(1640140182488,1,10) as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’

–日期字符串与日期相互转化
select to_date(‘2021-12-22 10:29:42’) – date 2021-12-22 字符串转换为日期
select cast(current_date() as string) – 直接将日期转化为 字符串 ‘2022-03-03’
select date_format(‘2021-12-22 10:29:42’,‘yyyy-MM-dd’) – string '2021-12-22’转换日期字符串格式

select current_date(); – date 2022-03-03 当前日期
select date_sub(current_date(),1); – date 2021-03-02 日期减去天数,几天前,返回的是日期
select date_add(current_date(),1); – date 2021-03-04 几天后,日期加天数,返回的是日期
select datediff(current_date(),to_date(‘2022-03-02 10:29:42’)); – int 两个日期差的天数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值