to_date:日期时间转日期函数
select to_date('2015-04-02 13:34:12');
输出:2015-04-02
## 当前日期和时间 SELECT current_timestamp(); -- 2018-04-28 11:46:03.136 ## 获取当前日期,当前是 2018-04-28 SELECT current_date; OR SELECT current_date(); -- 2018-04-28 ## 获取unix系统下的时间戳 SELECT UNIX_TIMESTAMP(); -- 1524884881 ## 当前是 2018-04-28 select substr(current_timestamp, 0, 10); -- 2018-04-28 ## 当前是 2018-04-28 select date_sub(current_date, 1); -- 2018-04-27 ## yyyy-MM-dd HH:MM:ss 截取日期 select to_date("2017-10-22 10:10:10"); -- 2017-10-22 ## 两个日期之间的天数差 select datediff("2017-10-22", "2017-10-12"); -- 10 select datediff("2017-10-22 10:10:10", "2017-10-12 23:10:10"); -- 10 select datediff("2017-10-22 01:10:10", "2017-10-12 23:10:10"); -- 10 ## 时间截取 select from_unixtime(cast(substr("1504684212155", 0,10) as int)) dt; -- 2017-09-06 15:50:12 ## 时间戳转日期 ## 语法: to_date(string timestamp) select to_date(from_unixtime(UNIX_TIMESTAMP())); -- 2018-04-28 select FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd 10:30:00'); -- 2018-04-28 10:30:00 select concat(date_sub(current_date,1),' 20:30:00'); -- 2018-04-27 20:30:00 -- hive version 1.2.0 select date_format(date_sub(current_date,1),'yyyy-MM-dd 20:30:00');
from_unixtime:转化unix时间戳到当前时区的时间格式
select from_unixtime(1323308943,’yyyyMMdd’);
输出:20111208
unix_timestamp:获取当前unix时间戳
select unix_timestamp();
输出:1430816254
select unix_timestamp('2015-04-30 13:51:20');
输出:1430373080
year:返回日期中的年
select year('2015-04-02 11:32:12');
输出:2015
month:返回日期中的月份
select month('2015-12-02 11:32:12');
输出:12
day:返回日期中的天
select day('2015-04-13 11:32:12');
输出:13
hour:返回日期中的小时
select hour('2015-04-13 11:32:12');
输出:11
minute:返回日期中的分钟
select minute('2015-04-13 11:32:12');
输出:32
second:返回日期中的秒
select second('2015-04-13 11:32:56');
输出:56
weekofyear:返回日期在当前周数
select weekofyear('2015-05-05 12:11:1');
输出:19
datediff:返回开始日期减去结束日期的天数
select datediff('2015-04-09','2015-04-01');
输出:8
date_sub:返回日期前n天的日期
select date_sub('2015-04-09',4);
输出:2015-04-05
date_add:返回日期后n天的日期
select date_add('2015-04-09',4);
输出:2015-04-13