Hive 日期函数

对于日期时间的操作,记录一下:

1、 unix时间戳 ⇋ 日期(string)

unix时间戳 —->日期
from_unixtime(unixtime, format)
默认格式是:yyyy-MM-dd HH:mm:ss 1970-01-01 00:00:00

hive> select from_unixtime(1323308943,’yyyyMMdd’) from dual;
20111208

日期 —->unix时间戳
unix_timestamp(string date, string pattern) 转换pattern格式的日期到时间戳
默认格式是:yyyy-MM-dd HH:mm:ss 1970-01-01 00:00:00

hive> select unix_timestamp(‘20111207 13:01:03’,’yyyyMMdd HH:mm:ss’)
from dual; 1323234063

2、日期转年,月,日等

•日期时间转日期函数: to_date(string timestamp),返回值: string

hive> select to_date(‘2011-12-08 10:03:01’) from dual;
2011-12-08

•日期转年函数: year(string date),返回值: int

hive> select year(‘2011-12-08 10:03:01’) from dual;
2011
hive> select year(‘2012-12-08’) from dual;
2012

• 日期转月函数: month(string date),返回值: int
• 日期转天函数: day(string date),返回值: int
• 日期转小时函数: hour(string date),返回值: int
• 日期转分钟函数: minute(string date),返回值: int
• 日期转秒函数: second(string date),返回值: int
• 日期转周函数: weekofyear(string date),返回值: int

3、日期比较,加,减

• 日期比较函数: datediff(string enddate, string startdate), 返回值: int

hive> select datediff(‘2012-12-08’,’2012-05-09’) from dual;
213

• 日期增加函数: date_add(string startdate, int days),返回值: string

hive> select date_add(‘2012-12-08’,10) from dual;
2012-12-18

• 日期减少函数: date_sub(string startdate, int days),返回值: string

参考链接:http://sishuok.com/forum/blogPost/list/6222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值