Hive Sql常用的时间处理方法

hive 常用日期格式转换

把固定日期转换成时间戳
select unix_timestamp('2020-05-29','yyyy-MM-dd')  --返回结果 1590681600

select unix_timestamp('20200529','yyyyMMdd') --返回结果 1590681600

select unix_timestamp('2020-05-29T13:12:23Z', "yyyy-MM-dd'T'HH:mm:ss'Z'") --返回结果 1590729143

select unix_timestamp('2020-05-29 13:12:23', 'yyyy-MM-dd HH:mm:ss') --返回结果 1590729143
把 29/May/2020:11:30:03 +0800 转成正常格式(yyyy-MM-dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('29/May/2020:11:30:03 +0800', 'dd/MMM/yyy:HH:mm:ss Z'))
--返回结果  2020-05-29 11:30:03
时间戳转换成固定日期
select from_unixtime(1590681600,'yyyy-MM-dd') --返回结果 2020-05-29

select from_unixtime(1590681600,'yyyyMMdd') --返回结果 20200529

select from_unixtime(1590729143,'yyyy-MM-dd HH:mm:ss') --返回结果 2020-05-29 13:12:23

select from_unixtime(1590729143) --返回结果 --2020-05-29 13:12:23 不加默认 yyyy-MM-dd HH:mm:ss 格式

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'Y年') --返回结果 2020年

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'yyyy-MM') --返回结果 2020-05

select from_unixtime(unix_timestamp('20200529','yyyyMMdd'),'yyyy-MM-dd')  --返回结果 2020-05-29

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'EEEE') --返回结果 Friday

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'w') --返回第几周(22)

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'u') -- 返回本周的第几天

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'D') -- 本年的第几天(150)

select from_unixtime(unix_timestamp('2020-05-29','yyyy-MM-dd'),'d') -- 本月的第几天(29)
对日期进行格式化转换
select date_format('2020-05-29','yyyyMMdd') --返回结果  20200529
计算得出星期X,date_code是日期参数,如2020-06-01
select pmod(datediff(date_code, '2012-01-01'), 7) = 0 --当等于0时,这天是'星期日'

select pmod(datediff(date_code, '2012-01-01'), 7) = 1 --当等于1时,这天是'星期一'

select pmod(datediff(date_code, '2012-01-01'), 7) = 2 --当等于2时,这天是'星期二'

select pmod(datediff(date_code, '2012-01-01'), 7) = 3 --当等于3时,这天是'星期三'

select pmod(datediff(date_code, '2012-01-01'), 7) = 4 --当等于4时,这天是'星期四' 

select pmod(datediff(date_code, '2012-01-01'), 7) = 5 --当等于5时,这天是'星期五'

select pmod(datediff(date_code, '2012-01-01'), 7) = 6 --当等于6时,这天是'星期六'
返回日期时间字段中的日期部分
select to_date('2020-05-29 13:12:23) -- 返回结果 2020-05-29
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Barry Lzq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值