parse date from string

One Star

parse date from string

I receive date in form of string like "2009-02-20 02:04:36"
i want to separate date and time and convert in date time format

Re: parse date from string

is TalendDate.parse("yyyy-MM-dd hh-mm-ss", row.aDt )
what you are looking for?
One Star

Re: parse date from string

I want date and time separate as these two goes in different columns.
One Star Lie
One Star

Re: parse date from string

Hi
Get your date with TalendDate.parse() then in a tMap, output two columns :
TalendDate.format("yyyy-MM-dd", row.myDate)
TalendDate.format("hh:mm:ss", row.myDate)

You can also use the Talend native date tool with which you can select the format of your date (in the schema, when you select Date as the type of your column, you can set the pattern of your date. Use "yyyy-MM-dd hh:mm:ss" in the input flow and use "yyyy-MM-dd" in the output date flow and "hh:mm:ss" in the output time flow)

Regards,
Lie
One Star

Re: parse date from string

I am using tJavaRow and performing this on my input string "2009-02-20 02:04:36"
dt=TalendDate.parseDate("yyyy-mm-dd HH:mm:ss",date);
dt=TalendDate.parseDate("MM-dd-yyyy",TalendDate.formatDate("MM-dd-yyyy", dt));
ti=TalendDate.parseDate("hh:mm:ss",TalendDate.formatDate("hh:mm:ss", dt));
but i m getting result as
20-01-2009|01-01-1970
which is totally wrong. I can only use tJavaRow as I m doing lot more thing in it.
One Star Lie
One Star

Re: parse date from string

You're doing it wrong.
You have a Date Object which contains all of your information "2009-02-20 02:04:36 +GMT blablabla"
You have an input string "2009-02-20 02:04:36". You convert it into a date object (with the parseDate) or natively in Talend when you choose "Date" as your column type.
Then you can output the same date object in the format you want :
"MM-dd-yyyy" will output your date object as "02-20-2009"
"HH:mm:ss" will output your date object as "02:04:36"
But you could also output your date as :
"MM/dd@ss+mm_dd" that would output your date object as "02/20@36+04_20" (that's just a fantasy example)
You just have ONE date object and TWO (or more) outputs.

myDate = TalendDate.parseDate("yyyy-mm-dd HH:mm:ss", input_row.date);
output_row.dt = myDate;
output_row.ti = myDate;

Then select "Date" type for dt and ti columns in your output_row and select the good format to output.
One Star

Re: parse date from string

This is working fine when I show output on tLogRow. But When I tries put output into Mysql database the 2 columns shows same date time.
Seven Stars

Re: parse date from string

The Date Patterns only affect file input or output (or tLogRow) because they are effectively display masks and do not change the underlying field value. That is why the full date value is being sent to MySQL.
If you send the results to DATE and TIME fields in MySQL rather than DATETIME fields, the extraneous part should just be ignored.
One Star

Re: parse date from string

what happens when you have the incoming date value as 01/0001 and you want output to be null for the date value. How do you do this?
Seven Stars

Re: parse date from string

Because that is not a valid date, you will not be able to read it from the file as a date without causing an error and thus the row to reject. You will need to use a tReplace to change the String "01/0001" to a null and then a tConvertType to change the (cleaned) Strings to a Date before writing to your output.