Question

How to copy a string from a text type property to a date type property

How to copy a string from a text type property to a date type property, say the string is like 2016/05/30.

I have date value (like 2016/05/30) stored in a text type property, i need to copy it to a date type property. Is there any way to do this ?

Message was edited by: Lochan to add Category

**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Correct Answer
August 8, 2016 - 12:21pm

[ This is a re-post from a post I made to an internal forum ; but I think it should help here also ]

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

 

The internal representations

 

The internal representation of a Date value is eight digits in the format YYYYMMDD, for example 20061215 for December 15, 2006.

 

The internal representation of Time of Day value is six digits in the format HHMMSS, where the hours portion ranges from 00 to 23. For example, 153025 represents 25 seconds after 3:15 P.M.

 

 

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:

 

Copies the value without examining whether it is valid.

 

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).

 

1.png

 

2.png

 

3.png

 

4.png

 

Java Steps are shown below - First Date and then Time:

 

 

try {  

    org.joda.time.LocalDate localdate=new org.joda.time.LocalDate(InDateString);  

    outDateString=localdate.toString("YYYYMMdd");  

  

}  

catch(Exception e) {  

    throw new PRRuntimeException(e);  

}  

 

 

[NOTE : See my CORRECTION note below for a slight correction to this (the ':' shouldn't be here)  - although this step apparently still worked as-is]

 

 

try {  

    org.joda.time.LocalTime localtime=new org.joda.time.LocalTime(InTimeString);  

    outTimeString = localtime.toString("HH:mm:ss");  

}  

catch(Exception e) {  

    throw new PRRuntimeException(e);  

}  

 

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.

 

5.png

 

Here's my Page after running it:

 

 

<pagedata>  

<MyInputString>2016-02-02 13:29:23</MyInputString>  

<MyTimeProp>132923</MyTimeProp>  

<pxObjClass>Tmp-scratch-Work</pxObjClass>  

<MyDateProp>20160202</MyDateProp>  

<pzStatus>valid</pzStatus>  

</pagedata> 

 

The Activity is essentially hardcoded to only accept dates exactly in one of them variations of the ISO-8601. formats. (Change the Property-Set containing the 'substring' to slice the date /time differently)

    CORRECTION:

    NOTE: I actually had overly formatted the timestring (although it still appeared to have worked...) in the second Java Step.

    The 'internal represenation' for the 'TimeOfDay' type has no separators; so the step should be in fact this:

     

        try {   
            org.joda.time.LocalTime localtime=new org.joda.time.LocalTime(InTimeString);  
            outTimeString = localtime.toString("HHmmss"); 
        }   
        catch(Exception e) {    
            throw new PRRuntimeException(e);   
        } 

     

    (Whereas it was 'HH:mm:ss')

    Comments

    Keep up to date on this post and subscribe to comments

    Pega
    May 27, 2016 - 9:28am

    Hi

    You can try using @ String.toDate(inputString)

    May 31, 2016 - 9:01am
    Response to Santanu

    Hi Santanu,

    I tried your suggestion, @ String.toDate(inputString) returned value 19700101 for string 2016/06/01, i tried changing the date (not the format) it returned the same value (19700101).

    May 27, 2016 - 9:30am

    I would probably use FormatDateTime UDF.

    May 31, 2016 - 9:21am
    Response to CONNECTS

    i don't think FormatDateTime UDF converts source strings like 2016/06/01, it converts only the standard date time string format  (like 20090109T162504.370 GMT) to a required format.

    May 27, 2016 - 2:02pm

    Hi Venkatesan,

    You can write a simple function that takes a String as input parameter and code something like this one:

    java.util.Date myDate = new java.text.SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH).parse(“2016/05/30”);

    return myDate;

    Kindly let us know if you need further help.

    Regards,

    Mahesh

    May 31, 2016 - 9:50am
    Response to Mahesh Midthuru

    tried and got the following error

    java.util.Date myDate = new java.text.SimpleDateFormat("yyyy/MM/dd",Locale.ENGLISH).parse("2016/05/30");

    ^^^^^^

    Locale cannot be resolved to a variable

    Pega
    May 31, 2016 - 10:02am
    Response to DilipanV105

    How about using parseDateTimeStamp and parseDateTimeString

         

    java.util.Date

    parseDateTimeStamp(java.lang.String strDateTime)

    Converts string form of datetimestamp to a Date object.

    java.util.Date

    parseDateTimeString(java.lang.String strDateTime)

    Converts string containing various formats of date & datetime into a Date object.

    August 8, 2016 - 10:00am

    Please check the function "pxFormatDateTime". I was successful using this however, still testing on how the converted value behaves on a report.

    Pega
    August 8, 2016 - 12:21pm

    [ This is a re-post from a post I made to an internal forum ; but I think it should help here also ]

    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

     

    The internal representations

     

    The internal representation of a Date value is eight digits in the format YYYYMMDD, for example 20061215 for December 15, 2006.

     

    The internal representation of Time of Day value is six digits in the format HHMMSS, where the hours portion ranges from 00 to 23. For example, 153025 represents 25 seconds after 3:15 P.M.

     

     

    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:

     

    Copies the value without examining whether it is valid.

     

    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).

     

    1.png

     

    2.png

     

    3.png

     

    4.png

     

    Java Steps are shown below - First Date and then Time:

     

     

    try {  

        org.joda.time.LocalDate localdate=new org.joda.time.LocalDate(InDateString);  

        outDateString=localdate.toString("YYYYMMdd");  

      

    }  

    catch(Exception e) {  

        throw new PRRuntimeException(e);  

    }  

     

     

    [NOTE : See my CORRECTION note below for a slight correction to this (the ':' shouldn't be here)  - although this step apparently still worked as-is]

     

     

    try {  

        org.joda.time.LocalTime localtime=new org.joda.time.LocalTime(InTimeString);  

        outTimeString = localtime.toString("HH:mm:ss");  

    }  

    catch(Exception e) {  

        throw new PRRuntimeException(e);  

    }  

     

    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.

     

    5.png

     

    Here's my Page after running it:

     

     

    <pagedata>  

    <MyInputString>2016-02-02 13:29:23</MyInputString>  

    <MyTimeProp>132923</MyTimeProp>  

    <pxObjClass>Tmp-scratch-Work</pxObjClass>  

    <MyDateProp>20160202</MyDateProp>  

    <pzStatus>valid</pzStatus>  

    </pagedata> 

     

    The Activity is essentially hardcoded to only accept dates exactly in one of them variations of the ISO-8601. formats. (Change the Property-Set containing the 'substring' to slice the date /time differently)

      CORRECTION:

      NOTE: I actually had overly formatted the timestring (although it still appeared to have worked...) in the second Java Step.

      The 'internal represenation' for the 'TimeOfDay' type has no separators; so the step should be in fact this:

       

          try {   
              org.joda.time.LocalTime localtime=new org.joda.time.LocalTime(InTimeString);  
              outTimeString = localtime.toString("HHmmss"); 
          }   
          catch(Exception e) {    
              throw new PRRuntimeException(e);   
          } 

       

      (Whereas it was 'HH:mm:ss')

      Pega
      August 8, 2016 - 12:23pm

      Just realized the original post is in fact also available here : https://pdn.pega.com/community/pega-product-support/question/how-map-string-date-property-data-page