on 01-13-2023 11:26 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.