Talend Date Routine functions with Examples-Part 1

Talend Date Routine functions with Examples-Part 1
4.75 (95%) 4 votes

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

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

 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.

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.

 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)

 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)

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)

 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.

Examples:

Add 5 Years

 Add 5 Months

 Add 5 Days

 Add 5 Hours

 Add 5 Minutes

 Add 5 Seconds

 

  • 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.

Example:

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.

like YEAR, MONTH, HOUR, or DAY_OF_WEEK, WEEK_OF_MONTH, WEEK_OF_YEAR, TIMEZONE and so on.

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.

e.g.

Get Year

 Get Month

 Get HOUR

 Get DAY_OF_WEEK

 Get WEEK_OF_MONTH

 Get WEEK_OF_YEAR

 Get TIMEZONE

 

 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.  

 Date compare greater than example. 

 Date compare less than example.

 

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
    TalendDate.addDate(row2.added_date,-630,”mm”),
    TalendDate.addDate(myDate,addValue,”MM”)
    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
      Umesh

      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 *