hive时间函数
| 返回类型 | 函数名 | 解释 | 实例 |
|---|---|---|---|
| bigint | unix_timestamp() | 获取当前时间时间戳 | hive> select unix_timestamp() from dual; 1539691341 |
| bigint | unix_timestamp(string date) | 将格式为“yyyy-MM-dd HH:mm:ss”的字符串转成时间戳,如果格式不对返回null |
hive> select unix_timestamp('2018-10-16 20:02:21') from dual; 1539691341 |
| bigint | unix_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 |
| string | from_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 |
| string | to_date(string timestamp) | 返回时间字符串的日期部分 |
hive> select to_date('2018-10-16 20:02:21') from dual; 2018-10-16 |
| int | datediff(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 |
| string | date_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 |
| string | date_sub(string startdate,int days) | 开始时间减去days |
hive> select date_sub('2018-10-16',6); 2018-10-10 |
| string | current_date | 获取当前日期 |
hive> select current_date from dual; hive> select current_date() from dual; |
| timestamp | current_timestamp | 获取当前时间戳 |
hive> select current_timestamp() from dual; hive> select current_timestamp from dual; |
|
int | year(string date) | 返回日期中的年 |
hive> select year('2019-07-11'); hive> select year('2019-07-11 10:00:00'); 2019 |
| int | month(string date) | 返回日期中的月 |
hive> select month('2019-07-11'); 7 hive> select month('2019-07-11 10:00:00'); 7 |
| int | day(string date) | 返回日期中的天 |
hive> select day('2019-07-11'); 11 hive> select day('2019-07-11 10:00:00'); 11 |
| int | hour(string date) | 返回日期中的小时 |
hive> select hour('2019-07-11 10:00:00'); 10 hive> select hour('2019-07-11'); NULL |
| int | minute(string date) | 返回日期中的分钟 |
hive> select minute('2019-07-11 10:20:30'); 20 hive> select minute('10:20:30'); 20 |
| int | second(string date) | 返回日期中的秒 |
hive> select second('2019-07-11 10:20:30'); 30 hive> select second('10:20:30'); 30 |
| int | weekofyear(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 |

600

被折叠的 条评论
为什么被折叠?



