Forum Discussion

Paramanand_Ghod's avatar
Paramanand_Ghod
Contributor
15 years ago

[Resolved] How to verify date format using groovy scripting?

I have a Date field in response and is of format YYYY-MM-DDT19:06:27.
How to verify this date format using groovy scripting?


Thanks,
Param

3 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    You could also use the Joda Time library (http://www.joda.org/ - download the joda-time-1.6.jar and put it in the soapui\bin\ext directory) and try to parse the string:

    import org.joda.time.DateTime
    import org.joda.time.format.ISODateTimeFormat

    def dateString = "1999-10-11T19:06:27"

    try {
    ISODateTimeFormat.dateTimeParser().parseDateTime(dateString)
    } catch (java.lang.IllegalArgumentException iae) {
    assert false
    }
    assert true
  • you don't need to use the Joda time library as the standard java one can be used it is an option though

    using the same general structure as above, define your date format then try to parse the value into that format, if the data doesn't adhere to the format the exception is thrown



    import com.eviware.soapui.support.XmlHolder
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    def dateFieldValue = context.expand( 'INSERT RESPONSE ELEMENT HERE' )
    assert dateFieldValue != null

    log.info dateFieldValue

    try {date = new Date().parse('dd/MM/yyyy', dateFieldValue)
    } catch (Exception e) {
    assert false
    }
    log.info "Expected date format = 'dd/MM/yyyy', value found = '"+dateFieldValue+"'"
    assert true