Hive之时间函数
1、unix_timestamp()
返回当前时区的unix时间戳
返回类型:bigint
hive> select unix_timestamp() from hive_sum limit 1;
1465875016
2、from_unixtime(bigint unixtime[,string format])
时间戳转日期函数
返回类型:string
hive> select from_unixtime(unix_timestamp(),’yyyyMMdd’) from hive_sum limit 1;
20160614
3、unix_timestamp(string date)
返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
hive> select unix_timestamp(‘2016-06-01’) from hive_sum limit 1;
NULL
hive> select unix_timestamp(‘2016-06-01 00:00:00’) from hive_sum limit 1;
1464710400
4、unix_timestamp(string date,string pattern)
返回指定日期格式的时间戳
返回类型:bigint
hive> select unix_timestamp(‘2016-06-01’,’yyyyMMdd’) from hive_sum limit 1;
1449331200
5、to_date(string date)
返回时间字段中的日期部分
返回类型:string
hive> select to_date(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016-06-01
6、year(string date)
返回时间字段中的年
返回类型:int
hive> select year(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016
7、month(string date)
返回时间字段中的月
返回类型:int
hive> select month(‘2016-06-01’) from hive_sum limit 1;
6
8、day(string date)
返回时间字段中的天
返回类型:int
hive> select day(‘2016-06-01’) from hive_sum limit 1;
1
9、weekofyear(string date)
返回时间字段是本年的第多少周
返回类型:int
hive> select weekofyear(‘2016-06-01’) from hive_sum limit 1;
22
10、datediff(string enddate,string begindate)
返回enddate与begindate之间的时间差的天数
返回类型:int
hive> select datediff(‘2016-06-01’,’2016-05-01’) from hive_sum limit 1;
31
11、date_add(string date,int days)
返回date增加days天后的日期
返回类型:string
hive> select date_add(‘2016-06-01’,15) from hive_sum limit 1;
2016-06-16
12、date_sub(string date,int days)
返回date减少days天后的日期
返回类型:string
hive> select date_sub(‘2016-06-01’,15) from hive_sum limit 1;
2016-05-17
13、add_months(string date,int months)
返回date增加months月后的日期
返回类型:string
hive> select add_months(‘2016-06-01’,1) from hive_sum limit 1;
2016-07-01
-``
1-4、current_date
返回当前日期
返回类型:string
```sql
select
cust_id
,sum(case when update_time between add_months(current_date,-3) and current_date then 1 else 0 end) as num_cnt
from src group by cust_id;
15.date_format(date,格式)
根据格式整理日期
返回类型:string
select date_format('2019-02-10','yyyy-mm') ;
2019-02
- next_day()
- 取当前天的下一个周一
select next_day('2019-02-12', 'MO') ;
- 取当前天的周一
select date_add(next_day('2019-02-12', 'MO'), -7) ;
18 . last_day()
返回当月的最后一天
select last_day('2019-02-10') ;
19 . months_between(date1, date2)
返回两个日期间的月数(
如果date1在日历中比date2晚,那么MONTHS_BETWEEN()就返回一个正数。
如果date1在日历中比date2早,那么MONTHS_BETWEEN()就返回一个负数。
如果date1和date2日期一样,那么MONTHS_BETWEEN()就返回一个0。
)
返回类型: int
SQL> select months_between(to_date('2014-3-21','yyyy-mm-dd'), to_date('2014-1-10','yyyy-mm-dd')) months
2 from dual;
MONTHS
----------
2.35483871
SQL> select months_between(to_date('2014-1-10','yyyy-mm-dd'), to_date('2014-3-21','yyyy-mm-dd')) months
2 from dual;
MONTHS
----------
-2.3548387
SQL> select months_between(to_date('2014-1-10','yyyy-mm-dd'), to_date('2014-1-10','yyyy-mm-dd')) months
2 from dual;
MONTHS
----------
0
--2014.3.21和2014.1.10之间,相差2个月加11天,11天按月换算成小数(在oracle里面,以31天为基数):
SQL> select 11/31 from dual;
11/31
----------
.35483871