SOAPUI - groovy script not working on ONE of machines - groovy

see below simple groovy script for Date parsing:
def setProperty=testRunner.testCase.getTestStepByName("Properties");
Date currentDate = new Date();
String tmpDate = currentDate
//setting current DateTime to corresponding property
def DateTime = new Date().parse("E MMM dd H:m:s z yyyy", tmpDate).format("yyyy-MM-dd'T'hh:mm:ss")
setProperty.setPropertyValue('DateTime', DateTime);
This scripts correctly works on some machines with Win 8.1, java 7 or 8 installed. But for one of machines (Win 8.1) it returns an error
java.text.ParseException: Unparseable date: "Tue Jan 20 11:59:58 EET 2015"
What's the problem? Script absolutely the same...
Sincerely,
Dmitry

What you are doing is extremely awkward! You should consider using String.format() to format your date straight up:
def propertiesStep = testRunner.testCase.getTestStepByName("Properties")
propertiesStep.setPropertyValue('DateTime',
String.format("%tFT%tT", new Date(), new Date()))

Your code seems confusing... you're generating a java.util.Date with new Date(), then you're generating a String when you do String tmpDate=currentDate, then you're trying to get a Date() again parsing the tmpDate string and finally you're parsing the last date to get a string date with specific format...
I think that you must clean up your code a bit... I think that you're looking for something as follow:
def setProperty=testRunner.testCase.getTestStepByName("Properties")
def currentDate = new Date()
def dateTime = currentDate.format("yyyy-MM-dd'T'hh:mm:ss")
log.info dateTime
setProperty.setPropertyValue('DateTime', dateTime)
Hope this helps,

Related

Add days to current date in groovy

I am trying to get the current date and change the format and add 30 days to it, i tried the following in groovy :
def date = new Date().format("yyyy-MM-dd")
def laterdate = date + 30
log.info laterdate
I get the output as (formatting looks good)
Mon Jul 24 12:24:04 MST 2017:INFO:2017-07-2430
can someone please advise where i am doing wrong
To add days:
Date date = new Date().plus(30)
To Substract days:
Date date = new Date().plus(-30)
def today = new Date()
def yesterday = today + 30
log.info today.format("yyyy-MM-dd")
log.info yesterday.format("yyyy-MM-dd")

inbuilt parser in Python for handling dates like : 05/May/2010:12:01:15 +0000

In a logfile, I have my date and time recorded in the format :
[05/May/2010:12:01:15 +0000]
I am trying to extract only the time from the above in Python3.x. I was mainly looking for a inbuilt parser in Python3.x. I ran into different formats except for this. I came up with a solution in JAVA using the below code and I am looking for something similar in Python3.x. Is there one ? Or do I have to write my own parser for extracting the date,time out of this ? Here is the JAVA code of what I wanted :
//field[3] contains "[25/May/2015:23:11:15 +0000]"
String timeStamp = fields[3].substring(1,fields[3].length()).split(" ")[0];
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.US);
Date d = null;
try {
d = df.parse(timeStamp);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Time :"+ d.getTime());// Prints 23:11:15
You can use time.strptime() to parse it into a time.struct_time object:
import time
your_field = "[25/May/2015:23:11:15 +0000]"
parsed = time.strptime(your_field, "[%d/%b/%Y:%H:%M:%S %z]")
# NOTE: %z depends on implementation, you might need to remove the timezone info
# before parsing your date/time with `time.strptime()`.
# print time:
print(time.strftime("%H:%M:%S", parsed))
# prints: 23:11:15
But if you just want to get the time you don't need to parse it just to build it again, instead you can just substring it out:
your_field = "[25/May/2015:23:11:15 +0000]"
your_time = your_field.split(":", 1)[1].split(" ", 1)[0]
# print time:
print(your_time)
# prints: 23:11:15
Here is a solution using datetime.strptime:
from datetime import datetime
field3 = '[25/May/2015:23:11:15 +0000]'
result = datetime.strptime(field3, '[%d/%b/%Y:%H:%M:%S %z]')
print(result.strftime("%H:%M:%S"))
Output
23:11:15

How to get current datetime with format Y-m-d H:M:S using node-datetime library of nodejs?

I'm using node-datetime library. I want to get current datetime with format such as Year-month-day hour-minute-second
ex : 2016-07-04 17:19:11
var dateTime = require('node-datetime');
var dt = dateTime.create();
dt.format('m/d/Y H:M:S');
console.log(new Date(dt.now()));
But my result such as:
Mon Jul 04 2016 17:19:11 GMT+0700 (SE Asia Standard Time)
See the docs for details on format:
Returns a formatted date time string.
Store the result of your call to dt.format and don't pass this to the Date constructor:
var dateTime = require('node-datetime');
var dt = dateTime.create();
var formatted = dt.format('Y-m-d H:M:S');
console.log(formatted);
[ Run the example above: https://replit.com/#SamGluck1/so-node-datetime ]
I have amended the format string from 'm/d/Y' to 'Y-m-d' as per your question.

Convert time in milliseconds to date format (YYYY-MM-DD) in Groovy

Is there a function to convert time in milliseconds to date format (YYYY-MM-DD) in Groovy?
I have a Groovy script which needs to compare to date values as follows:
for(i in order_lineitems)
{
if(i.startDate==order_submit_date)
{
matchedIds1 += i.salesOrderLineitemId+',';
}
}
Here i.startDate has time in milliseconds of the date format yyyy-mm-dd whereas order_submit_date has the time in milliseconds in the date format yyyy-MM-dd HH:mm:ss. I need to convert order_submit_date into this format yyyy-mm-dd within the if block itself.
I am new to the Groovy script and I need help here.
There was a small mistake in my code. I corrected it.
The if block should be as follows if (i.startDate == order_submit_date) and both are long values represented in millis.
Now I need to make sure the condition is right i.e. start date is equal to order submit date.
Here what is happening is :
i.startDate has the value 1452105000000 (Thu Jan 07 2016 00:00:00) which is been stored in the DB when a Sales Order is created
and order_submit_date has the value 1452158393097 (Thu Jan 07 2016 14:49:53) which is being genertaed on the flow when a user submits the Sales order for approvals in the UI.
Now since order_sbmit_date has both date and time the long value is different and am unable to satisfy the condition.
Hence now i have a question as to wether there a function in groovy which would convert my order_submit_date long value to Date(yyyy-mm-dd) format and then compare both the values so as to satisfy the if block.
You can compare your dates in millis like this:
Notice that solutions depend on timezone.
Groovy option:
def compare(def m1, def m2) {
def dateInMillis1 = new Date(m1)
def dateInMillis2 = new Date(m2)
dateInMillis1.clearTime() == dateInMillis2.clearTime()
}
Java option 1:
boolean compare1(long millis1, long millis2) {
Date dateFromMillis = new Date(millis1);
Date dateFromMillis2 = new Date(millis2);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
sdf.format(dateFromMillis).equals(sdf.format(dateFromMillis2));
}
or you can use Calendar:
Java option 2:
boolean compare2(long m1, long m2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(m1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTimeInMillis(m2);
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH);
}
Your question is not clear that whether your dates are in string format or in milliseconds (long).
If dates are in string format like "2015-10-31"
You can use SimpleDateFormat for this.
import java.text.SimpleDateFormat
...
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd")
SimpleDateFormat dateTimeParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
for (i in order_lineitems) {
if (dateParser.parse(i.startDate) >= dateTimeParser.parse(order_submit_date)) {
matchedIds1 += i.salesOrderLineitemId+',';
}
}
If dates are in milliseconds:
for (i in order_lineitems) {
if (new Date(i.startDate.toString().toLong()) >= new Date(order_submit_date.toString().toLong())) {
matchedIds1 += i.salesOrderLineitemId+',';
}
}
Note: Capital Y and small y (similarly for m & h) matterns in terms of formatting so please be clear about the usage.
Actual Answer to the Question:
You don't need any of the above solution instead you can simply use the clearTime() method on the date like below:
for (i in order_lineitems) {
if (new Date(i.startDate) >= new Date(order_submit_date).clearTime()) {
matchedIds1 += i.salesOrderLineitemId+',';
}
}
The clearTime() method will simply remove the time part from your date i.e. will convert Thu Jan 07 2016 14:49:53 to Thu Jan 07 2016 00:00:00`
What are type for i.startDate and order_submit_date java.util.Date? or String?
And do you want use i.startDate and order_submit_date only to do compare?
-- updated --
ok, then maybe like folowing?
import java.text.SimpleDateFormat
String startDate = "2016-01-20"
String order_submit_date = "2016-01-20 12:34:56"
SimpleDateFormat formatDateWithTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd")
if (formatDate.parse(startDate) >= formatDateWithTime.parse(order_submit_date)) {
println "hehe"
} else {
println "hoho"
}
SimpleDateFormat#parse() returns java.util.Date.
Also you can compare with these.
You can also write like follows!
import java.text.SimpleDateFormat
SimpleDateFormat formatDateWithTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd")
def matchedIds1 = order_lineitems.findAll {
formatDate.parse(it.startDate) >= formatDateWithTime.parse(order_submit_date)
}.join(",")
Long(type) version
def matchedIds1 = order_lineitems.findAll {
new Date(it.startDate).clearTime() == new Date(order_submit_date).clearTime()
}.join(",")
Long(type) without clearTime version
String format = "yyyy-MM-dd 00:00:00"
SimpleDateFormat fillByZero = new SimpleDateFormat(format)
def matchedIds1 = order_lineitems.findAll {
Date a = new Date(it.startDate)
Date b = new Date(order_submit_date)
fillByZero.parse(a.format(format)) == fillByZero.parse(b.format(format))
}.join(",")

date method not working properly in groovy

i have this
class MainController {
def test = {
def day1 =1
def month1 = 10
def year1 = 2011
def date1 = new Date(year1 ,month1, day1);
}
}
But the OutPut is
Wed Nov 01 00:00:00 PKT 3911
Why its 3911 , shouldnt it be 2011 ??
Any solution
Thanks
Read the Date API of this deprecated constructor. You must pass in the year-1900. Also, the month is zero-based (also in the API docs).
That information was also shown in my answer to one of your previous questions.

Resources