SPARK-SQL內置函數之時間日期類


一、獲取當前時間
1.current_date獲取當前日期
2018-04-09

2.current_timestamp/now()獲取當前時間
2018-04-09 15:20:49.247

二、從日期時間中提取字段
1.year,month,day/dayofmonth,hour,minute,second
Examples:> SELECT day('2009-07-30'); 30

2.dayofweek (1 = Sunday, 2 = Monday, ..., 7 = Saturday),dayofyear
Examples:> SELECT dayofweek('2009-07-30');   5

Since: 2.3.0

3.weekofyear
weekofyear(date) - Returns the week of the year of the given date. A week is considered to start on a Monday and week 1 is the first week with >3 days.

Examples:> SELECT weekofyear('2008-02-20');   8

4.trunc截取某部分的日期,其他部分默認為01
第二個參數 ["year", "yyyy", "yy", "mon", "month", "mm"]

Examples:

SELECT trunc('2009-02-12', 'MM');
2009-02-01
SELECT trunc('2015-10-27', 'YEAR');
2015-01-01
5.date_trunc ["YEAR", "YYYY", "YY", "MON", "MONTH", "MM", "DAY", "DD", "HOUR", "MINUTE", "SECOND", "WEEK", "QUARTER"]
Examples:> SELECT date_trunc('2015-03-05T09:32:05.359', 'HOUR');  2015-03-05T09:00:00

Since: 2.3.0

6.date_format將時間轉化為某種格式的字符串
Examples:> SELECT date_format('2016-04-08', 'y');    2016

三、日期時間轉換
1.unix_timestamp返回當前時間的unix時間戳
Examples:

SELECT unix_timestamp();  1476884637
SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');   1460041200
2.from_unixtime將時間戳換算成當前時間,to_unix_timestamp將時間轉化為時間戳
Examples:

SELECT from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');  1970-01-01 00:00:00

SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');  
1460041200

SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd');  
1460041200
3.to_date/date將字符串轉化為日期格式,to_timestamp(Since: 2.2.0)
SELECT to_date('2009-07-30 04:17:52');  2009-07-30
SELECT to_date('2016-12-31', 'yyyy-MM-dd');   2016-12-31
SELECT to_timestamp('2016-12-31 00:12:00');   2016-12-31 00:12:00
4.quarter 將1年4等分(range 1 to 4)
Examples:> SELECT quarter('2016-08-31'); 3

四、日期、時間計算
1.months_between兩個日期之間的月數
months_between(timestamp1, timestamp2) - Returns number of months between timestamp1 and timestamp2.

Examples:> SELECT months_between('1997-02-28 10:30:00', '1996-10-30');  3.94959677

  1. add_months返回日期后n個月后的日期
    Examples:> SELECT add_months('2016-08-31', 1);  2016-09-30

3.last_day(date),next_day(start_date, day_of_week)
Examples:

SELECT last_day('2009-01-12');  2009-01-31
SELECT next_day('2015-01-14', 'TU');  2015-01-20
4.date_add,date_sub(減)
date_add(start_date, num_days) - Returns the date that is num_days after start_date.

Examples:

SELECT date_add('2016-07-30', 1);  2016-07-31
5.datediff(兩個日期間的天數)
datediff(endDate, startDate) - Returns the number of days from startDate to endDate.

Examples:> SELECT datediff('2009-07-31', '2009-07-30'); 1

6.關於UTC時間
to_utc_timestamp
to_utc_timestamp(timestamp, timezone) - Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, 'GMT+1' would yield '2017-07-14 01:40:00.0'.

Examples:> SELECT to_utc_timestamp('2016-08-31', 'Asia/Seoul');  2016-08-30 15:00:0

from_utc_timestamp
from_utc_timestamp(timestamp, timezone) - Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in UTC, and renders that time as a timestamp in the given time zone. For example, 'GMT+1' would yield '2017-07-14 03:40:00.0'.

Examples:> SELECT from_utc_timestamp('2016-08-31', 'Asia/Seoul');  2016-08-31 09:00:00

參考 https://blog.csdn.net/bowenlaw/article/details/111644932


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM