Inline Scripting - need to add X hours to specific datetime value?
Hey!
So- - new job (finally!) and I've got a payload that I need to handle with a bit of datetime inline scripting.
SO! - I have 2 fields in the payload with values with the formats as below:
<OccurrenceDateTime>CCYY-MM-DDTHH:mm:ss</OccurrenceDateTime>
<ReturnDateTime>CCYY-MM-DDTHH:mm:ss</ReturnDateTime>
The ReturnDateTime value needs to be X number of hours AFTER the OccurenceDateTime tag value (as it's a return journey), BUT, I also need to be able to specify OccurenceDateTime as currentdatetime plus/minus X hours
My easiest recourse is to use a bit of inline scripting so I tried altering the following options:
${=new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date())}
OR
${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -0); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());}
I tried various edits around the following bit (to add and subtract X hours)
(cal.getTime())
but none of my edits worked - I tried just adding in +6 into the getTime method - but that didn't work - I tried a couple of other things - but that didnt work
I can add or minus time from a value in a groovy script, but that's when I'm working with a variable - I cant seem to manage adding/minusing hours using the inline scripting parameterisation using the above inline scripting items as a template.
Can anyone help out please?
Much appreciated! 🙂
Rich
Hey!
I found what i was looking for in link
so if anyone else is looking you can use the following to plus/minus hours and days to current date
CURRENT DATE ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -0); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());} CURRENT DATE MINUS 2 DAYS FROM CURRENT DATETIME ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -2); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());} CURRENT DATE PLUS 4 DAYS FROM CURRENT DATETIME ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, +4); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());} MINUS 8 HOURS FROM CURRENT DATETIME ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR_OF_DAY, -8); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());} ADD 7 HOURS FROM CURRENT DATETIME ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR_OF_DAY, +7); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());}
so I'm marking this as fixed!
UPDATE:
Oh just an update - if you want to add say more than one day - e.g. one and a half days - you can either just add the same number of hours (1.5 days = 36hours) as follows:
${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR_OF_DAY, +36); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());}
OR - you can add in a separate cal.add() method as well - so the above has the cal.add(Calendar.HOUR_OF_DAY, +36) - but you can add in the cal.add(Calendar.DATE) method as well so it reads as follows:
${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, +1); cal.add(Calendar.HOUR_OF_DAY, +12); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(cal.getTime());}
I hope I've been clear!
Thanks,
Rich