Convert String in java.util.Date

Hi dear members,

For using blaze, we have to declared some variable in the publishableCanonical in Object / java.util.Date format.

When we received an Idoc from SAP, on the Handler we defined a mapping service to map Data from the Idoc to the publishableDocument.

The string that we have from the IDoc structure is like : yyyyMMddhhmmss

I tried to understand the build-in service :

pub.date:dateTimeFormat , but the output is a String

So I created a Java Service to make a new transformer in order to map the String field from Idoc to the Date field of the canonical:

IDataCursor pipelineCursor = pipeline.getCursor();
String Input = IDataUtil.getString( pipelineCursor, “DateInput”);
pipelineCursor.destroy();

SimpleDateFormat df = new SimpleDateFormat (“yyyyMMddhhmmss”);
java.util.Date d= df.parse(Input);

IDataCursor pipelineCursorOut = pipeline.getCursor();
IDataUtil.put( pipelineCursorOut, “DateOutput”, d);
pipelineCursorOut.destroy();

I’m not very good in JAVA, and I’m not sure if it’s working, because when I try to save it in Developper I got the error :

unreported exception java.text.ParseException; must be caught or declared to be thrown
java.util.Date d= df.parse(Input);
^
1 error

To you have an idea, or maybe a better practices ?

Many Thanks,

Vincent

Hi, Try this… Java service

Input variable: input
Output variable: dateValue

Assumed input date pattern is yyyyMMddHHmmss… You can change it if you want…

IDataCursor pipelineCursor = pipeline.getCursor();
String input = “”;
if (pipelineCursor.first(“input”)) {
input = (String)pipelineCursor.getValue();
}
else {
throw new ServiceException(“input must be supplied!”);
}
String strDateFormat = “yyyyMMddHHmmss”;
Date startingDate = null;
SimpleDateFormat ssdf = new SimpleDateFormat(strDateFormat);
try {
startingDate = ssdf.parse(input);
}
catch (Exception e) {
throw new ServiceException(e.toString());
}
Object dateValue = new Object();
IDataUtil.put( pipelineCursor, “dateValue”, startingDate );
pipelineCursor.destroy();

Thanks Sentil !

That’s work fine. I just declare as Output dateValue in Object format and in properties panel add the Java.util.date as java wrapper type parameter.

I have some others format like yyMMdd or YYYYMMdd from SAP Idoc, I think I’ll reuse a lot your code :slight_smile:

Thanks,

Regards,
Vincent