Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert date into Milliseconds

Nagappan Murugappan March 16, 2021

i have date value which comes from JIRA database (we are using - 

  • v8.13.1)

In groovy script I store this date into a variable and wanted convert that into milliseconds to calculate further. The core is as follows. Please let me know what is the best way to do this.

 

def emailSendDate = (it.SEND_DATE)

def dt = new Date()

def dateObject = dt.parse("yyyy-MM-dd'T'HH:mm:ss", emailSendDate)

log.warn(dateObject)

 

Getting the error - 

2021-03-16 12:45:02,579 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: java.util.Date.parse() is applicable for argument types: (String, java.sql.Timestamp) values: [yyyy-MM-dd'T'HH:mm:ss, 2021-03-15 11:53:24.0] Possible solutions: parse(java.lang.String), parse(java.lang.String, java.lang.String), parse(java.lang.String, java.lang.String, java.util.TimeZone), wait(), clone(), any() at Script2391$_run_closure4$_closure5$_closure6.doCall(Script2391.groovy:133) at Script2391$_run_closure4$_closure5.doCall(Script2391.groovy:124) at Script2391$_run_closure4.doCall(Script2391.groovy:102) at Script2391.run(Script2391.groovy:86)

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
March 21, 2021

Hi Nagappan,

You may want to try using the Simple Date Formatter to format the date instead of the Date.parse.

For example:-

import java.sql.Timestamp
import java.text.SimpleDateFormat

def originalDate = getFieldById(fieldChanged)
def originalDateValue = originalDate.value.toString()

def additionalDays = getFieldByName("Additional Days")
def additionalDaysValue = additionalDays.value as Double

def newDate = getFieldByName("New Date")

def days = 24 * 60 * 60 * 1000

if( originalDateValue != "" && originalDateValue.length()>0 && additionalDays.value != null) {

def oldDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(originalDateValue)

def additionalDaysNum = (new Double(additionalDaysValue)).longValue()

def ts = new Timestamp(oldDate.time).time

def updatedTimeStamp = new Timestamp(ts + additionalDaysNum * days).time

def updatedDate = new Date(updatedTimeStamp)

def formattedDate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss zzz").format(updatedDate)

newDate.setFormValue(formattedDate)
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to modify it accordingly.

 

I hope this helps to solve your question. :)

 

Thank you and Kind Regards,

Ram

Nagappan Murugappan March 23, 2021

Thanks Ram. I was able to achieve with the below mentioned.

 

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateString = "23-03-2021 11:18:32"

Date date3 = sdf.parse(dateString);
log.warn ( date.getTime())

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events