HiveSql时间集合应用

// add_months 获取当前日期的后N个月的日期

select add_months('2021-08-09',3);    --返回   2021-11-09


// 时间戳进行格式化

select from_unixtime(1519818348,'yyyyMMdd');    --返回  通过时间戳对应的格式 20180228


// 获取当天的时间戳

select unix_timestamp() as lin;   --返回 默认时间戳

select from_unixtime(unix_timestamp(),'yyyyMMdd')as lin;  --返回默认时间戳的格式化时间


// 获取当前日期N天后的日期

select date_add('2018-02-28',3) as shijian;

select date_add('2018-02-28',-3) as shijian;

//  获取当前日期减少N天后的日期
select date_sub('2012-12-08',10) as shijian;


// -------------------------------------------------------------------------------------------------》
// 例子: 20180905 转成 2018-09-05      20170728102031   转成2017-07-28 10:20:31  的集合
 // unix_timestamp 是把指定的数据转换成时间戳,然后在由 from_unixtime转换成时间格式为’yyyy-mm-dd‘
select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd') as li;

//20170728102031    regexp_replace就是替换指定字符  转换成 2017-07-28 10:20:31

select concat(substr('20170728102031',1,4),'-',
    substr('20170728102031',5,2),'-',
    substr('20170728102031',7,2),' ',
    substr('20170728102031',9,2),':',
    substr('20170728102031',11,2),':',
    substr('20170728102031',13,2)
    ) as li;

//第二种做法
select from_unixtime(unix_timestamp('20170728102031','yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss') as li;
select unix_timestamp('20170728102031','yyyyMMddHHmmss') as li;

//--------------------------------------------------------------------------------------------------------------->

// 算出两个日期之间的差  datediff

select datediff('2012-12-08','2012-05-09') as lin;

select to_date('2011-12-08 10:03:01') as li;

//计算练习
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'),'2021-11-12 15:06:20')*24 as li;


//各种格式练习
select to_date(from_unixtime(unix_timestamp('2012-08-20','yyyyMMdd'),'yyyyMMdd')) as lin;


select from_unixtime(unix_timestamp(),'yyyy.MM.dd') as lin;

// eg:这里的日期要注意大小写要一样。
select from_unixtime(unix_timestamp('20210809','yyyyMMdd'),'yyyy.MM.dd')  as lin;



//日期函数  返回的是年月日
select year('2018-12-08 10:03:01');--2018 --年
select year('2018-12-08');--2018
select month('2018-12-08 10:03:01');--12 --月
select month('2018-12-08');--12
select day('2018-12-08 10:03:01');--8  --日
select day('2018-12-08');--8
select hour('2018-12-08 10:03:01');--10  --时
select minute('2018-12-08 10:03:01');--3   --分
select second('2018-12-08 10:03:01');--1   --秒
select weekofyear('2018-12-08 10:03:01')--49   日期转换为周

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值