Need to Get the current DateTime in the Talend Studio?

Rajesh kannan picture Rajesh kannan · May 7, 2015 · Viewed 37.2k times · Source

I am working the Talend studio tool for data migration. Now I want to set the Current DateTime in the Date field. I get the DateTime from this code TalendDate.getDate("yyyy-MM-dd HH:mm:ss") but it returns String type data. But I need Date type to insert.Is there any String to date (Sample insert is like this:1999-12-13 16:14:48) conversion is in the Talend Studio.

Answer

Jordi Castilla picture Jordi Castilla · May 7, 2015

You can use routine function TalendDate.parseDate to convert a String to a Date.

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", yourStringData);

If you want the current datetime:

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss"));

But this makes no sense.

Parsedate function is prepared to receive a string and convert it to a Date object. Date objects have it's own format, so you don't have to care how is stored, you need to change the Date format in the moment you show it, but not when you store it:

// this will produce a correct Date Object to store in your Date field
Date currentDate = TalendDate.getCurrentDate();  

When you need to show/print it use SimpleDateFormat for example if you want to show 2015-07-05 16:00:00 you must do like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
System.out.println("My date formatted is: " + sdf.format(currentDate ));