pentaho/pentaho-kettle · error · RuntimeException

Could not apply the given format " + sArg2 + " on the…

Error message

Could not apply the given format " + sArg2 + " on the string for " + sArg1 + " : " + e.getMessage()

What it means

When str2date() is called with two arguments, the second argument is used as a SimpleDateFormat pattern. If the string cannot be parsed with that pattern, this RuntimeException is thrown, naming the format and the string plus the parser message. Common wrapped causes are ParseException and IllegalArgumentException for an invalid pattern itself.

Solutions

  1. Make the pattern exactly match the input string, including separators and component widths (e.g. 'yyyy-MM-dd HH:mm:ss').
  2. Check the wrapped message in the RuntimeException: ParseException indicates a data/pattern mismatch, IllegalArgumentException indicates a bad pattern.
  3. Use MM for months and mm for minutes; verify literal separators match the data.
  4. Trim inputs and handle null/empty in script before calling str2date.

Example fix

// before
var d = str2date('2026-09-13', 'dd/MM/yyyy');
// after
var d = str2date('2026-09-13', 'yyyy-MM-dd');
Defensive patterns

Strategy: try-catch

Validate before calling

if (s != null && String(s).trim().length > 0 && /^[yMdHms\-\/: .]+$/.test(fmt)) { var d = str2date(s, fmt); }

Try / catch

try { var d = str2date(s, fmt); } catch (e) { logError('format ' + fmt + ' failed on ' + s + ': ' + e.message); d = null; }

Prevention

When it happens

Trigger: Calling str2date('2026-09-13','dd/MM/yyyy') where the string doesn't match the pattern, or str2date(x,'bogus-pattern') where the pattern itself is invalid (IllegalArgumentException).

Common situations: Mixing up MM (month) and mm (minute); using yyyy vs yy incorrectly; passing a timestamp string with time components to a date-only pattern; null or empty string argument.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/10df20de01f1f172. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:1024

          // DecimalFormat formatter = new DecimalFormat();
          // sRC= formatter.format(sArg1);
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not apply local format for " + sArg1 + " : " + e.getMessage() );
        }
        break;
      case 2:
        try {
          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
            return null;
          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
            return undefinedValue;
          }
          sArg1 = (String) ArgList[0];
          sArg2 = (String) ArgList[1];
          Format dfFormatter = new SimpleDateFormat( sArg2 );
          oRC = dfFormatter.parseObject( sArg1 );
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not apply the given format "
            + sArg2 + " on the string for " + sArg1 + " : " + e.getMessage() );
        }
        break;
      case 3:
        try {
          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
            return null;
          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
            return undefinedValue;
          }
          sArg1 = (String) ArgList[0];
          Format dfFormatter;
          sArg2 = (String) ArgList[1];
          sArg3 = (String) ArgList[2];
          if ( sArg3.length() == 2 ) {
            Locale dfLocale = EnvUtil.createLocale( sArg3 );
            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );
            oRC = dfFormatter.parseObject( sArg1 );

View on GitHub (pinned to f3058517a1)