hive常用时间日期函数

  1. 获取当前时间
第一种获取方式
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss');
可以通过修改时间格式('yyyy-MM-dd HH:mm:ss'),来调整输出结果
比如:调成yyyy-MM-dd则只显示年月日,不显示时间。
注:hive中yyyyMMdd其中一定要对应好字母的大小写关系,否则会报错。
 ----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   2020-08-05 17:08:41
-----------------------------------------------------------
第二种获取方式
select current_timestamp;
这种方式较为简单,可以通过其他函数获取其中年月日等,在下面有具体函数介绍。
----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   2020-08-05 17:15:26.998
-----------------------------------------------------------
  1. 获取时间戳
select unix_timestamp();
----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   1596619480
-----------------------------------------------------------
  1. 获取当前日期
SELECT CURRENT_DATE; 
----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   2020-08-05
-----------------------------------------------------------
同select from_unixtime(unix_timestamp(),'yyyy-MM-dd');
  1. 获取年、月、日、小时、分钟、秒、当年第几周
    这里获取年、月、日、小时、分钟、秒、当年第几周比较简单,只要记住函数名和格式就够了
select year('2020-08-05 17:08:41');
也可以select year(current_timestamp);获取的是当前时间的年份
此时current_timestamp为2020-08-05 17:08:41.517
----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   2020
-----------------------------------------------------------
接下来直接演示其余的函数,不再做说明。
select 
	month(current_timestamp)      as month,
	day(current_timestamp)        as day,
	hour(current_timestamp)       as hour,
	minute(current_timestamp)     as minute,
    second(current_timestamp)     as second,
    weekofyear(current_timestamp) as weekofyear;
------------------------------------------------------------------
 输出结果:| month | day | hour | minute | second | weekofyear
 -----------------------------------------------------------------
           |   8  |  5  |  17  |   8     |   41   |    32        
------------------------------------------------------------------

5.日期时间转日期函数

select to_date('2020-08-05 17:08:41.517');
----------------------------------------------------------
 输出结果:			_c0
 ----------------------------------------------------------
                   2020-08-05
-----------------------------------------------------------

6.时间戳转换函数

select from_unixtime(unix_timestamp(),'yyyy-MM-dd');
-----------------------------------------------------------
 输出结果:			_c0
-----------------------------------------------------------
                   2020-08-05
-----------------------------------------------------------

7.日期差函数
返回结果是第一个日期减去第二个日期获得的天数

select datediff('2020-08-05','2020-01-01');
-----------------------------------------------------------
 输出结果:			_c0
-----------------------------------------------------------
                   217
------------------------------------------------------------

8.日期加减函数

select 
	date_add('2020-08-05',3) as add,
    date_sub('2020-08-05',3) as sub;
-----------------------------------------------------------
 输出结果:	|     add      |       sub     
-----------------------------------------------------------
            | 2020-08-08   |  2020-08-02   
------------------------------------------------------------

9.非日期格式转时间

FROM_UNIXTIME(UNIX_TIMESTAMP(APPEAL_END_DATE,'yyyyMMdd'))

猜你喜欢

转载自blog.csdn.net/weixin_42856363/article/details/107820226