Hive时间日期使用

hive时间函数

返回类型函数名解释实例
bigintunix_timestamp()获取当前时间时间戳hive> select unix_timestamp() from dual;
1539691341
bigintunix_timestamp(string date)将格式为“yyyy-MM-dd HH:mm:ss”的字符串转成时间戳,如果格式不对返回null

hive> select unix_timestamp('2018-10-16 20:02:21') from dual;

1539691341

bigintunix_timestamp(string date,string pattern)将指定时间字符串格式转成时间戳,如果格式不对返回null

hive> select unix_timestamp('2018-10-16','yyyy-MM-dd') from dual;

1539619200

hive> select unix_timestamp('20181016','yyyyMMdd') from dual;

1539619200

stringfrom_unixtime(bigint,format)将时间戳转为format格式,可以为“yyyy-MM-dd HH:mm:ss”,"yyyy-MM-dd HH","yyyy-MM-dd","yyyyMMdd"等

hive> select from_unixtime(1539691341,'yyyy-MM-dd HH:mm:ss') from dual;

2018-10-16 20:02:21

hive> select from_unixtime(1539691341,'yyyy-MM-dd') from dual;

2018-10-16

stringto_date(string timestamp)返回时间字符串的日期部分

hive> select to_date('2018-10-16 20:02:21') from dual;

2018-10-16

intdatediff(string enddate,string startdate)计算两个日期相差的天数

hive> select datediff('2018-10-16','2018-06-08');

130

hive> select datediff('2018-10-16 11:22:00','2018-06-08 22:11:00');

130

hive> select datediff('20181016','20180608');

NULL

stringdate_add(string startdate,int days)开始时间加上days

hive> select date_add('2018-10-16',6);

2018-10-22

hive> select date_add('2018-10-16 12:11:22',6);

2018-10-22

hive> select date_add('20181016',6);

NULL

stringdate_sub(string startdate,int days)开始时间减去days

hive> select date_sub('2018-10-16',6);

2018-10-10

stringcurrent_date获取当前日期

hive> select current_date from dual;
2018-11-02

hive> select current_date() from dual;
2018-11-02

timestampcurrent_timestamp获取当前时间戳

hive> select current_timestamp() from dual;
2018-11-02 20:00:45.348

hive> select current_timestamp from dual;
2018-11-02 20:01:04.789

int

year(string date)返回日期中的年

hive> select year('2019-07-11');
2019

hive> select year('2019-07-11 10:00:00');

2019

intmonth(string date)返回日期中的月

hive> select month('2019-07-11');

7

hive> select month('2019-07-11 10:00:00');

7

intday(string date)返回日期中的天

hive> select day('2019-07-11');

11

hive> select day('2019-07-11 10:00:00');

11

inthour(string date)返回日期中的小时

hive> select hour('2019-07-11 10:00:00');

10

hive> select hour('2019-07-11');

NULL

intminute(string date)返回日期中的分钟

hive> select minute('2019-07-11 10:20:30');

20

hive> select minute('10:20:30');

20

intsecond(string date)返回日期中的秒

hive> select second('2019-07-11 10:20:30');

30

hive> select second('10:20:30');

30

intweekofyear(string date)返回日期当前周数

hive> select weekofyear('2019-07-11 10:20:30');

28

int..返回星期几(网上大神写的)

hive> select if(pmod(datediff(current_date, '1920-01-01') - 3, 7)='0',7,pmod(datediff(current_date, '1920-01-01') - 3, 7));

3

    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值