天天看点

hive 常用日期格式处理

固定日期转换成时间戳

* select unix_timestamp('2016-08-16','yyyy-MM-dd')                          --1471276800
* select unix_timestamp('20160816','yyyyMMdd')                              --1471276800
* select unix_timestamp('2016-08-16T10:02:41Z', "yyyy-MM-dd'T'HH:mm:ss'Z'") --1471312961
           

16/Mar/2017:12:25:01 +0800 转成正常格式(yyyy-MM-dd hh:mm:ss)

时间戳转换程固定日期

* select from_unixtime(1471276800,'yyyy-MM-dd') --2016-08-16
* select from_unixtime(1471276800,'yyyyMMdd') --20160816
* select from_unixtime(1471312961) -- 2016-08-16 10:02:41
* select from_unixtime( unix_timestamp('20160816','yyyyMMdd'),'yyyy-MM-dd') --2016-08-16
* select date_format('2016-08-16','yyyyMMdd') --20160816
           

返回日期时间字段中的日期部分

* select to_date('2016-08-16 10:03:01') --2016-08-16
取当前时间
* select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
* select from_unixtime(unix_timestamp(),'yyyy-MM-dd')
           

返回日期中的年

* select year('2016-08-16 10:03:01') --2016返回日期中的月
* select month('2016-08-16 10:03:01') --8返回日期中的日
* select day('2016-08-16 10:03:01') --16返回日期中的时
* select hour('2016-08-16 10:03:01') --10返回日期中的分
* select minute('2016-08-16 10:03:01') --3返回日期中的秒
* select second('2016-08-16 10:03:01') --1
           

返回日期在当前的周数

返回结束日期减去开始日期的天数

返回开始日期startdate增加days天后的日期

返回开始日期startdate减少days天后的日期

返回当天三种方式

* SELECT CURRENT_DATE;--2017-06-15
* SELECT CURRENT_TIMESTAMP;--返回时分秒--2017-06-15 19:54:44
* SELECT from_unixtime(unix_timestamp());--2017-06-15 19:55:04
           

返回当前时间戳

返回当月的第一天

返回当年的第一天

获取某一时间段的每天时间遍历

select regexp_replace(date_add(dt,row_number() over(order by dt) - 1),'-','') as dt
from (select explode(split(rpad('2019-07-20',(datediff('2019-08-05','2019-07-20') + 1 ) * 11 - 1,',2019-07-20'),',')) as dt
     ) a