1. 获取当前时间
hive
select from_unixtime(unix_timestamp());
select from_unixtime(unix_timestamp('2021-12-07 13:01:03'),'yyyy-MM-dd HH:mm:ss');
select current_date;
select unix_timestamp();
select now();或者select current_timestamp
select current_date;
select current_date - interval '1' day;
2.字符串日期转时间戳
select unix_timestamp('2021-01-08 10:36:15','yyyy-MM-dd HH:mm:ss')
select to_unixtime(cast('2021-01-08 13:53:36' as timestamp))
3.时间戳转字符串日期
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
select format_datetime(from_unixtime(1610085216),'yyyy-MM-dd HH:mm:ss')
4. 字符串日期格式转换
select from_unixtime(unix_timestamp('2021-01-08 10:36:15','yyyy-MM-dd HH:mm:ss'),'yyyy-MM-dd')
select to_date('2021-01-08 10:36:15')
select to_date(from_unixtime(unix_timestamp('20210110','yyyyMMdd')));
select date(cast('2021-01-08 10:36:15' as timestamp))
select format_datetime(from_unixtime(to_unixtime(cast('2021-01-08 13:53:36' as timestamp))),'yyyy-MM-dd')
select format_datetime(date_parse('20210110','%Y%m%d'),'yyyy-MM-dd')
5. date类型转字符类型串日期
--presto
SELECT format_datetime(cast('2021-06-19' as date),'yyyy-MM-dd') ->输出varchar类型日期:2021-06-19
6. 日期加减
select date_add('2021-01-08',2);
select date_add(current_timestamp,2);
select date_add('day',-2,cast('2020-01-10' as date));
select date_add('day',-2,current_date);
select format_datetime(date_add('day',-1,cast('2021-06-19' as date)),'yyyy-MM-dd')
7. 日期截断
dd当天, MM所在月第一天,yyyy所在年第一天
SELECT trunc(CURRENT_DATE,'MM')
day当天, month所在月第一天,year所在年第一天
SELECT date_trunc('month',CURRENT_DATE)