Question
Asurion
US
Last activity: 2 Feb 2016 7:09 EST
Unable to convert datetime to yyyy-MM-dd
Hi ,
Is there a function to convert the date format to yyyy-MM-dd and return a date object. I followed the steps in article SA-14171 but it returning me date in this format yyyyMMdd. I'm able to write a function which formats and returns the date as string. but not able to return the date as date object in this format(yyyy-MM-dd)
Thanks
Nagendran A
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Pegasystems Inc.
DE
As Paul already said above, Date does not have a format!
I assume that your Startdate property has the type java.util.Date, and that is binary representation of a Time/Date value without a time zone. It can be translated into a String - given the right function is used with the right arguments, that String may have the format "yyyy-MM-dd" as defined by ISO 8601. But giving that String (of type java.lang.String) to a function that takes an argument of type java.util.Date will cause the exception you mention above - as a java.lang.String is by no means a java.util.Date, no matter what content that String has, and if it could be interpreted as a Time/Date value or not.
Using java.util.Date for date-only properties may cause other problems as well as they always have a time component, too. Although the instance as such do not have a time zone, it will be interpreted under some conditions as for the current time zone - what can result in shifting of the resulting day.
Pegasystems Inc.
US
Date objects have no format. To get a formatted string with "-", you have to convert to String.
Updated: 26 Jan 2016 13:19 EST
Pegasystems Inc.
GB
Agreed: the 'DATE' type is defined in the help as :
(this happens to be from 63SP1):
Date For a date with no time. The internal representation follows the ISO 8601 standard and does not indicate a time zone. The internal format is YYYYMMDD, for example 20071125.
So DATE types have no time-component, no time-zone and no separators in their literal form.
(constrast with the more complex 'DATETIME' type).
Are you asking how to format a Date Property in a particular way ?
Or are you trying to create a Date Property instance given a string ?
If the second example: you can actually use a Property-Set from a String; so long as you keep the format as 'YYYYMMDD'.
From the help again (search for 'cast') ; the converstion from text->date is possible; but it is marked as category 'C' casting: which means:
Copies the value without examining whether it is valid.
Which means you should perform some sanity-checks on your input before casting to a DATE; otherwise you might end up with invalid (or at least unexpected) DATE values.....
If your input string is (say) in this format 'YYYY-MM-DD' : you can just drop the '-' and Property-Set to a DATE type using the Expression:
Agreed: the 'DATE' type is defined in the help as :
(this happens to be from 63SP1):
Date For a date with no time. The internal representation follows the ISO 8601 standard and does not indicate a time zone. The internal format is YYYYMMDD, for example 20071125.
So DATE types have no time-component, no time-zone and no separators in their literal form.
(constrast with the more complex 'DATETIME' type).
Are you asking how to format a Date Property in a particular way ?
Or are you trying to create a Date Property instance given a string ?
If the second example: you can actually use a Property-Set from a String; so long as you keep the format as 'YYYYMMDD'.
From the help again (search for 'cast') ; the converstion from text->date is possible; but it is marked as category 'C' casting: which means:
Copies the value without examining whether it is valid.
Which means you should perform some sanity-checks on your input before casting to a DATE; otherwise you might end up with invalid (or at least unexpected) DATE values.....
If your input string is (say) in this format 'YYYY-MM-DD' : you can just drop the '-' and Property-Set to a DATE type using the Expression:
Property-Set MyDateProperty = @(Pega-RULES:String).replaceAll(.MyDateString, "-", "")
But do make sure your 'MyDateString' is valid before doing this: otherwise you'll initialize an a default value for the DATE property (1970,01,01).
Thanks,
John
Asurion
US
Hi John ,
we have a property Startdate of type Date , it will have the value in yyyyMMdd format , we are using it in a rest service where we send the Startdate in response , the client is expecting the date to be in this format yyyy-MM-dd. I'm able to convert to this format yyyy-MM-dd using function , but i'm able to return only string. When I set a string to date property (Startdate) I get this exception "2015-11-26" is not a date. is there any way we can return a date in this format (yyyy-MM-dd) so that I can set it to property of type date.
Accepted Solution
Pegasystems Inc.
DE
As Paul already said above, Date does not have a format!
I assume that your Startdate property has the type java.util.Date, and that is binary representation of a Time/Date value without a time zone. It can be translated into a String - given the right function is used with the right arguments, that String may have the format "yyyy-MM-dd" as defined by ISO 8601. But giving that String (of type java.lang.String) to a function that takes an argument of type java.util.Date will cause the exception you mention above - as a java.lang.String is by no means a java.util.Date, no matter what content that String has, and if it could be interpreted as a Time/Date value or not.
Using java.util.Date for date-only properties may cause other problems as well as they always have a time component, too. Although the instance as such do not have a time zone, it will be interpreted under some conditions as for the current time zone - what can result in shifting of the resulting day.