Date & Time Functions

Apache Pig provides the different types of Date and Time functions to work on the Date and Time data types such as ToDate, GetHour, ToDate, and so on. In this tutorial, we will see the Date and Time function.

The following is the list of Apache Pig Date and Time functions.

S.No. Functions Description
1 ToDate(milliseconds) After applying this function the result is returned as a date-time object.
2 CurrentTime() After applying this function the result is returned as a date-time object for the current time.
3 GetDay(DateTime) After applying this function the result is returned as a day of a month.
4 GetHour(DateTime) After applying this function the result is returned as the hour of a day.
5 GetMilliSecond(DateTime) After applying this function the result is returned the millisecond of a second
6 GetMinute(DateTime) After applying this function the result is returned to the minute of an hour.
7 GetMonth(DateTime) After applying this function the result is returned the month of a year.
8 GetSecond(DateTime) After applying this function the result is returned the second of a minute.
9 GetWeek(DateTime) After applying this function the result is returned the week of a year.
10 GetWeekYear(DateTime) After applying this function the result is returned the week year.
11 GetYear(DateTime) After applying this function the result is returned the year.
12 AddDuration(DateTime, duration) After applying this function the result is returned to the result of a date-time object along with the duration object.
13 SubtractDuration(DateTime, duration) Subtracts the Duration object from the Date-Time object and returns the result.
14 DaysBetween(datetime1, datetime2) After applying this function the result is returned the number of days between the two date-time objects.
15 HoursBetween(datetime1, datetime2) After applying this function the result is returned the number of hours between two date-time objects.
16 MilliSecondsBetween(datetime1, datetime2) After applying this function the result is returned the number of milliseconds between two date-time objects.
17 MinutesBetween(datetime1, datetime2) After applying this function the result is returned the number of minutes between two date-time objects.
18 MonthsBetween(datetime1, datetime2) After applying this function the result is returned the number of months between two date-time objects.
19 SecondsBetween(datetime1, datetime2) After applying this function the result is returned the number of seconds between two date-time objects.
20 WeeksBetween(datetime1, datetime2) After applying this function the result is returned the number of weeks between two date-time objects.
21 YearsBetween(datetime1, datetime2) After applying this function the result is returned the number of years between two date-time objects.

Let us see the below example.


CurrentTime()

The currentTime function is used to return the DateTime object of the current time.

Syntax:
grunt> CurrentTime()

We have used the “date_detail.txt” dataset to perform this operation. We will put “date_detail.txt” in the HDFS location “/pigexample/” from the local file system.

Content of “date_detail.txt”:

1,1999/10/13 04:00:00 2,1988/06/20 11:22:00 3,1990/11/23 02:11:44 4,1997/05/27 06:11:44 5,1978/02/13 05:11:44

We will load “date_detail.txt” from the local filesystem into HDFS “/pigexample/” using the below commands.

Command:
$hadoop fs -copyFromLocal /home/cloudduggu/pig/tutorial/date_detail.txt /pigexample/

Now we will create relation "datedata" and load data from HDFS to Pig.

Command:
grunt> datedata = LOAD '/pigexample/date_detail.txt' USING PigStorage(',')
   as (sr_number:int,date:chararray);


Now we will use CurrentTime() function to generate current time from relation “datedata” and store it in the "currenttime" relation and using the DUMP operator we will print output on the terminal.

Command:
grunt> currenttime  = foreach datedata generate CurrentTime();
grunt> DUMP currenttime;

Output:
output of currenttime command