I am trying to convert milliseconds (e.g : 1503478800000) to yyyy-MM-ddTHH:mm:ss.SSS'Z' (e.g : 2017-08-23T09:00:000Z) date-time format.
Milliseconds value stored in the Soapui Global variable.
def testCase = messageExchange.modelItem.testCase;
def NewDateTime = testCase.testSuite.project.getPropertyValue("StartDateTime").toInteger();
log.info NewDateTime.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Error popup display :-
For input string: "1503478800000"
With Groovy you can do it with Date.format(String format) method, e.g.
def millis = testCase.testSuite.project.getPropertyValue("StartDateTime").toLong()
log.info new Date(millis).format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
Related
I'm trying to transform my JSON dates using Nifi. They are imported in this format:
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
def ff = session.get()
if(!ff)return
ff = session.write(ff, {rawIn, rawOut->
// transform streams into reader and writer
rawIn.withReader("UTF-8"){reader->
rawOut.withWriter("UTF-8"){writer->
//parse reader into Map
def json = new JsonSlurper().parse(reader)
// set my variable and define what format it is in
json.date = new Date(json.date as Long).format('HH:mm yyyy-MM-dd')
// Reformat it
json.date = DateFormat.parse("yyyy-MM-dd HH:mm", json.date)
//write changed object to writer
new JsonBuilder(json).writeTo(writer)
}
}
} as StreamCallback)
session.transfer(ff, REL_SUCCESS)
The incoming flowfile has this body:
[{"date":"09:00 2019-05-29","data":460.0,"name":"login"},{"date":"10:00 2019-05-29","data":548.0,"name":"login"},{"date":"11:00 2019-05-14","data":0.0,"name":"login"},{"date":"00:00 2019-06-15","data":0.0,"name":"login"}]
I want this output:
[{"date":"2019-05-29 09:00","data":460.0,"name":"login"},{"date":"2019-05-29 10:00","data":548.0,"name":"login"},{"date":"2019-05-14 11:00","data":0.0,"name":"login"},{"date":"2019-06-15 00:00","data":0.0,"name":"login"}]
The error I get is this:
Can anyone please help me understand where I am going wrong?
The input is a list of the objects in question. The incoming date is a
String -- not a Long.
So the first error is to use json.date as it implies json*.date
(which gives a list of all date).
Next casting the date to Long, create a new Date and then format it is
the wrong way around.
So to change the format of all the date something like this is needed:
json.each{
it.date = Date.parse('HH:mm yyyy-MM-dd', it.date).format('yyyy-MM-dd HH:mm')
}
I have date strings being created with different timezones, and I need to display them in a different format, but still showing each one's original timezone.
I'm able to parse, but it parses to a unix timestamp, which then loses the original timezone.
def dateCreated = issue.fields.created
// 2018-12-21T10:20:00.483-0800
def dateParsed = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSz", dateCreated)
// 1545416400483
def dateFormatted = dateParsed.format('yyyy-MM-dd h:mm a, z')
// 2018-12-21 6:20 PM, UTC
Is there a way to parse/format straight to the desired format without losing the timezone in the middle?
in java8 there are new classes to parse/format datetime:
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
def dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
ZonedDateTime zdt = ZonedDateTime.parse('2018-12-21T10:20:00.483-0800',dtf)
println zdt.getZone()
and starting from groovy 2.5 this code could be minimized to
ZonedDateTime zdt = ZonedDateTime.parse('2018-12-21T10:20:00.483-0800',"yyyy-MM-dd'T'HH:mm:ss.SSSX")
println zdt.getZone()
Okay, I realized that this wouldn't actually get me what I want. The original string uses -0800, which is an offset, not a timezone. So I looked elsewhere in the API response and found the timezone of the person who created the issue. I used this to format the date properly.
Final code:
def dateCreated = issue.fields.created
// 2018-12-21T10:20:00.483-0800
def creatorTimeZone = issue.fields.creator.timeZone
// America/Los_Angeles
def dateParsed = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSz", dateCreated)
// 1545416400483
def dateFormatted = dateParsed.format('yyyy-MM-dd h:mm a, z', TimeZone.getTimeZone(creatorTimeZone))
// 2018-12-21 10:20 AM, PST
In SoapUI, I am grabbing a testcase property that displays the value as 47.79.
def test= messageExchange.modelItem.testStep.testCase.getPropertyValue('test')
I want to grab this variable value and create two new variables. One will add up to a pence so that the value would be 47.80, and another that will subtract a pence so that it's 47.78.
I am unsure how to implement this in groovy as they tend to append at the end or give me error. How is it written in groovy to do the above?
Below is my attempt:
def testUp= Double.Parse(test) + 0.01
def testDown= Double.Parse(test) - 0.01
Thanks
Assuming that there a test case level property, say, test is defined with value 47.79 as stated in the question.
The mistake in your script is Double.Parse()
Here is the script it can do what you are looking for:
//Get property value and convert it as Double
def testValue = context.expand('${#TestCase#test}') as Double
//Add and subtract with new variables.
def testUp = testValue + 0.01
def testDown = testValue - 0.01
//Check if the values are correct, of course, value may vary based on the data.
assert 47.80 == testUp
assert 47.78 == testDown
Correct function name is : Double.parseDouble(test)
def test= messageExchange.modelItem.testStep.testCase.getPropertyValue('test')
def testUp= Double.parseDouble(test) + 0.01
log.info testUp
log.info testUp.getClass()
I want to convert def date= new Date() to string so that i could make this expression def text=data+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response and then use text as an string i have tries toString() but it wasn't helpful any better ideas?
You don't even need toString(), just using it in a String concatenation transforms it, but you have to use String + Date, not Date + String, just like in Java. So either use
def text=""+date+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response
or
def text=(data as String)+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response
or
def text=data.toString()+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response`
I am working in SoapUI , which supports GroovyScript in TestCases.
In some TestCases i supposed to use a date of now + 15 minutes, 30, or 90 minutes.
If im using this script:
import java.util.Calendar;
def tdFormat = "yyyy-MM-dd HH:mm"
def today = Calendar.getInstance()
def today15min = today.add(today.MINUTE,15)
def todayFormated = today15min.format(tdFormat)
gets NullPointerException: Cannot invoke method format() on null object error at line: 6.
How can i fix this?
Using TimeCategory.
use( groovy.time.TimeCategory ) {
println 15.minutes.from.now.format( 'yyyy-MM-dd HH:mm' )
}
Calendar is a static class used to create Dates. Calendar.add() returns void, because it simply modifies the Calendar. You need to call getTime() to get a Date object which you can then format how you please.