Posted: 17 Aug 2020 10:20 EDT Last activity: 18 Aug 2020 19:56 EDT
How to convert String to Date format
I have a requirement where I'm getting a value from response in string format i.e "15 Jul 2020". I wanted to map this value to a date property but When i map It's throwing an error saying" String can't be mapped to Date".
So need a function of format where i can map text to date property.
I had a look through the Functions associated with the DateTime Library and there doesn't appear to be one that does what you need. You may find a combination to chain together that appears to do what you need, however as I expect you want to keep timestamps and timezones out of it, I would caution against the OTB Pega DateTime Functions for date-only conversion as most do some sort of conversion to a timestamp, which risks a timezone conversion changing your date portion back a day if you are running in a timezone east of GMT.
For a few lines of Java, you may be better off implementing your own Function. If you are on a Java 8 (or higher) JVM, the new java.time.LocalDate and java.time.format.DateTimeFormatter classes are ideal for doing this conversion without involving times and timezones.
If you had:
An inputDate String parameter containing your date (e.g. "15 Jul 2020"), and
An inputFormat String parameter containing the DateTimeFormatter-compatible format you expect it to be in
(e.g. "d MMM yyyy", you may need "dd MMM yyyy" if the day portion is zero-padded when less than 10)
The Function would be something like:
DateTimeFormatter INPUT_FORMAT = DateTimeFormatter.ofPattern(inputFormat);
DateTimeFormatter PEGA_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");
return LocalDate.parse(inputDate, INPUT_FORMAT).format(PEGA_FORMAT);
// needs some try/catch handling of the parse/format exceptions
I would expect the String value returned could be stored in a "Date only" property
Posted: 8 months ago
Updated: 8 months ago
Posted: 18 Aug 2020 1:40 EDT Updated: 18 Aug 2020 4:27 EDT
This approach assumes that the java.util.Date constructor can interpret your input date. It obviously does in the format you are currently using.
If you anticipate needing to be able to convert dates in other formats that java.util.Date may not natively recognize, then this either throws an exception - or worse - misinterprets the input format and puts the day and the month the wrong way around. For example:
This likely won't throw an exception, but depending on the JVM configuration this will be interpreted as one of January 2 or February 1.
I recommend you design this in a way that also takes the date format of the input date as a parameter so that the caller can be explicit about what format the input date is in.
There is also a risk that using java.util.Date - which ultimately tracks a date & time value specific to the millisecond (even if you only give it a date as input) - will undergo a timezone conversion that you don't expect. If you are on Java 8 or higher, I recommend using LocalDate for date-only conversions that do not involve a time component, which eliminates this risk.