Impala时间转换to_date、to_timestamp

在时间的运算上,也常常使用到日期格式的转换,如日期字符串转为日期型,日期转为格式化字符串,是两种常见的需求;另外也有需要将时间转为时间戳的场景等等;

时间数据的转换上,主要用to_date、to_timestamp,即可满足日期字符串转为日期型的需求;

from_timestamp、from_unixtime,即可满足日期转为格式化字符串的需求;

unix_timestamp,可以满足具有时间戳要求的场景;

再有其他场景大家可以根据需要,看看下面的方法有没有你所需要的哈

方法说明:

序号

语法类型/方法名称

输出类型

使用说明

1

to_date(timestamp date)

string

返回时间戳对应的date

2

to_timestamp(bigint unixtime)

timestamp

返回整数对应的timestamp值

3

to_timestamp(string date,string pattern)

timestamp

返回字符串对应的timestamp值

4

to_utc_timestamp(timestamp t,string timezone)

timestamp

指定时区的时间戳转化为UTC时区的时间戳

5

from_timestamp(timestamp t,string pattern)

string

把timestamp按照pattern进行格式化

6

from_timestamp(string date,string pattern)

string

把date按照pattern进行格式化

7

from_unixtime(bigint unixtime)

string

把时间戳秒数转化为本地地区中的字符串

8

from_unixtime(bigint unixtime,string pattern)

string

时间戳转化为本地时区字符串,pattern格式

9

from_utc_timestamp(timestamp t,string timezone)

timestamp

UTC时区指定时间戳转化为指定时区时间戳

10

unix_timestamp(string datetime)

bigint

把string类型的date或日期转化成时间戳Unix

11

unix_timestamp(timestamp datetime)

bigint

把string类型的timestamp转化成时间戳Unix

12

unix_timestamp(string datetime,string pattern)

bigint

日期按pattern转化成时间戳Unix

方法示例:

序号

语法类型/方法名称

输出结果

1

select now()2022-11-10 14:22:36.927

2

select to_date(now())2022-11-10

3

select to_timestamp(1668089883)2022-11-10 14:18:03.000

4

select to_timestamp('2022/11/10','yyyy/MM/dd') 2022-11-10 00:00:00.000

5

select to_utc_timestamp(now(),'Asia/Shanghai')2022-11-10 06:23:36.036

6

select from_timestamp(now(),'yyyy/MM')2022/11

7

select from_timestamp('2022-11-10','yyyy/MM') 2022/11

8

select from_unixtime(1668089883)            2022-11-10 14:18:03

9

select from_unixtime(1668089883,'yyyy/MM')      2022/11

10

select from_utc_timestamp(now(),'Asia/Shanghai')     2022-11-10 22:24:25.055

11

select unix_timestamp('2022-11-01')                   1667260800

12

select unix_timestamp(now())                         1668090279
13select unix_timestamp('2022-11-01 14:26:27','yyyy-MM-dd HH:mm:ss') 1667312787
  • 2
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值