hive日期时间函数

to_date():日期时间转日期函数:

返回值: string

说明: 返回日期时间字段中的日期部分。

select to_date(’2021-02-08 10:03:01′) from dual;

2021-02-08

current_date():当前日期

select current_date() --2021-02-09

current_timestamp():当前日期时间

select current_timestamp() ; --2021-02-09 11:43:00

from_unixtime():转化unix时间戳到当前时区的时间格式(格式中的M必须大写)

返回值: string

说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

select from_unixtime(1565776746,'yyyyMMdd'); -'20190814'

select from_unixtime(1565776746,'yyyy-MM-dd'); -'2019-08-14'

select from_unixtime(1612838819),from_unixtime(unix_timestamp()); --2021-02-09 10:46:44     2021-02-09 10:46:59

select from_unixtime(1565776746,'yyyy-MM-dd HH:mm:ss'); -'2019-08-14 17:59:06'

unix_timestamp():获取当前UNIX时间戳函数

unix_timestamp(string date):日期转UNIX时间戳函数

unix_timestamp(string date, string pattern):指定格式日期转UNIX时间戳函数

返回值: bigint

select unix_timestamp(),unix_timestamp('2019-04-30 13:51:20'); --1430816254  1556603480

应用:yyyymmdd与yyyy-mm-dd格式转换

select unix_timestamp('20171205','yyyymmdd'); --1483546320

select from_unixtime(unix_timestamp('20171205','yyyyMMdd'),'yyyy-MM-dd') --20171205转成2017-12-05

select from_unixtime(unix_timestamp('2017-12-05','yyyy-MM-dd'),'yyyyMMdd') from dual; --2017-12-05转成20171205

 

year():返回日期/时间中的年

返回值: int

select year('2021-02-08 10:03:01') --2021

quarter():返回日期/时间的季度

select quarter('2021-02-09'), quarter('2021-04-09 12:12:01') --1  2

month():返回日期/时间中的月

返回值: int

select month('2021-02-08 10:03:01') --2

day():返回日期/时间中的天

返回值: int

select day('2021-02-14 13:14:12'); --14

hour():返回日期/时间中的小时

返回值: int

select hour('2021-01-13 11:32:12'); --11

minute():返回日期/时间中的分钟

返回值: int

select minute('2021-01-14 13:14:12'); --14

second():返回日期/时间中的秒

返回值: int

select minute('2021-01-14 13:14:12'); --12

last_day(): 返回日期/时间月末日期

select last_day('2021-02-02 12:34:34'); --2021-02-28

next_day():返回当前时间的下一个星期X所对应的日期

select next_day('2020-11-05','Sunday') --返回下一个周日

select next_day('2021-02-09', 'TU') --返回下一个周二 2021-02-16

datediff():返回开始日期/时间减去结束日期/时间的天数

select datediff('2019-04-09','2019-04-01'); --8

date_sub():返回日期/时间前n天的日期

select date_sub('2019-04-09',4),date_sub('2019-04-09 13:51:20',4); --2019-04-05      2019-04-05

date_add():返回日期/时间后n天的日期

select date_add('2019-04-09',4); --'2019-04-13'

add_months(): 返回日期/时间后n月的日期

select add_months('2019-04-09 12:34:33',4); --'2019-08-09'

weekofyear():返回日期/时间在当前年第几周

select weekofyear('2019-01-09 12:11:01'); --当年中的第2周

dayofmonth(): 返回日期/时间在当前月第几天

select dayofmonth('2019-01-09 12:11:01'); --当年中的第9天

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值