在上篇文章【hive_窗口函数】中学习了hive常见的窗口函数的使用场景及怎么使用,在工作中时间格式的数据也经常会遇到,今天就学习一下,常见的hive时间处理。
1、返回当周的周一
--法1:
select current_date,date_sub(next_day(current_date,'MO'),7);
--法2:
select current_date,date_sub(current_date,pmod(datediff(current_date,'1900-01-08'),7));
2、返回当周周五
select current_date,date_sub(current_date,pmod(datediff(current_date,'1900-01-05'),7))
3、日期加减(日、小时、分钟、秒)
select current_date,date_add(substr(current_date,1,10),6); --当前日期加6天
select current_date,from_unixtime(unix_timestamp(current_date)+43200,'yyyy-MM-dd HH:mm:ss') --12h=43200s:12*60*60,5m=300s:5*60
--mysql:
select date_add(substr(current_date,1,10),interval 6 day);
4、时间差(日)
select datediff('2020-07-24 11:42:58','2020-07-23 15:01:13')
5、时间戳转化成时间
--10位时间戳
select from_unixtime(1595487673,'yyyy-MM-dd HH:mm:ss')
--13位时间戳
select from_unixtime(cast(1595487673343/1000 as int),'yyyy-MM-dd HH:mm:ss')
6、日期转化成时间戳
--10位时间戳
select unix_timestamp(cast('2020-07-23 15:01:13' as timestamp))
--13位时间戳
select unix_timestamp(cast(substr('2020-07-23 15:01:13.343', 1, 19) as timestamp)) * 1000 + cast(substr('2020-07-23 15:01:13.343', 21) as bigint)
详细内容请看我公众号~