How to parse string to Localdate in timer event.

1
0
-1

I created a timer connector inside Bonita bpm process. I set the end date with a groovy script. However, I cannot get the result.

There is no error, however, the timer doesn't evoke the next trigger

I would like to get the exact time like 2019-02-20 13:11:00

My code is as follows.

    import java.time.LocalDate
    import java.time.format.DateTimeFormatter

    final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    final String input = "2019-02-20";
    final LocalDate localDate = LocalDate.parse(input, DATE_FORMAT);

    //LocalDate dueDate = localDate.plusDays(1);
    //LocalDate dueDate = localDate.minusDays(2);

    //create calendar
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(localDate.getYear(), 
    localDate.getMonthValue(),localDate.getDayOfMonth(), 15, 11, 00);
    calendar.getTime();

How can parse a string to Local date in groovy?

Any help would be appreciated.

Thanks.

1 answer

1
+1
-1

Hello,

I hope I understood well your question.

LocalDate doesn't store the time, I suggest you to use LocalDateTime instead.

Here's an example of code that I tested :

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    final String input = "2019-02-20 13:11:00";
    final LocalDateTime localDate = LocalDateTime.parse(input, DATE_FORMAT);

    //create calendar
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(localDate.getYear(), localDate.getMonthValue(),localDate.getDayOfMonth(), localDate.getHour(), localDate.getMinute(),        localDate.getSecond());
    calendar.getTime();

Does this work better ? if not, you can upload an export of your project (.bos file) and send the link. I can then understand better and check if the error is not somewhere else.

Regards,

Comments

Submitted by donghee.baik on Thu, 02/21/2019 - 14:54

Thanks for your command. ;)

Since I used the data type date only. I need to use LocalDate

Finally, I found the reason why I cannot get the calendar time. localDate.getMonthValue() returned number -1 of the month.

For example, in the case of February, the return value is 1.

I put the -1, now it is working. I am not sure it is the correct answer.

//create calendar
Calendar calendar = GregorianCalendar.getInstance();
calendar.set(localDate.getYear(), localDate.getMonthValue()-1,localDate.getDayOfMonth(), 16, 21, 00);
//calendar.set(2019, 1,20, 16, 15, 46);
calendar.getTime();

Thanks.

Notifications