Question
5
Replies
5653
Views
PEGA
Posted: February 2, 2016
Last activity: February 29, 2016
Closed
Solved
How to map a string to date property in a data page
I am trying to map to a Date Time property in a data page from XML. In the XML the date time is string in a format like the below example:
2016-02-01 13:29:23 i.e. yyyy-MM-DD HH24:mm:ss
I am trying to map this in the data transform, but keep getting empty or incorrect dates e.g. I see 19700101T000000.000 GMT on the clipboard.
I have tried using @DateTime.parseDateString(.propertry) but this dosn't seem to support this format.
The following Activity works - using Java Steps.
You might want to re-factor this into a Function (or two functions I guess) : or at least re-factor into a single Activity rather than the two I'm using here (one that using Params, the other using Pages - which calls the first one).
Additionally: I decided to split the Date and Time Components into a PRPC 'Date' Property and a PRPC 'TimeOfDay' Property - this is because these two types have simple well-defined internal-storage formats in the help:
See: (prpchost:port)://prhelp/procomhelpmain.htm#concepts/concepts2/conceptsdatetime.htm
Additionally the PRPC help page:
(prpchost:port)://prhelp/procomhelpmain.htm#designer studio/expressionbuilder/ref_conversions.htm
Has a Table showing different 'casting' operations allowed in Pega7 : and for Text->Date, and Text->TimeOfDay; the casting operation is marked as 'C'.
Which means:
So it's on us to make sure the Text Property being cast to Date/TimeOfDay is really what we need it to be.
I'm using the Joda Time Library : but this is already included in PRPC - so no need to import anything.
Joda Time has two simple types called 'LocalDate' and 'LocalTime' - which are conceptually the same as a PRPC 'Date' and a PRPC 'TimeOfDay' (They represent just a date and just a time only : no timezones to complicate things).
They also provide validation checks: including rejecting non-existant dates (2015-02-29 will fail [not a leap year], whereas 2016-02-29 is fine [is a leap year]) for instance).
Java Steps are shown below - First Date and then Time:
[NOTE : See my REPLY below for a slight correction to this (the ':' shouldn't be here) - although this step apparently still worked as-is]
I have switched on a JUMP step upon exception for both these steps - just jumps to the END of the Activity.
You might want to change this so that it 'dirties' the page rather than chuck an exception - dunno.
[But it's better that it flags the error when it happens rather than finding out later that the date is set to 1970-01-01 I think]
And here's my second testing Activity - which just calls the first: and sets Properties on a Page.
I have three Properties defined on my class here: 'MyInputString' , 'MyDateProp' and 'MyTimeProp'.
They are 'Text', 'Date' and 'TimeOfDay' respectively.
Here's my Page after running it:
The Activity is essentially hardcoded to only accept dates exactly in the format you have (an ISO-8601 format I believe).