对于日期时间的操作,记录一下:
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