Search code examples
stringdatetimetalend

Convert string to date to string in Talend Open Studio


I am trying to covert a date from one string format ("M/d/yyyy H:mm:ss") to another ("yyyy-MM-ddTHH:mm:ss") in Talend Open Studio. I've been reading through forum posts, blogs, and the help documentation, and all of the solutions I've found ultimately result in error. I think it's because most solutions are actually attempting to convert a date to a string or vice versa, but none of them are attempting, as I am, to convert a string to a date and then back again. (And I'm willing to be told that's the wrong way to approach this, but it seems like the natural way to do the conversion since, presumably, Talend knows how to convert all the parts of the Date type.)

My current iteration looks like this:

row2._ACTIVITY_DUE_DATE_==""?"":TalendDate.formatDate("yyyy-MM-dd HH:mm:ss", TalendDate.parseDate("M/d/yyyy H:mm:ss", row2._ACTIVITY_DUE_DATE_)) 

So if I feed in 5/18/2012 1:00:00 PM, I would want to get back 2012-05-18 13:00:00. I'm getting an unparsable date error, which I've included below. I don't understand why this is happening. The expression above should only be trying to parse the date if it's not equal to "", but apparently that's the value it can't parse:

Exception in component tMap_1
java.lang.RuntimeException: java.text.ParseException: Unparseable date: ""
at routines.TalendDate.parseDate(TalendDate.java:864)
at routines.TalendDate.parseDate(TalendDate.java:808)
at     msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.tFileInputDelimited_1Process(MSM_Activities_i360_Tasks.java:2854)
at msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.runJobInTOS(MSM_Activities_i360_Tasks.java:3690)
at msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.main(MSM_Activities_i360_Tasks.java:3549)
Caused by: java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:850)
... 4 more

Solution

  • When comparing strings in Java you should use .equals. So in your case you should use "".equals(row2._ACTIVITY_DUE_DATE) ? "" : ....

    Everything else about your date format conversion looks fine.