Talend Date Routine functions with Examples-Part 1

Talend Has various System routines (custom code classes) and we will see how to use each of them.

Talend Date

is a public class which has several static function, those are very useful when you deal with dates.

It will help you easily manipulate, parse, convert and compare dates from various formats.

Is Date.

isDate method test string value as a date (with right pattern). If pattern is wrong then no use of using this function.

@param stringDate (A <code>String</code> whose beginning should be parsed)
@param pattern (the pattern to format, like: “yyyy-MM-dd HH:mm:ss”)
@return the result wheather the stringDate is a date string that with a right pattern

System.out.println(TalendDate.isDate("2008-11-24 12:15:25", "yyyy-MM-dd HH:mm"));
output: false (second is missing)
System.out.println(TalendDate.isDate("2008-11-32 12:15:25", "yyyy-MM-dd HH:mm:ss"));
output: false (date has 32 day)

isDate with Ignore time Zone.

isDate(String stringDate, String pattern, boolean ignoreTimeZone) this method will ignore time Zone when parse with given pattern. It will accept three parameters.

@param stringDate (A <code>String</code> whose beginning should be parsed)
@param pattern (the pattern to format, like: “yyyy-MM-dd HH:mm:ss”)
@param ignoreTimeZone (if true ignore TimeZone when pare date with pattern)
@return the result whether the stringDate is a date string that with a right pattern

System.out.println(TalendDate.isDate("20110327 021711", "yyyyMMdd HHmmss",true));

 Get Current Date Method

This method return the current date with time stamp. it has no input parameters so you can use it directly wherever you need it.

Output: Mon Apr 01 13:28:22 IST 2015

Get Date Method.

Return the current datetime with the given display format
format : (optional) string representing thewished format of the date.
This string contains fixed strings and variables related to the date.
By default, the format string is “DD/MM/CCYY”.

@param pattern ( string date format no empty string)

Note: for real examples of pattern you can check on Convert String To Date post.

Output: 27/04/2015

 Get First Day Of Month Method.

This method takes one argument and return the first day of month for given date. Check below example for more details.

@param date (a date value)
@return a new date (the date has been changed to the first day)

Output:Wed Apr 01 13:40:11 IST 2015

 Get Last Day Of Month.

getLastDayOfMonth will return a last day of month for given date.

@param date (a date value)
@return a new date (the date has been changed to the last day)

Output: Thu Apr 30 13:43:12 IST 2015

Get Random Date.

getRandomDate will return an ISO formatted random date.

@param minDate string(“2007-01-01”) min : minimum date
@param max Date string(“2008-12-31”) max : maximum date (superior to min)

System.out.println(TalendDate.getRandomDate("2015-04-20", "2015-04-28"));
Output: Thu Apr 23 12:37:47 IST 2015

 Add Date Methods.

  • addDate(Date date, int nb, String dateType)

This function is used to add number of day, month … to a date (with Java date type !)

@param date (a <code>Date</code> type value)
@param nb (the value to add)
@param dateType (date pattern = (“yyyy”,”MM”,”dd”,”HH”,”mm”,”ss”,”SSS” ))
@return a new date in Date type.


Add 5 Years
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"yyyy")
Output: 2009/04/24 12:15:25
 Add 5 Months
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"MM")
Output:2009/04/24 12:15:25
 Add 5 Days
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"dd")
Output:2008/11/29 12:15:25
 Add 5 Hours
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"HH")
Output:2008/11/24 17:15:25
 Add 5 Minutes
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"mm")
Output: 2008/11/24 12:20:25
 Add 5 Seconds
TalendDate.addDate(TalendDate.parseDate("yyyy/MM/dd HH:mm:ss","2008/11/24 12:15:25"), 5,"ss")
Output: 2008/11/24 12:15:30


  • addDate(String string, String pattern, int nb, String dateType)

This method works in same way as above, except it has different signature to convert string to date using given format.

@param date (a Date given in string)
@param pattern (the pattern for the related date)
@param nb (the value to add)
@param dateType (date pattern = (“yyyy”,”MM”,”dd”,”HH”,”mm”,”ss”,”SSS” ))
@return a new date in String type.


TalendDate.addDate("2008/11/24 12:15:25","yyyy/MM/dd HH:mm:ss", 5,"ss");
Output:2008/11/24 12:15:30

As you can see in example you don’t need to parse the date it is easy of parsing and adding values using single method but it returns date in string format.

 Get Part Of Date

  • getPartOfDate(String partName, Date date) returns int.

This function is used to get part of date.


It accepts two parameters, and return result in integer.

@param partName which part to get.
@param date the date value.
@return the specified part value.


Get Year
System.out.println("YEAR="+TalendDate.getPartOfDate("YEAR", TalendDate.getCurrentDate()));
And Output is: 
 Get Month
System.out.println("MONTH="+TalendDate.getPartOfDate("MONTH", TalendDate.getCurrentDate()));
System.out.println("HOUR="+TalendDate.getPartOfDate("HOUR", TalendDate.getCurrentDate()));
Output: HOUR=6
System.out.println("DAY_OF_WEEK="+TalendDate.getPartOfDate("DAY_OF_WEEK", TalendDate.getCurrentDate()));
System.out.println("WEEK_OF_MONTH"+TalendDate.getPartOfDate("WEEK_OF_MONTH", TalendDate.getCurrentDate()));
System.out.println("WEEK_OF_YEAR="+TalendDate.getPartOfDate("WEEK_OF_YEAR", TalendDate.getCurrentDate()));
System.out.println("TIMEZONE ="+TalendDate.getPartOfDate("TIMEZONE ", TalendDate.getCurrentDate()));
Output:TIMEZONE =0


 Compare Date.

CompareDate method compares two dates and return values in int. It accepts two parameters as date Object.

@param date1 (first date)

@param date2 (second date)

@return the result whether two date is the same.

If first one less than second one return number -1.

Equals return number 0, bigger than return number 1. (can compare partly)

Date compare Equal example.  
System.out.println(TalendDate.compareDate(TalendDate.parseDate("dd/MM/yyyy", "27/04/2015"), TalendDate.parseDate("dd/MM/yyyy", "27/04/2015")));
Output: 0
 Date compare greater than example. 
System.out.println(TalendDate.compareDate(TalendDate.parseDate("dd/MM/yyyy", "28/04/2015"), TalendDate.parseDate("dd/MM/yyyy", "27/04/2015")));
Output: 1
 Date compare less than example.
System.out.println(TalendDate.compareDate(TalendDate.parseDate("dd/MM/yyyy", "24/04/2015"), TalendDate.parseDate("dd/MM/yyyy", "27/04/2015")));
Output: -1


Next part of the post we will explain rest of the functions with examples.

Next-Talend Date Routine-Function with examples- Part-2


About dwetl

4 comments on “Talend Date Routine functions with Examples-Part 1

  1. Hi i’m having one query.
    I need to convert the given time to EST format in a given table. I converted using
    But now the requirement is i need to change the time zone for each country from where the table is updated. Its a huge process to move the job to dev system and edit the time and again moving it to production system.
    Whether there are any possibilities to create a configuration file such that only the time can be changed and it can be moved to production system easily. Can you help me out with this.

    1. Hi Shivraj,

      Yes time value can be loaded from context variable using context load component, for that create context variables for timeValue with integer type and then define this variable in configuration file.
      assign appropriate time value to timeValue in config file then it will be loaded using tContextLoad component.

      hope this helps you

      Thank You

      1. Hi umesh,

        Thanks for ur response. Can you suggest me how to create a configuration file. It is the first time i’m facing these kind of scenario. Can you please help me out in this, so that it can be used as an example in future if some one facing these kind of scenario.
        Whether its possible to write a “IF” statement in TalendDate.addDate(myDate,addValue,”MM”) and load the time variables such that the time zone will be automatically identified.
        The actual scenario is i’m having a table which contains ID,Fname,Lname,email,added_date,converted_date as column names.
        Initially while loading into table all the 5 column will be loaded and converted_date will be null(added_date will be in IST format).
        I created a simple job such that if i run the job the converted_date will be loaded to their required time format.
        Can you help me in creating a configuration file such that i don’t need to change time whenever the time zone changes.

Leave a Reply

Your email address will not be published. Required fields are marked *