工作原因有时候需要使用到hive的日期函数,但是很多东西都记不住,每次网上去找又浪费时间,干脆自己整理一个。
-
获取日期
select current_date(); --返回2021-04-26
-
获取时间戳
# 单位:秒 返回bigint select unix_timestamp(); --返回1619401695
-
获取日期时间
select current_timestamp(); --返回2021-04-25 19:48:43.153
-
格式化时间戳
select from_unixtime(1619351633,'yyyy-MM-dd') --返回结果 2021-04-25 select from_unixtime(1590681600,'yyyyMMdd') --返回结果 20210425 select from_unixtime(1590729143,'yyyy-MM-dd HH:mm:ss') --返回结果 2021-04-25 19:53:53 select from_unixtime(1590729143) --返回结果 --2021-04-25 19:53:53 不加默认 yyyy-MM-dd HH:mm:ss 格式 select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'Y年') --返回结果 2021年 select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'yyyy-MM') --返回结果 2021-04 select from_unixtime(unix_timestamp('20210425','yyyyMMdd'),'yyyy-MM-dd') --返回结果 2021-04-25 select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'EEEE') --返回结果 Sunday select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'w') --返回第几周(18) select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'u') -- 返回本周的第几天(7) select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'D') -- 本年的第几天(115) select from_unixtime(unix_timestamp('2021-04-25','yyyy-MM-dd'),'d') -- 本月的第几天(25)
-
日期转时间戳
# 参数时可选的,当参数为空时,返回当前Unix是时间戳,精确到秒。可以指定一个具体的日期,转换为Unix时间戳格式 # unix_timestamp(string datetime,string format) # 日期时间 <