Forum Discussion

CByler's avatar
CByler
Contributor
4 years ago
Solved

Java code snippet no longer working to create dates in 3.3.2, worked fine in 3.3.1 and prior builds

I have been using the following custom property for years to create dates which I then pass for my insertion of transactions in my rest web service calls.  The below example has always created the date of tomorrow in "effectiveEntryDate":09/14/2020 format.

 

${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1);cal.getTime().toLocaleDateString()}

 

It worked fine in 3.3.1 but once I upgraded to 3.3.2, it now returns an error:

 

"effectiveEntryDate":"No signature of method: java.util.Date.format() is applicable for argument types: (String) values: [MM/dd/yyyy]
Possible solutions: from(java.time.Instant), toYear(), stream(), getAt(java.lang.String), parse(java.lang.String), print(java.io.PrintWriter)"

 

Is it possible I need to upgrade the java for ReadyAPI, etc? Any help would be greatly appreciated!

  • CByler's avatar
    CByler
    4 years ago

    The custom property worked perfectly! Let me ask one more thing. With the old java, I could modify the dates such as:

     

    Same Day
    ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.getTime().format("MM/dd/yyyy")}

    Future Date
    ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1000);cal.getTime().format("MM/dd/yyyy")}

     

    Is that possible using the SimpleDate snippets?

  • 1) Future date custom property with Local Date class: 

    ${= import java.time.LocalDate; LocalDate.now().plusDays(1).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))}

    or

    ${=java.time.LocalDateTime.now().plusDays(1).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))}

     

    2) Future Date in groovy with Simple Date Format:

    import groovy.time.TimeCategory
    import java.text.SimpleDateFormat

     

    use(TimeCategory) {
    def time = new Date()+ 1.days
    date = new SimpleDateFormat ("MM/dd/yyyy").format(time)

    }

4 Replies

  • PrathapR's avatar
    PrathapR
    Frequent Contributor

    Hi CByler 

     

    Can you please try using 

    For custom property:

    ${=import java.text.SimpleDateFormat ; new SimpleDateFormat("yyyy-MM-dd").format(new Date())}

    You can update "yyyy-MM-dd" with your requirement(EX: yyyy-MM-dd'T'HH:mm:ss). 

     

    For Groovy Script:

    import java.text.SimpleDateFormat

    def currentTime = new SimpleDateFormat ("yyyy-MM-dd").format(new Date())

     

    Thanks!

    • CByler's avatar
      CByler
      Contributor

      The custom property worked perfectly! Let me ask one more thing. With the old java, I could modify the dates such as:

       

      Same Day
      ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.getTime().format("MM/dd/yyyy")}

      Future Date
      ${=import javax.xml.datatype.DatatypeFactory; def cal = Calendar.instance;cal.add(Calendar.DATE, 1000);cal.getTime().format("MM/dd/yyyy")}

       

      Is that possible using the SimpleDate snippets?

      • PrathapR's avatar
        PrathapR
        Frequent Contributor

        1) Future date custom property with Local Date class: 

        ${= import java.time.LocalDate; LocalDate.now().plusDays(1).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))}

        or

        ${=java.time.LocalDateTime.now().plusDays(1).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))}

         

        2) Future Date in groovy with Simple Date Format:

        import groovy.time.TimeCategory
        import java.text.SimpleDateFormat

         

        use(TimeCategory) {
        def time = new Date()+ 1.days
        date = new SimpleDateFormat ("MM/dd/yyyy").format(time)

        }