cancel
Showing results for 
Search instead for 
Did you mean: 

[SAP CPI]: Groovy date parsing issue

babruvahana
Contributor
0 Kudos

Hi Experts,

My requirement is to send the date from yyyy-MM-dd'T'HH:mm:ss.SSSXXX to yyyyMMddHHmmssSSS.

But sometimes, the data is received without milliseconds, due to this my groovy script is failing with an unparsable date issue.

Works when the date is in 2022-11-30T11:45:53.243+01:00 format

Fails when the date is in 2022-11-30T11:45:53+01:00 format

import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.*
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
def Message processData(Message message) {

    String mod_time = message.getProperty("PL_ModificationTime");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    Date MOD = sdf.parse(mod_time);

    message.setProperty("ModificationTime", MOD.format("yyyyMMddHHmmssSSS"));

	return message;
}

Any leads to fix this issue in the below groovy script will be really helpful.

Regards,

Pavan

Accepted Solutions (1)

Accepted Solutions (1)

karthikarjun
Active Contributor

Hi babruvahana - Would you have a look at the below code to fix your problem?

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;

//String mod_time = message.getProperty("PL_ModificationTime");
String mod_time = "2022-11-30T11:45:53+01:00"; 
List<SimpleDateFormat> knownPatterns = new ArrayList<SimpleDateFormat>();
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));


for (SimpleDateFormat pattern : knownPatterns) {
    try {
        // Take a try
        Date modify = pattern.parse(mod_time)
        String out = modify.format("yyyyMMddHHmmssSSS")
        message.setProperty("ModificationTime", out );

    } catch (ParseException pe) {
        // Loop on
    }
}

Input 1: 2022-11-30T11:45:53+01:00
Output 1: 20221130104553000
Input 2: 2022-11-30T11:45:53.243+01:00
Output 2: 20221130104553243

Regards,
Karthik Arjun
SAP CPI | Fiori Senior Specialist 

Answers (2)

Answers (2)

PiotrRadzki
Active Participant

Hi Pavan,

you could assign date to string variable check the length and in case it's shorter than expected you can concatenate with fixed milliseconds and time zone (.000+01:00 ).

BR, Piotr

ibibhu
Product and Topic Expert
Product and Topic Expert

Not formatted in CPI format i hope you can make it modular 😛

import java.text.SimpleDateFormat
import java.text.ParseException

// String dateString = "2022-11-30T11:45:53+01:00" 
String dateString = "2022-11-30T11:45:53.243+01:00"

SimpleDateFormat inputFormatType1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
SimpleDateFormat inputFormatMain = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

try {
    // Parse the main format "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
    date = inputFormatMain.parse(dateString)
    output = outputFormat.format(date)
} catch (ParseException e) {
    try {
         // if fails check for "yyyy-MM-dd'T'HH:mm:ssXXX"
        inputFormatType1.parse(dateString)
        // Add the missing 000
        dateString = dateString.replaceAll("(?<=:\\d{2})(?=\\+\\d{2}:\\d{2})", ".000")
        date = inputFormatMain.parse(dateString)
        output = outputFormat.format(date)
    } catch (ParseException ex) {
        println ex
    }
}

println output



To correct the date string

This statement uses the replaceAll() method with a regular expression to match the pattern where there is no milliseconds after the seconds and before the time offset.

The regular expression is made up of two parts:

  • (?<=:\\d{2}): This is a positive lookbehind. It asserts that the pattern that follows must be preceded by a colon and two digits (seconds).
  • (?=\\+\\d{2}:\\d{2}): This is a positive lookahead. It asserts that the pattern that precedes must be followed by a plus sign, two digits, a colon and two digits (time offset).

So, this regular expression matches the pattern which is after the seconds and before the time offset, and replace it with ".000"

This way you can correct the date string and add the milliseconds "000" to it.

Please note that this modification will not change the date, but it will change the format of the date string.