Hive
UTC转GMT+0800(东八区)
select from_utc_timestamp(cast(regexp_replace(regexp_replace('2019-07-12T09:01:59.056Z','T',' '),'Z','') as timestamp),"GMT+0800")
【重点在于】:
from_utc_timestamp(‘1970-01-01 08:00:00’,‘PST’) 函数可以将UTC时区的时间转换为其他时区,但是其第一个参数格式需要是"yyyy-MM-dd HH:mm:ss.SSS"中间不可有其他符号
获取昨天
-- 方式一:一天是86400秒,当前时间减一天就是昨天
from_unixtime(unix_timestamp()-86400,'yyyyMMdd')
from_unixtime(unix_timestamp()-86400,'yyyy-MM-dd')
-- 方式二
regexp_replace(date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1),'-','')
...根据自己想要的格式可以自行更换
日期格式化
from_unixtime(unix_timestamp('20190909','yyyyMMdd'),'yyyy-MM-dd')
返回当前时间的月末日期
select last_day('2017-01-16 09:55:54');
PostgreSQL
时区转换
SELECT '2019-04-05 16:13:05.921+00' AT TIME ZONE 'Asia/shanghai'
日期格式转换时
--format_datetime(date_parse('20190909','%Y%m%d'),'yyyy-MM-dd')
select '20221205'::date -- yyyy-MM-dd 2022-12-05 可以直接强转,得到“-”日期,结果为date类型
select to_char('20221205'::date,'yyyy/mm/dd') --2022/12/05 通过to_char(日期/时间戳 , 格式) 进行日期格式转换
获取昨天
format_datetime(now()- interval '1' day,'yyyyMMdd')
获取周几
select extract(dow from '20221205'::date) -- dow:一周里面的第几天 from 日期/时间
获取两个日期之间的日期列表
-- 获取两个时间段之间的日期列表
select to_char(generate_series('2022-02-01'::date,'2022-02-25','1 day'),'yyyy-mm-dd');
select to_char(generate_series('2022-02-01'::date,current_date,'1 day'),'yyyy-mm-dd');
- 结果如图:
Presto
取周(周一为一周的开始与Hive weekofyear()一致)
week(date_parse('20190805','%Y%m%d'))
日期格式化
date_format(date_parse('20190805','%Y%m%d'),'%Y-%m-%d')
昨天
format_datetime(now()- interval '1' day,'yyyyMMdd')
时间戳转换格式
format_datetime(from_unixtime(1554480785921*0.001),'yyyyMMdd')``