Question
Raytheon Technologies
US
Last activity: 10 Jan 2022 6:08 EST
Convert ISO DateTime to Pega DateTime
There are other posts on this subject but they all seem closed without an answer.
Is there a function to easily convert an ISO DateTime string to a Pega DateTime? Many answers that I've seen posted say to use the FormatDateTime function. While that can be used to format a Pega DateTime to the ISO format, it does not work if you provide an ISO formatted date as input.
Here's an example of formatting a Pega DateTime as ISO:
FormatDateTime(CurrentDateTime(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "GMT", null)
Which gives output that looks like this:
2020-06-10T20:38:36.700Z
Using the Expression builder, if I enter "2020-06-10T20:38:36.700Z" as the first argument to FormatDateTime, I get a blank result.
I've also tried the parseDateTimeStamp but that gives me an error message in the expression builder.
Right now we're getting past this using a series of replaceAll functions but it really seems like there should be a better way to do this.
This is a problem every time we get date values back when calling REST services.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Blue Rose Technologies
DE
Hi Michael,
You can create a new function for this using below code.
org.joda.time.format.DateTimeFormatter fmt = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
org.joda.time.DateTime DatetimeVal = fmt.parseDateTime(ISODateVal);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS 'GMT'");
String stringDateTime = formatter.format(DatetimeVal.toDate());
return stringDateTime;
Please see testing screenshot below with input in ISO format.
-
Sam mk Balakrishna G
Raytheon Technologies
US
Hi Shyju,
Thanks for the code for this function. I wanted to make sure there wasn't something already out of the box before I created my own.
If you look closely at your test result, there's a bug in your code. The input you tested with, 2020-06-10T20:38:36.700Z, has 'Z' for the time zone which means UTC (or GMT). And we want the output to also be UTC / GMT, but the hour value in your result is different from the input. In your code it seems that the date is being converted as if the input was in your local time zone rather than UTC / GMT.
I updated your code to fix this bug and also to use only one datetime library rather than 2 different ones.
Here's the code change:
org.joda.time.format.DateTimeFormatter fmt = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
org.joda.time.DateTime DatetimeVal = fmt.parseDateTime(ISODateVal);
fmt = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss.SSS 'GMT'");
String stringDateTime = fmt.print(DatetimeVal.withZone(org.joda.time.DateTimeZone.UTC));
return stringDateTime;
This will work if the timezone for the input value is UTC using 'Z', +0000 or -0000. It also works to convert other time zones formatted with no colons (i.e. -0500) to UTC / GMT.
-
Sigang Hu
Raytheon Technologies
US
After looking at this some more, I decided to use java.time instead of org.joda.time. I've also created a function that uses "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" as the default pattern but can take other patterns as a parameter.
Here's the code:
if (formatPattern == null || formatPattern.isEmpty()){
formatPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
}
java.time.format.DateTimeFormatter fmt = java.time.format.DateTimeFormatter.ofPattern(formatPattern);
java.time.ZonedDateTime zdt = java.time.ZonedDateTime.parse(dateTimeVal, fmt);
fmt = java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss.SSS 'GMT'");
return java.time.ZonedDateTime.ofInstant(zdt.toInstant(), java.time.ZoneId.of("UTC")).format(fmt);
-
Sigang Hu
Blue Rose Technologies
DE
That's great Michael. We have a better solution now which will help many. My code was not done/tested as part of any application I worked. I just tried and did a basic testing after I saw this question yesterday and found that there was no solutions in other posts.
Raytheon Technologies
US
After posting this I found an OOTB function called pxConvertISODateTimeToPegaInternalFormat. It takes a Page as input instead of a single string value. I haven't tested it yet and the documentation is not clear to me. I'm wondering if it finds and converts every value that looks like an ISO formatted date in the page? If so, that could be useful.
Updated: 9 Jul 2020 2:23 EDT
Pegasystems Inc.
AU
Michael, I can confirm that this is the case. It's will convert any ISO Date and Date Time format on the given page into the Pega internal Date and DateTime format. What it doesn't convert is a TimeOfDay format, ie time format only.
-
Michael Fede Pawel Tyl
Raytheon Technologies
US
In that case, seems like it's a good option to convert dates coming back in response payloads for REST connectors all in one shot. It would be nice to have detailed documentation on how it works and exactly what format is expected. I've seen very small format differences (like weather or not there's a colon in the timezone offset) cause problems for date time parsing code. Since this operates on a Page, it's more complex to "unit test" and ensure it's going to work for the format we are dealing with.
Updated: 6 Aug 2020 2:11 EDT
Accenture
AU
Create a custom function using EngineAPI provided functions.
Your custom function can have the parameter "ISODateTime" as the ISO format.
Your Java source can only have one line:
return PRDateFormat.formatInternalDateTime(PRDateFormat.parseXSDDateTime(ISODateTime));
That should do it.
For future reference, what PRDateFormat calls "XSD Date/Time format" is synonymous to ISO8601 formats.
-
Herbert Theva Ravi M
Updated: 4 May 2021 11:37 EDT
COTIVITI
IN
Hi All, We can achieve this ISO to Pega internal DateTime conversion with just a simple & OOTB code below. in your Data Transform or Activity use the below code. (DTProperty is DateTime Property, DTValue is a text type parameter) Say Param.DTValue has ISO datetime value. Param.DTValue = @replaceAll(Param.DTValue,"-","") Param.DTValue = @replaceAll(Param.DTValue,":","") Param.DTProperty = @replaceAll(Param.DTValue,"Z"," GMT") -> ISO datetime format is standard and never changes, no worries with code updates. I used this DateTime property for Wait shape, it worked perfect. Cheers...
-
Chandramouli Kommavarapu
Accenture
SG
Use this function provided ootb on steppage context, should do the needful
@(Pega-RULES:Page).pxConvertISODateTimeToPegaInternalFormat(tools, myStepPage)
-
Pawel Tyl