{"record":{"id":"76f181744a5179fa","repo":"pentaho/pentaho-kettle","slug":"to-date-couldn-t-convert-string-to-date-stringutil","errorCode":null,"errorMessage":"TO_DATE Couldn't convert String to Date ","messagePattern":"TO_DATE Couldn't convert String to Date ","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/util/StringUtil.java","lineNumber":486,"sourceCode":"  public static final boolean isEmpty( StringBuilder string ) {\n    return string == null || string.length() == 0;\n  }\n\n  public static Date str2dat( String arg0, String arg1, String val ) throws KettleValueException {\n    SimpleDateFormat df = new SimpleDateFormat();\n\n    DateFormatSymbols dfs = new DateFormatSymbols();\n    if ( arg1 != null ) {\n      dfs.setLocalPatternChars( arg1 );\n    }\n    if ( arg0 != null ) {\n      df.applyPattern( arg0 );\n    }\n\n    try {\n      return df.parse( val );\n    } catch ( Exception e ) {\n      throw new KettleValueException( \"TO_DATE Couldn't convert String to Date \" + e.toString() );\n    }\n  }\n\n  public static String getIndent( int indentLevel ) {\n    return INDENTCHARS.substring( 0, indentLevel );\n  }\n\n  /**\n   * Giving back a date/time string in the format following the rule from the most to the least significant\n   *\n   * @param date\n   *          the date to convert\n   * @return a string in the form yyyddMM_hhmmss\n   */\n  public static String getFormattedDateTime( Date date ) {\n    return getFormattedDateTime( date, false );\n  }\n","sourceCodeStart":468,"sourceCodeEnd":504,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/util/StringUtil.java#L468-L504","documentation":"StringUtil.str2dat converts a string to a java.util.Date using a SimpleDateFormat with the given pattern; if df.parse fails (unparseable or mismatched format), it wraps the exception in a KettleValueException with message \"TO_DATE Couldn't convert String to Date \" plus e.toString(). This mirrors the SQL TO_DATE function semantics used in Kettle formula/expression evaluation.","triggerScenarios":"Calling StringUtil.str2dat(value, format) where value does not match the pattern arg0 (wrong date format string, localized text, extra whitespace, or a completely non-date string).","commonSituations":"CSV or database extract delivering dates as 'dd/MM/yyyy' while the pattern is 'yyyy-MM-dd'; locale-dependent month names (e.g. 'Jan' vs 'Januar'); empty or null-ish strings passed to TO_DATE in expressions.","solutions":["Make the input format match the pattern passed to str2dat/TO_DATE exactly (check day/month order, separators, and time components).","Log/inspect the offending value and fix the upstream source to emit a consistent format.","Use lenient parsing or pre-clean the string (trim, remove time zones, map month names) before conversion.","Handle null/empty inputs before calling TO_DATE."],"exampleFix":"// before\nDate d = StringUtil.str2dat(\"13/09/2026\", \"yyyy-MM-dd\");\n// after\nDate d = StringUtil.str2dat(\"13/09/2026\", \"dd/MM/yyyy\");","handlingStrategy":"validation","validationCode":"// validate the string parses before str2dat\nSimpleDateFormat probe = new SimpleDateFormat(format);\nprobe.setLenient(false);\nprobe.parse(value); // throws ParseException early if bad","typeGuard":null,"tryCatchPattern":"try {\n  Date d = StringUtil.str2dat(value, format);\n} catch (KettleValueException e) {\n  log.error(\"Unparseable date value: \" + value + \" with pattern \" + format);\n  d = null; // or route the row to an error stream\n}","preventionTips":["Agree on a single ISO-8601 date format for all upstream feeds.","Always call setLenient(false) in your own probes to catch format drift.","Route unparseable rows to error handling instead of failing the transformation."],"tags":["date","parsing","format-mismatch"],"backgroundTag":"invalid-date-format","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}