hive里经常需要将字符串转化为date或timestamp 或者转化为日期格式的string
先说一个简单的 cast(xx as date/string/timestamp) 这个大多情况都可以用
1.to_date
to_date只保留年月日,参数必须是string类型的yyyy-MM-dd HH:mm:ss或者date或timestamp类型,返回值是date类型',
select "to_date('2009-07-30 04:17:52')",to_date('2009-07-30 04:17:52') union all
select "to_date('2009-07-30 04')",to_date('2009-07-30 04') union all
select "to_date(current_date)",to_date(current_date) union all
select "to_date(current_timestamp)",to_date(current_timestamp)
2.date_format
date_format(date/timestamp/string, fmt) - converts a date/timestamp/string to a value of string in the format specified by the date format fmt.
date_format 参数1可以是date timestamp 和string类型,第二个是format格式(yyyy-MM-dd hh:mm:ss),返回值是string。
select "date_format('2009-07-30 12:13:14','yyyyMMddHHmmss')", date_format('2009-07-30 12:13:14','yyyyMMddHHmmss') union all
select "date_format(current_date,'yyyy-MM-dd HH:mm:ss')", date_format(current_date,'yyyy-MM-dd HH:mm:ss') union all
select "date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss') ",date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss')
3.unix_timestamp 和
unix_timestamp(date[, pattern]) - Converts the time to a number
这个是把时间转化为时间戳的 也就是我们常说的 时间到1970-01-01 过了多少ms,返回值bigint
from_unixtime(unix_time, format) - returns unix_time in the specified format
这个就是把时间戳转化为时间格式
select unix_timestamp(current_date) --1671379200
select unix_timestamp('20221219','yyyyMMdd') --1671379200
select unix_timestamp('20221219','yyyyMM') --4843814400 注意这里错了,格式一定要对
select from_unixtime(1671379200,'yyyyMM')--202212
这两个函数一般联合使用。
select "from_unixtime(UNIX_TIMESTAMP('20221219','yyyyMMdd'))",from_unixtime(UNIX_TIMESTAMP('20221219','yyyyMMdd'))union all
select "from_unixtime(UNIX_TIMESTAMP('2022/12/19','yyyy/MM/dd'))",from_unixtime(UNIX_TIMESTAMP('2022/12/19','yyyy/MM/dd'))
4.date_add
date_add(start_date, num_days) - Returns the date that is num_days after start_date.
start_date is a string in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. num_days is a number. The time part of start_date is ignored.
可以是string类型的日期,或者date类型或timestamp类型,返回值为date类型
select "date_add('2022-12-12',1)",date_add('2022-12-12',1) union all
select "date_add('2022-12-12 12:00:00', 1)",date_add('2022-12-12 12:00:00', 1)union all
select "date_add('2022/12/12 12:00:00', 1)",date_add('2022/12/12 12:00:00', 1) union all
select "date_add(CURRENT_DATE,1)",date_add(CURRENT_DATE,1) union all
select "date_add(CURRENT_timestamp,1)",date_add(CURRENT_timestamp,1)
5.add_months
add_months(start_date, num_months, output_date_format) - Returns the date that is num_months after start_date.
start_date is a string or timestamp indicating a valid date. num_months is a number. output_date_format is an optional String which specifies the format for output.
The default output format is 'YYYY-MM-dd'.
这个函数第一个入参是string类型的日期,或date,或者timestamp,第二个是增加的月份可以为负数,第三个是转化的类型,最后返回值是string类型
select "add_months('2009-07-30',1)",add_months('2009-07-30',1) union all
select "add_months(current_date,1)",add_months(current_date,1) union all
select "add_months(current_timestamp,1)",add_months(current_timestamp,1) union all
select "add_months(current_date,1,'yyyy-MM-dd HH:mm:ss')",add_months(current_date,1,'yyyy-MM-dd HH:mm:ss') union all
select "add_months('2009-07-30 12:13:14',1,'yyyyMMdd')",add_months('2009-07-30 12:13:14',1,'yyyyMMdd')
其实这个函数应该是分为两部分,第一部分计算增加月份,第二部分格式化date_format
6.其他日期函数
datediff
day
dayofmonth
DAYOFWEEK
floor_day
floor_month
floor_quarter
floor_year