hive 常见时间日期函数的使用

2023-06-15,,

1.时间戳函数
日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数
获得当前时区的UNIX时间戳: select unix_timestamp(); 1533716607

将指定的时间转为UNIX时间戳 :select unix_timestamp('2018-08-08 16:22:01'); 1533716521

将指定的时间转为UNIX时间戳: select unix_timestamp('2018-08-08 16:22:01','yyyy-MM-dd HH:mm:ss'); 1533716521

select unix_timestamp('2018-08-08 16:22:01','yyyyMMdd HH:mm:ss'); 1533716521 --将指定的时间转为UNIX时间戳

2.时间戳转日期
select from_unixtime(1533716521); 2018-08-08 16:22:01
select from_unixtime(1533716521,'yyyyMMdd'); 20180808
select from_unixtime(1533716521,'yyyy-MM-dd HH:mm:ss'); 2018-08-08 16:22:01
获取系统当前时间 :
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'); 2018-08-08 16:28:21

日期转换为其他格式的日期:
select from_unixtime(unix_timestamp('2018-08-08 16:28:21',yyyy-MM-dd HH:mm:ss'),'yyyyMMdd'); 20180808

3.获取当前日期: current_date
select current_date ;2018-08-08

4.日期时间转日期:to_date(string timestamp)
select to_date('2018-08-08 17:12:00') ;2018-08-08

分区参数转日期:

to_date(from_unixtime(unix_timestamp('${dt}','yyyyMMdd')))

分区参数当月第一天日期:
to_date(from_unixtime(unix_timestamp(concat(substr('${dt}',1,6),'01'),'yyyyMMdd')))

5.计算两个日期之间的天数: datediff
select datediff('2018-08-08','2018-08-01') ; 7

6.日期增加和减少: date_add/date_sub(string startdate,int days)
select date_add('2018-08-04',1) , date_sub('2018-08-04',1) ; 2018-08-05 2018-08-03

7.其他日期函数
查询当前系统时间(包括毫秒数):` current_timestamp;

查询当月第几天: dayofmonth(current_date);

月末: last_day(current_date)

当月第1天: date_sub(current_date,dayofmonth(current_date)-1)

年:year(date)

月:month(date)

日:day(date)

小时:hour(date)

分:minute(date)

秒:second(date)

第几周:weekofyear(date)

下个月第1天: `add_months(date_sub(current_date,dayofmonth(current_date)-1),1)

hive 常见时间日期函数的使用的相关教程结束。

《hive 常见时间日期函数的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。