TalendDate Routines - 6.3

Talend Real-time Big Data Platform Studio User Guide

EnrichVersion
6.3
EnrichProdName
Talend Real-Time Big Data Platform
task
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

The TalendDate routines allow you to carry out different kinds of operations and checks concerning the format of Date expressions.

To access these routines, double click on TalendDate under the system folder:

Routine

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.

TalendDate.compareDate(Date date1, Date date2, "format to be compared - eg.: yyyy-MM-dd")

diffDate

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

TalendDate.diffDate(Date1(), Date2(), "format of the part of the date to be compared - eg.:yyyy")

diffDateFloor

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

TalendDate.diffDateFloor(Date1(), Date2(), "format of the part of the date to be compared - eg.:MM")

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

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")

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, 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, "format of the date to be checked - eg.: yyyy-MM-dd HH:mm:ss")

parseDate

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

TalendDate.parseDate("format date of the string to be parsed", "string in the format of the date to be parsed")

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")

Warning

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.

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

How to format 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:

How to check 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:

How to compare Dates

It is easy to use the compareDate routine, along with a tJava component to compare two dates, for example to check if the current date is identical to, earlier than or later than a specific date, according to the format specified.

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

In this example the current date is initialized by the Java function new Date()and the value -1 is displayed in the Run view to indicate that the current date is earlier than the second date.

How to configure 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:

How to parse 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:

How to retrieve 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.

Note

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.

How to format 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):