Hive时间整理

 from_unixtime & unix_timestam

from_unixtime:时间戳转日期函数 返回值: string
unix_timestamp:日期转时间戳函数 返回值:   bigint

时间戳一般是10位数的,13位数的为毫秒级的,毫秒级时间戳直接使用from_unixtime转换需要/1000 

 

时间格式转换:

select to_date(from_unixtime(unix_timestamp('20200527','yyyyMMdd'), 'yyyy-MM-dd'));

select from_unixtime(unix_timestamp('20200527','yyyymmdd'),'yyyy-mm-dd');

select from_unixtime(unix_timestamp('2020-05-27','yyyy-mm-dd'),'yyyymmdd');

可使用最原始的字符串拼接方式:

select concat(substr('20200527',1,4),'-',substr('20200527',5,2),'-',substr('20200527',7,2));

正则替换方式:

select regexp_replace(regexp_extract('2020-05-27','(\\d{4}-\\d{2}-\\d{2})',1), '-', '');

其他一些时间相关

获取当前日期
select current_date;

获取当前时间
select current_timestamp;
select from_unixtime(unix_timestamp());

获取当前时间戳(10位)
select unix_timestamp();

 求两个日期时间差

datediff(date1, date2) - Returns the number of days between date1 and date2
date1为结束日期,date2为开始日期,返回结束日期减去开始日期的天数
select datediff('2020-05-27','2020-03-31') as datediff; 

日期加减:

date_add(start_date, num_days) - Returns the date that is num_days after start_date.
date_sub(start_date, num_days) - Returns the date that is num_days before start_date.
返回日期相加减后的日期
select date_add('2020-05-27',3) as dateadd;
select date_sub('2020-05-27',7) as datesub;

next_day:返回指定日期的下一个周几

next_day(start_date, day_of_week) - Returns the first date which is later than start_date and named as indicated.
返回指定日期的下一个周几
SELECT next_day('2020-05-27', 'TU');
2020-06-02

时间简写汇总

Monday Mon MO 周一
Tuesday Tue TU 周二
Wednesday Wed WE 周三
Thursday Thu TH 周四
Friday Fri FR 周五
Saturday Sat SA 周六
Sunday Sun SU 周日

一 月 

January 

Jan

二 月 

February 

Feb

三 月 

March 

Mar

四 月 

April 

Apr

五 月 

May 

May

六 月 

June 

Jun

七 月 

July 

Jul

八 月 

August 

Aug

九 月 

September 

Sep

十 月 

October 

Oct

十一月

November 

Nov

十二月

December 

Dec

猜你喜欢

转载自blog.csdn.net/qq_24256877/article/details/106384772
今日推荐