1

I am trying to convert soapui property value (e.g. : 2017/04/17 02:00:00) into milliseconds. I have to store tomorrow custom Date-time in milliseconds format into soapui property. As of now I am able to store date with custom time into property. Now need to convert it into milliseconds.

def date = new Date(); 
def nextDate = date + 1
tomorrow = nextDate.format("yyyy/MM/dd");
log.info tomorrow
def setTomorrow = testCase.testSuite.project.setPropertyValue("Date", tomorrow + ' 02:00:00' );

long millisecond = setTomorrow.getTime();
log.info millisecond

Error :

Cannot invoke method getTime() on null object

Please Help.

Rao
  • 20,781
  • 11
  • 57
  • 77
rAJ
  • 1,295
  • 5
  • 31
  • 66
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Brian Agnew Apr 16 '17 at 18:09

1 Answers1

1

If you want tomorrow's time in millis:

def date = new Date() + 1
log.info "Tomorrow's time in millis : ${date.time}"
//Set it into project property
context.testCase.testSuite.project.setPropertyValue('DATE_TIME', date.time.toString())

Hope you know how to access the above DATE_TIME project property using property expansion i.e., ${#Project#DATE_TIME}

If you want particular date string to millis, then use below:

def date2 = Date.parse('2017/04/17 02:00:00')
log.info date2

EDIT: Based on OP's comments, updated script below -

//Tomorrow date
def d = (new Date() +1).format('yyyy/MM/dd')
use(groovy.time.TimeCategory) {
    //Add fixed hours i.e., 2
    def tomorrow2hours = new Date(d) + 2.hours
    log.info "Tomorrow @ 2 hrs : ${tomorrow2hours}"
    log.info "In millis : ${tomorrow2hours.time}"
    context.testCase.testSuite.project.setPropertyValue('DATE_TIME', tomorrow2hours.time.toString())
}

EDIT2: Based on OPs comment that incorrect milliseconds which is false from below code - try adding below two after statement context... and see.

def dd = new Date(tomorrow2hours.time)
log.info dd.format('yyyy-MM-dd HH:mm:ss')
Rao
  • 20,781
  • 11
  • 57
  • 77
  • `def DateMilli = Date.parse(tomorrow + ' 02:00:00') def setTomorrow = testCase.testSuite.project.setPropertyValue("Date", DateMilli );` **Error : No signature of method: com.eviware.soapui.impl.wsdl.WsdlProject.setPropertyValue() is applicable for argument types: (java.lang.String, java.lang.Long) values: [Date, 1492461000000] Possible solutions: setPropertyValue(java.lang.String, java.lang.String), getPropertyValue(java.lang.String)** – rAJ Apr 17 '17 at 06:29
  • Why don't you try the provided solution as it is before modifying – Rao Apr 17 '17 at 06:57
  • Please understand my requirement. I want time should be hard code and date should be tomorrow. In first approach there is no custom time and in second approach there is hard code date. – rAJ Apr 17 '17 at 13:00
  • @rAJ, well that should have been part of your question. Any ways, please check the updated answer(edit part). – Rao Apr 17 '17 at 13:44
  • yes my mistake. Now the Tomorrow time is correctly calculated `Tomorrow @ 2 hrs : Tue Apr 18 02:00:00 IST 2017` but the millisecond is still incorrect `In millis : 1492461000000`...It is calculated for `Mon Apr 17 2017 20:30:00` – rAJ Apr 17 '17 at 14:28
  • Your last point the previous comment is not true. See the update - translating long to formatted string to show that milli seconds is correct unlike you said. – Rao Apr 17 '17 at 19:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141929/discussion-between-raj-and-rao). – rAJ Apr 18 '17 at 04:27
  • @rAJ, have you tried the given solution from the edits? You do not seem to be active in the chat. – Rao Apr 18 '17 at 08:59
  • @rAJ, if you want to add 2 hr 15 min, just do `def tomorrow2hours15min = new Date(d) + 2.hours + 15.minutes` – Rao Apr 20 '17 at 03:50