Skip to main content Skip to complementary content

TalendDate routine

The TalendDate routine contains several functions which allow you to carry out different kinds of operations and checks concerning the format of Date expressions.

Information noteWarning: These functions internally use the Java class SimpleDateFormat except when the pattern is set to yyyy-MM-dd or yyyy-MM-dd HH:mm:ss, where there is no check for the format of the input date string in order to achieve better performance. You need to make sure the input date string matches the set pattern precisely when using either of these two patterns.

You can access these functions by double-clicking the TalendDate node under the system routines folder in the Repository tree view.

Functions of the TalendDate routine

Function

Description

Syntax

addDate

Adds n days, n months, n hours, n minutes or n seconds to a Java date and returns the new date.

The Date format is: yyyy, MM, dd, HH, mm, ss or SSS.

TalendDate.addDate("initial date string", ["date format - eg.: yyyy/MM/dd"], integer n,"format of the part of the date to which n is to be added - eg.:yyyy").

compareDate

Compares all or part of two dates according to the format specified. Returns 0 if the dates are identical, -1 if the first date is earlier and 1 if the second date is earlier.

The pattern parameter specifies the date format, for example, "yyyy-mm-dd".

TalendDate.compareDate(Date date1, Date date2,[String pattern])

diffDate

Returns the difference between two dates in terms of years, months, days, hours, minutes, seconds, or milliseconds according to the comparison parameter specified.

The dateType parameter specifies the date part to be compared. The valid values are: "yyyy", "MM", "dd", "HH", "mm", "ss", "SSS". The default value is "dd".

The ignoreDST parameter specifies if DST needs to be taken into consideration.

TalendDate.diffDate(Date date1, Date date2,[String dateType],[boolean ignoreDST])

For example, TalendDate.diffDate(TalendDate.parseDate("yyyy-MM-dd", "2022-12-01"), TalendDate.parseDate("yyyy-MM-dd", "2022-10-02"), "MM") returns 2.

diffDateFloor

Returns the difference between two dates in terms of years or months according to the comparison parameter specified. The value is rounded down to the nearest integer.

The dateType parameter specifies the date part to be compared. The valid values are: "yyyy", "MM".

TalendDate.diffDateFloor(Date date1, Date date2, String dateType)

For example, TalendDate.diffDateFloor(TalendDate.parseDate("yyyy-MM-dd", "2022-12-01"), TalendDate.parseDate("yyyy-MM-dd", "2022-10-02"), "MM") returns 1.

diffDateIgnoreDST

Returns the difference between two dates in terms of days, months or years according to the comparison parameter specified, ignoring the DST.

The dateType parameter specifies the format of the date part to be compared, for example, "yyyy".

TalendDate.diffDateIgnoreDST(Date1(), Date2(),[String dateType])

formatDate

Returns a date string which corresponds to the format specified.

TalendDate.formatDate("date format - eg.: yyyy-MM-dd HH:mm:ss", Date() to be formatted)

formatDateInTimeZone

Formats a date into the specified format date/time string under the given timezone.

TalendDate.formatDateInTimeZone(String datePattern, Date tate, String TimeZoneID)

formatDateInUTC

Formats a date into the specified format date/time string under the UTC timezone.

TalendDate.formatDateInUTC(String datePattern, Date tate, String TimeZoneID)

formatDateLocale

Changes a date into a date/hour string according to the format used in the target country.

TalendDate.formatDateLocale("format target", java.util.Date date, "language or country code")

formatDatetimeoffset

Formats date to MSSQL 2008 type datetimeoffset ISO 8601 string with local time zone format string: yyyy-MM-dd HH:mm:ss.SSSXXX (JDK7 support it).

TalendDate.formatDatetimeoffset(Date date)

getCurrentDate

Returns the current date. No entry parameter is required.

TalendDate.getCurrentDate()

getDate

Returns the current date and hour in the format specified (optional). This string can contain fixed character strings or variables linked to the date. By default, the string is returned in the format of DD/MM/CCYY.

TalendDate.getDate("Format of the string - ex: CCYY-MM-DD")

getFirstDayOfMonth

Changes the date of an event to the first day of the current month and returns the new date.

TalendDate.getFirstDayMonth(Date)

getLastDayOfMonth

Changes the date of an event to the last day of the current month and returns the new date.

TalendDate.getLastDayMonth(Date)

getPartOfDate

Returns part of a date according to the format specified. This string can contain fixed character strings or variables linked to the date.

TalendDate.getPartOfDate("String indicating the part of the date to be retrieved, "String in the format of the date to be parsed")

getRandomDate

Returns a random date, in the ISO format.

TalendDate.getRandomDate("format date of the character string", String minDate, String maxDate)

isDate

Checks whether the date string corresponds to the format specified. Returns the boolean value true or false according to the outcome.

TalendDate.isDate(Date() to be checked, String pattern, [boolean ignoreTimeZone])

where, pattern specifies the format of the date to be checked. For example, yyyy-MM-dd HH:mm:ss.

isDateStrict

Tests string value as a date with right pattern using strict rules. This validation uses Java 8 time tools. The range of time-zone offsets is restricted to -18:00 to 18:00 inclusive. Returns a boolean value indicating whether the stringDate is a date string with a right pattern.

TalendDate.isDateStrict(String stringDate, String pattern)

parseDate

Changes a string into a Date. Returns a date in the specified format.

TalendDate.parseDate("format date of the string to be parsed", "string in the format of the date to be parsed",["boolean about whether parsing is set to be lenient, that is to say, accepting the heuristic match with the format"])

parseDateInUTC

changes a string into a Date in UTC. Returns a date in the UTC format.

In a Big Data Job running in a production environment, as multiple machines are involved from different time zones, you need always use this routine to guarantee the correct output of date/time parsing.

TalendDate.parseDateInUTC("format date of the string to be parsed", "string in the format of the date to be parsed", ["boolean about whether parsing is set to be lenient, that is to say, accepting the heuristic match with the format"])

parseDateLocale

Parses a string according to a specified format and extracts the date. Returns the date according to the local format specified.

TalendDate.parseDateLocale("date format of the string to be parsed", "String in the format of the date to be parsed", "code corresponding to the country or language")

setDate

Modifies part of a date according to the part and value of the date specified and the format specified.

TalendDate.setDate(Date, whole n, "format of the part of the date to be modified - eg.:yyyy")

TO_CHAR

Converts a date to a character string.

TalendDate.TO_CHAR(date[,format])

  • date: the date value you want to convert to a character string.
  • format: the string which defines the format of the return value.

TO_DATE

Converts a character string to a Date/Time datatype.

TalendDate.TO_DATE(string[, format])

  • string: the string you want to convert to a Date/Time datatype.
  • format: the format string that matches the part of the string argument. If not specified, the string value must be in the date format MM/dd/yyyy HH:mm:ss.SSS.

For example, TalendDate.TO_DATE("04/24/2017 13:55:42.123") will return Mon Apr 24 13:55:42 CST 2017.

ADD_TO_DATE

Adds a specified amount to one part of a datetime value, and returns a date in the same format as the date you pass to the function.

TalendDate.ADD_TO_DATE(date, format, amount)

  • date: the date value you want to change.
  • format: the format string specifying the portion of the date value you want to change.
    • Valid format strings for year: Y, YY, YYY, and YYYY.
    • Valid format strings for month: MONTH, MM, and MON.
    • Valid format strings for day: D, DD, DDD, DAY, and DY.
    • Valid format strings for hour: HH, HH12, and HH24.
    • Valid format string for minute: MI.
    • Valid format string for second: SS.
    • Valid format string for millisecond: MS.
  • amount: the integer value specifying the amount of years, months, days, hours, and so on by which you want to change the date value.

For example,

if TalendDate.getCurrentDate() returns Mon Apr 24 14:26:03 CST 2017,

TalendDate.ADD_TO_DATE(TalendDate.getCurrentDate(), "YY", 1) will return Tue Apr 24 14:26:03 CST 2018.

Information noteWarning:

Although "yyyy" and "YYYY" in the date format return the same year number in most cases, "YYYY" may not work as expected when used:

  • at the first week of a year if the year does not start by the first day of the week.
  • at the last week of a year if the year does not end by the last day of the week.

For example, when calculating what date it is 3 days before January 2, 2016, the code below would return a wrong date:

System.out.println(TalendDate.formatDate("YYYY-MM-dd", TalendDate.addDate(TalendDate.TO_DATE("01/02/2016 08:10:30.123"), -3, "dd")));

while the following code would work as expected:

System.out.println(TalendDate.formatDate("yyyy-MM-dd", TalendDate.addDate(TalendDate.TO_DATE("01/02/2016 08:10:30.123"), -3, "dd")));

Therefore, you should typically use "yyyy", which represents calendar year.

Formatting a Date

The formatDate routine is easy to use, along with a tJava component:

System.out.println(TalendDate.formatDate("dd-MM-yyyy", new Date()));

The current date is initialized according to the pattern specified by the new Date() Java function and is displayed in the Run view:

The result is 2010-02-25 17:28:07 in the Run view.

Checking a Date

It is easy to use the isDate routine, along with a tJava component to check if a date expression is in the format specified:

System.out.println(TalendDate.isDate("2010-02-09 00:00:00",
               "yyyy-MM-dd HH:mm:ss"));

A boolean is returned in the Run view:

The result is "true" in the Run view.

Comparing Dates

It is easy to use the compareDate routine, along with a tJava component to compare two dates, for example, to check if a date is identical to, earlier than, or later than a specific date, according to the format specified. This routine takes two parameters (the two dates) and returns one of the following three values.
  • -1: the first date is earlier than the second date.
  • 0: the first date is identical to the second date.
  • 1: the first date is later than the second date.

The following gives an example of using this routine.

System.out.println(TalendDate.compareDate(TalendDate.parseDate("yyyy-MM-dd", "2015/11/24"), TalendDate.parseDate("yyyy-MM-dd", "2025/11/24")));

This example compares the two dates: 2015/11/24 and 2025/11/24 and returns the value -1 in the Run view, indicating that the first date is earlier than the second date.

The result is -1 in the Run view.

Configuring a Date

It is easy to use the setDate routine, along with a tJava component to change the year of the current date, for example:
System.out.println(TalendDate.formatDate("yyyy/MM/dd HH:mm:ss",new Date()));
                  System.out.println(TalendDate.setDate(new Date(),2011,"yyyy"));

The current date, followed by the new date are displayed in the Run view:

The result is 2010/02/26 18:03:14 and then Sat Feb 26 18:03:14 CET 2011 in the Run view.

Parsing a Date

It is easy to use the parseDate routine, along with a tJava component to change a date string from one format into another date format, for example:
System.out.println(TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",
                  "1979/10/20 19:00:59"));

The string is changed and returned in the date format:

The result is Sat Oct 20 19:00:59 CET 1979 in the Run view.

Retrieving part of a Date

It is easy to use the getPartOfDate routine, along with a tJava component to retrieve part of a date, for example:

Date D=TalendDate.parseDate("dd-MM-yyyy HH:mm:ss", "13-10-2010 12:23:45");
               System.out.println(D.toString());
               System.out.println(TalendDate.getPartOfDate("DAY_OF_MONTH", D));
               System.out.println(TalendDate.getPartOfDate("MONTH", D));
               System.out.println(TalendDate.getPartOfDate("YEAR", D));
               System.out.println(TalendDate.getPartOfDate("DAY_OF_YEAR", D));
               System.out.println(TalendDate.getPartOfDate("DAY_OF_WEEK", D));

In this example, the day of month (DAY_OF_MONTH), the month (MONTH), the year (YEAR), the day number of the year (DAY_OF_YEAR) and the day number of the week (DAY_OF_WEEK) are returned in the Run view. All the returned data are numeric data types.

The result is Wed Oct 13 12:23:45 CEST 2010, and then 13, 9, 2010, 286, and 4 in the Run view.
Information noteNote: In the Run view, the date string referring to the months (MONTH) starts with 0 and ends with 11: 0 corresponds to January, 11 corresponds to December.

Formatting the Current Date

It is easy to use the getDate routine, along with a tJava component, to retrieve and format the current date according to a specified format, for example:

System.out.println(TalendDate.getDate("CCYY-MM-DD"));

The current date is returned in the specified format (optional):

The result is 2010-03-02 in the Run view.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!