Hive date functions Summary:
1. The time stamp function
- transfer stamp Date: from 1970-01-01 00:00:00 UTC time specified number of seconds
select unix_timestamp (); - to obtain the current time zone UNIX timestamp
select unix_timestamp ( '2017-09-15 14:23 : 00 ');
SELECT UNIX_TIMESTAMP (' 2017-09-15 14:23:00 ',' the mM-dd-YYYY HH: mm: SS ');
SELECT UNIX_TIMESTAMP (' 20,170,915 14:23:00 ',' HH yyyyMMdd : mm: ss');
- transfer date stamp
SELECT FROM_UNIXTIME (1505456567);
SELECT FROM_UNIXTIME (1505456567, 'yyyyMMdd');
SELECT FROM_UNIXTIME (1505456567, 'the MM-dd-YYYY HH: mm: SS');
SELECT FROM_UNIXTIME (UNIX_TIMESTAMP (), 'YYYY- mM-dd HH: mm: ss '); - the current time acquisition system
2. Get the current date: current_date
hive> select current_date from dual
2017-09-15
3. The date and time of transfer Date: to_date (string timestamp)
hive> select to_date(‘2017-09-15 11:12:00’) from dual;
2017-09-15
4. Get date in year / month / day / hour / minute / second / week
with dtime as(select from_unixtime(unix_timestamp(),‘yyyy-MM-dd HH:mm:ss’) as dt)
select year(dt),month(dt),day(dt),hour(dt),minute(dt),second(dt),weekofyear(dt)
from dtime
5. Calculate the number of days between two dates: datediff
hive> select datediff(‘2017-09-15’,‘2017-09-01’) from dual;
14
Increase and decrease 6. Date: date_add / date_sub (string startdate, int days)
hive> select date_add(‘2017-09-15’,1) from dual;
2017-09-16
hive> select date_sub(‘2017-09-15’,1) from dual;
2017-09-14
Other date functions
Query the current system time (including milliseconds): current_timestamp;
query the day of the month: dayofmonth (current_date);
end: last_day (current_date)
month Day 1: date_sub (current_date, dayofmonth ( current_date) -1)
of the following month 1 days: add_months (date_sub (current_date, dayofmonth (current_date) -1), 1)