Spectrum Technology Platform

Expand all | Collapse all

Convert date from String to Datetime using script

  • 1.  Convert date from String to Datetime using script

    Posted 4 days ago
    Hello Community

    In an Enterprise designer dataflow, I am try to convert a String date : 2019-10-07T00:00:00 to a datetime format
    I use a transformer stage in which I have set a script with this
    treatment :

    data['CREATIONDATE_datetime'] = stringToDate(data['Root.GoldenCustomer.CREATIONDATE'] , "yyyy-MM-dd'T'hh:mm:ss");

    But when running the dataflow in inspection mode I have this error :

    Exception: CANDIDATE_QUERY_odu2-Transformer threw StageException: com.g1.dcg.server.dataflow.stages.transformer.InvalidTransformDefinitionException: 'Custom Transform (Custom Transform CreationDate)' failed: No signature of method: static com.g1.dcg.server.dataflow.stages.transformer.customfunction.impl.ExtendedDateTimeFunctions.stringToDate() is applicable for argument types: (com.pb.spectrum.api.datetime.DateTime, java.lang.String) values: [2019-10-07T00:00:00, yyyy-MM-dd'T'hh:mm:ss] Possible solutions: stringToDate(java.lang.String, java.lang.String), stringToTime(java.lang.String, java.lang.String), stringToDateTime(java.lang.String, java.lang.String)

    Any idea what would be a good convert function and syntax ?

    thanks in advance

    ------------------------------
    Giang Pham-Dac
    Data Manager
    L'Occitane International SA
    Plan-Les-Ouates
    ------------------------------


  • 2.  RE: Convert date from String to Datetime using script

    Pitney Bowes
    Posted 3 days ago
    Hello,

    It seems the field "Root.GoldenCustomer.CREATIONDATE" is of type Datetime instead of String. You can try getting string value before using the stringToDate function. Something like below:

    if (data['Root.GoldenCustomer.CREATIONDATE'] != null) {
     String creationDateStr = data['Root.GoldenCustomer.CREATIONDATE'].toString();
     data['CREATIONDATE_datetime'] = stringToDate(creationDateStr, "yyyy-MM-dd'T'hh:mm:ss");
    }


    Let us know if it works.

    Thanks,
    Pankaj Dwivedi



    ------------------------------
    - Pankaj.dwivedi@pb.com
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 3.  RE: Convert date from String to Datetime using script

    Pitney Bowes
    Posted 3 days ago
      |   view attached
    Below two functions can be used in ED

    data['date_Val1'] = getDateFromString('2019-10-07T00:00:00', "yyyy-MM-dd'T'HH:mm:ss");
    data['date_Val2'] = dateFromString('2019-10-07T00:00:00', "yyyy-MM-dd'T'HH:mm:ss");

    Second function gives time in local timezone. Server is in 'Indian standard time' (i.e is GMT+5:30) hence it is converting
    '2019-10-07T00:00:00' to '10/6/19 6:30 PM'

    'date_Val1' and 'date_Val2' are 'datetime' type output fields created. See screenshot attached.

    Thanks
    Gunjan

    ------------------------------
    Gunjan Chaudhary
    Pitney Bowes Software India
    Noida
    ------------------------------



  • 4.  RE: Convert date from String to Datetime using script

    Posted 3 days ago
    I recently had the same question, but I was not interested in the time. Using 

    def arrDate = Date.parse("yyyyMMdd", arrStr)
    where arrStr is a string like 20200324.
    This worked fine for me.



    ------------------------------
    Adrie vlutters
    Consultant
    Document Dialog B.V.
    Bunnik
    ------------------------------