日期函数
无论做什么数据,都离不开日期函数的使用。
这里记录一下Hive的日期函数的使用,写的相当完整。
1.日期函数UNIX时间戳转日期函数: from_unixtime
语法:from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive> select from_unixtime(1323308943,'yyyyMMdd') from dual;
20111208
2.获取当前UNIX时间戳函数: unix_timestamp
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
hive> select unix_timestamp() from dual;
1323309615
3.日期转UNIX时间戳函数: unix_timestamp
语法:unix_timestamp(string date)
返回值: bigint
说明: 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp('2011-12-07 13:01:03') from dual;
1323234063
4.指定格式日期转UNIX时间戳函数: unix_timestamp
语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from dual;
1323234063
5.日期时间转日期函数: to_date
语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。
举例:
hive> select to_date('2011-12-08 10:03:01') from dual;
2011-12-08
6.日期转年函数: year
语法: year(string date)
返回值: int
说明: 返回日期中的年。
举例:
hive> select year('2011-12-08 10:03:01') from dual;
2011
hive> select year('2012-12-08') from dual;
2012
7.日期转月函数: month
语法: month (string date)
返回值: int
说明: 返回日期中的月份。
举例:
hive> select month('2011-12-08 10:03:01') from dual;
12
hive> select month('2011-08-08') from dual;
8
8.日期转天函数: day
语法: day (string date)
返回值: int
说明: 返回日期中的天。
举例:
hive> select day('2011-12-08 10:03:01') from dual;
8
hive> select day('2011-12-24') from dual;
24
9.日期转小时函数: hour
语法: hour (string date)
返回值: int
说明: 返回日期中的小时。
举例:
hive> select hour('2011-12-08 10:03:01') from dual;
10
10.日期转分钟函数: minute
语法: minute (string date)
返回值: int
说明: 返回日期中的分钟。
举例:
hive> select minute('2011-12-08 10:03:01') from dual;
3
11.日期转秒函数: second
语法: second (string date)
返回值: int
说明: 返回日期中的秒。
举例:
hive> select second('2011-12-08 10:03:01') from dual;
1
12.日期转周函数: weekofyear
语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
举例:
hive> select weekofyear('2011-12-08 10:03:01') from dual;
49
13.日期比较函数: datediff
语法: datediff(string enddate, string startdate)
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
hive> select datediff('2012-12-08','2012-05-09') from dual;
213
14.日期增加函数: date_add
语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
hive> select date_add('2012-12-08',10) from dual;
2012-12-18
15.日期减少函数: date_sub
语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive> select date_sub('2012-12-08',10) from dual;
2012-11-28
16. trunc(string date, string format)
说明:获取指定日期月份的第一天、年份的第一天
官方说明:
Returns date truncated to the unit specified by the format (as of Hive 1.2.0). Supported formats: MONTH/MON/MM, YEAR/YYYY/YY. Example: trunc(‘2015-03-17’, ‘MM’) = 2015-03-01
在hive1.2.0版本后执行此函数,此函数返回指定格式的单元,指定格式有:MONTH/MON/MM, YEAR/YYYY/YY
> select trunc('2019-02-24', 'YYYY');
+-------------+--+
| _c0 |
+-------------+--+
| 2019-01-01 |
+-------------+--+
> select trunc('2019-02-24', 'MM');
+-------------+--+
| _c0 |
+-------------+--+
| 2019-02-01 |
+-------------+--+
17.next_day(string start_date, string day_of_week)
说明:指定日期下周的指定周几
官网说明:
Returns the first date which is later than start_date and named as day_of_week (as of Hive 1.2.0). start_date is a string/date/timestamp. day_of_week is 2 letters, 3 letters or full name of the day of the week (e.g. Mo, tue, FRIDAY). The time part of start_date is ignored. Example: next_day(‘2015-01-14’, ‘TU’) = 2015-01-20.
在hive1.2.0版本后才支持此函数
> select next_day('2019-02-24', 'TU');
+-------------+--+
| _c0 |
+-------------+--+
| 2019-02-26 |
+-------------+--+
> select next_day('2019-02-24', 'MONDAY');
+-------------+--+
| _c0 |
+-------------+--+
| 2019-02-25 |
+-------------+--+
18.add_months(string start_date, int num_months, output_date_format)
说明:按指定格式返回指定日期增加几个月后的日期
官网说明:
Returns the date that is num_months after start_date (as of Hive 1.1.0). start_date is a string, date or timestamp. num_months is an integer. If start_date is the last day of the month or if the resulting month has fewer days than the day component of start_date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as start_date. The default output format is ‘yyyy-MM-dd’.
Before Hive 4.0.0, the time part of the date is ignored.
As of Hive 4.0.0, add_months supports an optional argument output_date_format, which accepts a String that represents a valid date format for the output. This allows to retain the time format in the output.
For example :
add_months(‘2009-08-31’, 1) returns ‘2009-09-30’.
add_months(‘2017-12-31 14:15:16’, 2, ‘YYYY-MM-dd HH:mm:ss’) returns ‘2018-02-28 14:15:16’.
此函数在hive1.1.0版本后
> select add_months('2019-02-28', 1);
+-------------+--+
| _c0 |
+-------------+--+
| 2019-03-31 |
+-------------+--+
> select add_months('2019-02-24 21:15:16', 2, 'YYYY-MM-dd HH:mm:ss');
+----------------------+--+
| _c0 |
+----------------------+--+
| 2019-04-24 21:15:16 |
+----------------------+--+
19.last_day获取每个月最后一天
select last_day('2020-10-01')
2020-10-31
SELECT last_day('2003-03-15 01:22:33')
2003-03-31
20.current_date获取当前时间
0: jdbc:hive2://linux01:10000> select current_date;
+-------------+
| _c0 |
+-------------+
| 2020-09-14 |
+-------------+
21.current_timestamp获取当前时间带时分秒
0: jdbc:hive2://linux01:10000> select current_timestamp;
+-------------------------+
| _c0 |
+-------------------------+
| 2020-09-14 17:16:19.88 |
+-------------------------+
21.获取当前时间戳
0: jdbc:hive2://linux01:10000> select UNIX_TIMESTAMP();
+-------------+
| _c0 |
+-------------+
| 1600074118 |
+-------------+
22.现在是当月的第几天
> select dayofmonth(current_date) 今天是5月10日
0: jdbc:hive2://emr-header-1:10000> select dayofmonth(current_date);
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
+------+
| _c0 |
+------+
| 22 |
+------+
————————————————
23.获取当月月份
0: jdbc:hive2://linux01:10000> select substr(current_date , 1 ,7 );
+----------+
| _c0 |
+----------+
| 2020-09 |
+----------+
24.获取本月月初第一天
select date_sub(current_date,dayofmonth(current_date)-1);
0: jdbc:hive2://linux01:10000> select date_sub(current_date,dayofmonth(current_date)-1);
+-------------+
| _c0 |
+-------------+
| 2020-09-01 |
+-------------+