AnsweredAssumed Answered

Converting Date format in Custom Scripting using Groovy

Question asked by 94071669 on Oct 7, 2016
Latest reply on Oct 19, 2016 by bevans988583

I got the formatting working in Groovy Console.

 

String dt = '12/01/2016';
String newDate = Date.parse('MM/dd/yyyy',dt).format("yyyy-MM-dd'T'HH:mm:ss'Z'");
println newDate;

 

But, when I put same code in Custom Scripting it doesn't work. Any ideas?? Error highlighted in bold.

Input to the script is a flat file with 2 columns:  abcstring&12/01/2016 

 

import java.io.InputStream;
import java.io.BufferedReader;
import java.util.Properties;
import java.text.SimpleDateFormat;
import java.util.Date.*;

 

String LINE_SEPARATOR = System.getProperty("line.separator");
String DELIMITER = '&';
String Name;
String oldDate;
String newDate;

String newLine;

 

for( int i = 0; i < dataContext.getDataCount(); i++ ) {

   InputStream is = dataContext.getStream(i);
   Properties props = dataContext.getProperties(i);

   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuffer outData = new StringBuffer();
   int lineNum = 0;

   while ((line = reader.readLine()) != null) {

 

      // Skip logic ifthis is the first line (assuming the file contains column headers
      if (lineNum >0) {

      // Parse the line into separate columns splitting on the delimiter defined above
      String[] columns = line.split(DELIMITER);

      // Obtain a column's value by its known position on the line (zero-indexed)
      Name= columns[0];
      oldDate = columns[1];

 

      newDate = Date.parse('MM/dd/yyyy', oldDate).format("yyyy-MM-dd'T'HH:mm:ss'Z'");
      newLine = Name + '&'+ newDate;

 

}
// Add line to output buffer including a line break at the end
outData.append(newLine);
outData.append(LINE_SEPARATOR);
lineNum++;

}

// Convert the output StringBuffer to an InputStream and store in the dataContext
is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
dataContext.storeStream(is, props);

}

Outcomes