You will need to use Talend routines. For example you can create a stringToDate() custom method. In its simpler form (considering you are tackling just one locale) you will pass just a String a parameter (not the locale). You will need to add the formats you will allow like you see in the function below. The class and the main method are just for testing purposes and you can see it in action here. These days is so much easier to share snippets of code that actually run ;-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* See it in action in http://runnable.com/VIHI8AhI-_MpHcsM/multipledatesparser-java | |
* | |
* */ | |
class MultipleDatesParser { | |
public static String stringToDate (String dateToParse) throws java.lang.Exception { | |
String parsedDate = null; | |
java.text.SimpleDateFormat sdf = null; | |
try { | |
sdf = new java.text.SimpleDateFormat("MM/DD/yy"); | |
parsedDate = sdf.parse(dateToParse).toString(); | |
} catch (Exception e1) { | |
try { | |
sdf = new java.text.SimpleDateFormat("MM/DD/yyyy"); | |
parsedDate = sdf.parse(dateToParse).toString(); | |
} catch (Exception e2) { | |
//Other data formats here following the pattern above or ... | |
throw e2; | |
} | |
} | |
return parsedDate; | |
} | |
public static void main (String[] args) throws java.lang.Exception { | |
System.out.println(stringToDate("12/20/2014")); | |
System.out.println(stringToDate("12/20/14")); | |
} | |
} |
No comments:
Post a Comment