使用hive进行时间处理的过程中,经常会用到一些时间函数,现整理一下hive中常用日期函数。
1、取得当前日期时间:
--取得当前日期:
select current_date();
输出:2021-08-14
--取得当前日期时间:
select current_timestamp();
输出:2021-08-14 13:14:57
--hive取得当前时间戳:
select unix_timestamp();
输出:1628911641
2、to_date:日期时间转日期函数,返回日期时间字段中的日期部分,
说明:字符串必须为:yyyy-MM-dd格式。
select to_date('2021-08-14 13:34:12');
输出:2021-08-14
3、from_unixtime:unix时间戳到转时间格式
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
select from_unixtime(1323308945,’yyyy-MM-dd’);
输出:2011-12-08
select from_unixtime(1323308945,’yyyyMMdd’);
输出:20111208
--取得当前时间,相当于select current_timestamp();
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
输出:2021-08-14 03:14:57
4、date_format:日期、时间戳、字符串类型格式化输出标准时间格式
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
输出:2021-08-14 11:14:46
select date_format(current_date(),