Hive日期函数

字符转为时间,转换为日期的时间部分
select to_date('2019-11-20');

查看当前时间的unix时间戳
select unix_timestamp(); 1574260385 unix时间转换为当前时区的时间,格式"yyyy-MM-dd HH:mm:ss",注意月份MM一定要大写,小写会出错!!!!
select from_unixtime(1574260385,'yyyy-MM-dd HH:mm:ss'); 2019-11-20 06:33:05 将指定日期转换为unix时间戳,unix_timestamp(string date, string pattern),注意前后格式一定要匹配
select unix_timestamp('2019-11-20 06:33:05', 'yyyy-MM-dd HH:mm:ss'); 或者 select unix_timestamp('2019/11/20 06:33:05', 'yyyy/MM/dd HH:mm:ss'); 1574260385 获取当前时间 select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss'); 2019-11-20 06:47:50 获取年份 select year(string date); select year(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss')); 获取月份 select month(string date); select month(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss')); 获取日期中的日 select day(string date); select day(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss')); 获取日期的小时 select hour(string date); select hour(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss'));
获取当前日期是一年的第几周
select weekofyear(string date); select weekofyear(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss')); 获取两个日期的日期差datediff(string enddate, string startdate) select datediff('2019-11-20','2019-11-01'); 19 日期增加函数date_add,往未来增加几天 select date_add('2019-11-20',2); 2019-11-22 日期减少函数date_sub,向历史倒推几天 select date_sub('2019-11-20',2); 或者 select date_add('2019-11-20',-2);
原文地址:https://www.cnblogs.com/lucas-zhao/p/11901801.html