Forum Discussion

hutabarat2014's avatar
hutabarat2014
Contributor
10 years ago

Evaluate a Function in Groovy Script

Hi All,

I tried the following script:

def ABC = new java.lang.String.format('SU%tY%<tm%<td%<tH%<tM%<tS%<tL', new Date())
testRunner ABC
log.info ABC


But, I got the following error message. Please advise.

Many thanks,
Leo

4 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    Please check the syntax. That does not appear to be correct as "java.lang.String.format" is not a constructor.

    Regards,
    SmartBear Support
  • Hi SmartBear,

    Would you please tell me what is the right constructor?

    Many thanks,
    Leo
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Leo,

    I believe it should be String, but as this is a Java question, I suggest you search the Java API for the correct syntax.

    Regards,

    Giscard
    SmartBear Support
  • nmrao's avatar
    nmrao
    Champion Level 3
    Is this the output you are looking for?
    SU20140730094629693


    Here is the script

    StringBuilder sb = new StringBuilder()
    Formatter formatter = new Formatter(sb, Locale.US)
    log.info formatter.format("SU%tY%<tm%<td%<tH%<tM%<tS%<tL", new Date())


    You may also want the value to a variable, just add below statement.
    def formattedString = sb.toString()