diff --git a/main/plugins/org.talend.librariesmanager/resources/java/routines/system/RuntimeUtils.java b/main/plugins/org.talend.librariesmanager/resources/java/routines/system/RuntimeUtils.java index d04c03dd02..7a16a1eb68 100644 --- a/main/plugins/org.talend.librariesmanager/resources/java/routines/system/RuntimeUtils.java +++ b/main/plugins/org.talend.librariesmanager/resources/java/routines/system/RuntimeUtils.java @@ -12,6 +12,7 @@ // ============================================================================ package routines.system; +import java.text.ParseException; import java.util.Date; import routines.TalendDate; @@ -31,6 +32,8 @@ public class RuntimeUtils { public static String getRuntimeType(Object o) { return o.getClass().getName(); } + + private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss"; /** * This function is in order to check the Date type in tRunJob when transmit the context to child job. @@ -45,11 +48,40 @@ public class RuntimeUtils { // when tRunJob transmit the date to child job, it should format with "yyyy-MM-dd HH:mm:ss" if (isDateType(o)) { - return TalendDate.formatDate("yyyy-MM-dd HH:mm:ss", (Date) o); //$NON-NLS-1$ + return TalendDate.formatDate(DEFAULT_DATE_PATTERN, (Date) o); //$NON-NLS-1$ } return o; } + + public static Date getDate(String pattern, String dateString) { + if (dateString == null || dateString.isEmpty()) { + return null; + } + + // when tRunJob transmit the date to child job: + //case 1: pass date string with pattern : yyyy-MM-dd HH:mm:ss + //case 2: pass date long value + //so here process two cases both and avoid exception + try { + return new java.text.SimpleDateFormat(pattern).parse(dateString); + } catch(ParseException e) { + //ignore exception + } + + try { + return new Date(Long.valueOf(dateString)); + } catch(NumberFormatException e) { + //ignore exception + //System.err.println(String.format("Null value will be used for context parameter as can't parse this to date: %s", dateString)); + } + + return null; + } + + public static Date getDate(String dateString) { + return getDate(DEFAULT_DATE_PATTERN, dateString); + } public static void main(String[] args) { int i = 10;