Hello,
I'm using the last version of the Community version (7.11.0).
I have a business variable including a DATE ONLY field.
I want to initialize the date with the value of December 31th 2019.
So, when I created my pool variable from a Business Object, I added a script in the "default value" field.
I tried many ways but the process execution always generates an error.
The last script I used :
import java.text.SimpleDateFormat
def formatDate = new SimpleDateFormat()
def myDate = formatDate.parse("yyyy-MM-dd","2019-12-31")
return myDate
And the error is :
org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.MissingMethodException: No signature of method: java.text.SimpleDateFormat.parse() is applicable for argument types: (java.lang.String, java.lang.String) values: [yyyy-MM-dd, 2019-12-31]\
I've also tried the following, but in vain :
def myDate = Date.parse("yyyy-MM-dd","2019-12-31")
def myDate = LocalDate.parse("yyyy-MM-dd","2019-12-31")
Thank you in advance for your help.
Hi, from the error message, it seems that you cannot use SimpleDateFormat.parse() with two strings. From the javadoc (https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String,java.text.ParsePosition)) it seems that it requires a string for the date as the first parameter and a Parse Position.
Since you are passing two String parameters, I'm having a hard time understanding your use case. What are you trying to do exactly? Pass a date and a date format and get the date in that format as a result? Or something else?
Hello, as written in the fisrt lines of my message : I want to initialize a DATE ONLY field of a business variable with the value 31/12/2019.
Oh, sry about that. After research, I found this stackoverflow page: https://stackoverflow.com/questions/4216745/java-string-to-date-conversion. I went with the Java 8 method since the DATE ONLY field uses java.time.LocalDate, thus you would need to initialize it with that.
So, I had:
String string = "December 31th 2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd'th' yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
You will notice that this will only work if you have "th" after the date, so if you have "st", "nd" or "rd", this will not work, I suggest removing the "th", "st", "nd" or "rt" from the string and the pattern would be "MMMM dd yyyy". To do this, I would do :
string = string.replace("th", "").replace("st", "").replace("nd", "").replace("rd", "");
Hope this helps.
Thank you very much, it works
The final script is :
import java.time.format.DateTimeFormatter
import java.time.LocalDate
def myDateString = '2019-12-31'
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate myDate = LocalDate.parse( myDateString, formatter);