diff --git a/main/plugins/org.talend.librariesmanager/resources/java/routines/StringHandling.java b/main/plugins/org.talend.librariesmanager/resources/java/routines/StringHandling.java index ac773cdf09..201e1c56df 100644 --- a/main/plugins/org.talend.librariesmanager/resources/java/routines/StringHandling.java +++ b/main/plugins/org.talend.librariesmanager/resources/java/routines/StringHandling.java @@ -5,6 +5,8 @@ // ============================================================================ package routines; +import java.math.BigDecimal; + public class StringHandling { /** @@ -377,7 +379,7 @@ public class StringHandling { */ public static String SUBSTR(String string, int start, Integer length) { - if (string == null) { + if (isVacant(string)) { return null; } if (start > string.length()) { @@ -411,13 +413,13 @@ public class StringHandling { * {example} LTRIM("aatestaa","a") #testaa */ public static String LTRIM(String value, String trim_set) { - if (value == null) { + if (isVacant(value)) { return null; } int len = value.length(); int st = 0; char[] val = value.toCharArray(); - if (trim_set == null) { + if (isVacant(trim_set)) { while ((st < len) && (val[st] <= ' ')) { st++; @@ -451,12 +453,12 @@ public class StringHandling { * {example} RTRIM("aatestaa","a") #aatest */ public static String RTRIM(String value, String trim_set) { - if (value == null) { + if (isVacant(value)) { return null; } int len = value.length(); char[] val = value.toCharArray(); - if (trim_set == null) { + if (isVacant(trim_set)) { while ((0 < len) && (val[len - 1] <= ' ')) { len--; @@ -495,30 +497,31 @@ public class StringHandling { * @return * {example} LPAD("test",6,"a") #aatest */ - public static String LPADA(String first_string, int length, String second_string) { + public static String LPAD(String first_string, int length, String second_string) { - if (first_string == null || length < 1) { - return null; - } + if (isVacant(first_string) || length < 1) { + return null; + } - int OriginLength = first_string.length(); - if (OriginLength >= length) { - return first_string; - } - - if (second_string == null || "".equals(second_string)) { - for (int i = OriginLength; i < length; i++) { - first_string = " " + first_string; - } - } else { - for (int len = second_string.length(); len < length - OriginLength; second_string += second_string) - len = second_string.length(); - - first_string = second_string.substring(0, length - OriginLength) + first_string; - - } - - return first_string; + int OriginLength = first_string.length(); + if (OriginLength >= length) { + return first_string; + } + + if (isVacant(second_string)) { + StringBuilder result = new StringBuilder(first_string); + for (int i = OriginLength; i < length; i++) { + result.append(" "); + } + return result.toString(); + } else { + StringBuilder result = new StringBuilder(second_string); + for (int len = result.length(); len < length - OriginLength; result.append(second_string)) + len = result.length(); + + first_string = result.substring(0, length - OriginLength)+first_string; + return first_string; + } } /** @@ -531,26 +534,27 @@ public class StringHandling { */ public static String RPAD(String first_string, int length, String second_string) { - if (first_string == null || length < 1) { - return null; - } + if (isVacant(first_string) || length < 1) { + return null; + } - int OriginLength = first_string.length(); - if (OriginLength >= length) { - return first_string; - } - for (int i = OriginLength; i < length; i++) { - if (second_string == null) { - first_string = first_string + " "; - } else { - first_string = first_string + second_string; - if(first_string.length()>length){ - first_string = first_string.substring(0, length); - } - } - } + int OriginLength = first_string.length(); + if (OriginLength >= length) { + return first_string; + } + StringBuilder result = new StringBuilder(first_string); + for (int i = OriginLength; i < length; i++) { + if (isVacant(second_string)) { + result.append(" "); + } else { + result.append(second_string); + if(result.length()>length){ + return result.toString().substring(0, length); + } + } + } - return first_string; + return result.toString(); } public String RPAD(String first_string, int length) { @@ -574,9 +578,15 @@ public class StringHandling { */ public static Integer INSTR(String string, String search_value, Integer start, Integer occurrence) { + // linguistic string comparison. int defaultStart = 1; int defaultOccurrence = 1; + Integer result = 0; + + if (isVacant(string) || isVacant(search_value)|| Math.abs(defaultStart) >= string.length()) { + return null; + } if (start != null && start != 0) { defaultStart = start; @@ -589,13 +599,6 @@ public class StringHandling { defaultOccurrence = occurrence; } - Integer result = 0; - - // linguistic string comparison. - if (string == null || string.equals("") || search_value == null || search_value.equals("") - || Math.abs(defaultStart) >= string.length()) { - return null; - } if (defaultStart < 0) { string = string.substring(0, string.length() + defaultStart + 1); int temp = string.lastIndexOf(search_value); @@ -728,5 +731,37 @@ public class StringHandling { } return 0; } + + protected static boolean isVacant(String value) { + return value == null || "".equals(value); + } + + /** + * + * @param numeric_value + * Numeric datatype. The numeric value you want to convert to a + * string + * @return String. NULL if a value passed to the function is NULL. + * + * Converts double values to strings of up to 16 digits and provides + * accuracy up to 15 digits. If you pass a number with more than 15 + * digits, TO_CHAR rounds the number to the sixteenth digit. + * Returns decimal notation for numbers in the ranges (-1e16,-1e-16] + * and [1e-16, 1e16). TO_CHAR returns scientific notation for + * numbers outside these ranges. + */ + + public static String TO_CHAR(Object numeric_value) { + if(numeric_value==null){ + return null; + } + + BigDecimal bigDecimal = new BigDecimal(numeric_value.toString()); + if(bigDecimal.abs().compareTo(new BigDecimal("1e16"))<0&&bigDecimal.abs().compareTo(new BigDecimal("1e-16"))>0){ + return bigDecimal.toPlainString(); + } + + return numeric_value.toString(); + } } diff --git a/main/plugins/org.talend.librariesmanager/resources/java/routines/TalendDate.java b/main/plugins/org.talend.librariesmanager/resources/java/routines/TalendDate.java index 2eb9bee248..d5edd430cc 100644 --- a/main/plugins/org.talend.librariesmanager/resources/java/routines/TalendDate.java +++ b/main/plugins/org.talend.librariesmanager/resources/java/routines/TalendDate.java @@ -1279,11 +1279,11 @@ public class TalendDate { * */ public static Date TO_DATE(String string, String format) throws ParseException { - String defaultFormat = "MM/DD/yyyy HH:mm:ss.sss"; - if (string == null) { + String defaultFormat = "MM/dd/yyyy HH:mm:ss.SSS"; + if (StringHandling.isVacant(string)) { return null; } - if (format != null) { + if (!StringHandling.isVacant(format)) { if (format.equals("J")) { return new Date(Long.parseLong(string)); } @@ -1321,8 +1321,9 @@ public class TalendDate { format = format.replaceAll("DDD", "D");// Day of year (001-366, // including leap years). format = format.replaceAll("DD", "d");// Day of month (01-31). + format = format.replaceAll("HH24", "zx@i#o%l!");//protect HH24 from HH format = format.replaceAll("(HH|HH12)", "hh"); - format = format.replaceAll("HH24", "HH"); + format = format.replaceAll("zx@i#o%l!", "HH"); format = format.replaceAll("MS", "sss"); format = format.replaceAll("MI", "mm"); format = format.replaceAll("SS", "ss"); @@ -1341,7 +1342,7 @@ public class TalendDate { * {example} ADD_TO_DATE(new Date(1464576463231l), "HH",2) #Mon May 30 12:47:43 CST 2016 */ public static Date ADD_TO_DATE(Date date, String format, int amount) throws ParseException{ - if (date == null || format == null) { + if (date == null || StringHandling.isVacant(format)) { return null; } if (format != null) { @@ -1404,4 +1405,25 @@ public class TalendDate { return format; } + + /** + * + * @param date Date/Time datatype. Passes the date values you want to convert to character strings. + * @param format Enter a valid TO_CHAR format string. The format string defines the format of the return value, + * @return String. NULL if a value passed to the function is NULL. + */ + + public static String TO_CHAR(Date date, String format) { + if (date == null) { + return null; + } + if(format==null||format.equals("")){ + format="MM/DD/YYYY HH24:MI:SS"; + } + if("J".equals(format)){ + return Long.toString(date.getTime()); + } + SimpleDateFormat sdf = new SimpleDateFormat(dateFormatConvert(format)); + return sdf.format(date); + } }