# Invalid Date time format value error

**URL:** <https://community.gatling.io/t/invalid-date-time-format-value-error/7283>\
**Category:** Gatling (Open-Source)\
**Created:** [November 2, 2022, 2:25pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283 "2022-11-02T14:25:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahope](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ahope](https://community.gatling.io/u/ahope)\
**Post date:** [November 2, 2022, 2:25pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/1 "2022-11-02T14:25:25Z")

</div>

Hi,  
I am trying to inject date variable to my json request body but getting this error:

```auto
body:
[{"errors":[{"message":"Variable $limitingPeriodEnd of type ISO8601DateTime was provided invalid value","locations":[{"line":1,"column":203}],"extensions":{"value":"#{endOfWeekDate}","problems":[{"path":[],"explanation":"Could not coerce value \"#{endOfWeekDate}\" to ISO8601DateTime"}]}},{"message":"Variable $limitingPeriodStart of type ISO8601DateTime was provided invalid value","locations":[{"line":1,"column":240}],"extensions":{"value":"#{todaysDate}","problems":[{"path":[],"explanation":"Could not coerce value \"#{todaysDate}\" to ISO8601DateTime"}]}},{"message":"Variable $now of type ISO8601DateTime was provided invalid value","locations"

```

Here is the session information with the generated date values:  
Session:  
`Session(Booking manager Navigation Booked,2,HashMap(gatling.http.cache.wsBaseUrl -> ... gatling.http.cache.baseUrl -> ., todaysDate -> 2022-11..-02T14:04:42.607Z, endOfWeekDate -> 2022-11-08T15:04:42.607Z`

Here how I inject them:

```auto
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
  dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
  val timeNow = (Calendar.getInstance())

val scnBookingManagerBooked = scenario("Booking manager Navigation Booked")
    .exec(session => {
      session.set(
        "todaysDate",
        dateFormat.format(timeNow.getTime())
      )
    })
    .exec(session => {
      timeNow.add(Calendar.DAY_OF_WEEK, +6)
      session.set(
        "endOfWeekDate",
        dateFormat.format(timeNow.getTime())
      )
    })

```

Finally here is the .json file I use as the request body for the graphql call:

```auto
{
        "operationName": "Bookings",
        "variables": {
            "bookingType": null,
            "first": 15,
            "limitBookings": true,
            "limitingPeriodEnd": "#{endOfWeekDate}",
            "limitingPeriodStart": "#{todaysDate}",
            "now": "#{todaysDate}",
            "safeguardCheck": null,
.....

```

I appreciate if you can help me figure out why I am getting the error even the format is correct.  
I tried changing the data.format to String but that didn’t work.  
One last thing. I don’t get an error when I copy and paste the date format from the session (logs of previously ran test), into the json file manually and run the test. So the format is the correct format.

Thanks,

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 2, 2022, 3:17pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/2 "2022-11-02T15:17:46Z")

</div>

First, `SimpleDateFormat` is not thread-safe. What you’re doing here is share a single instance and call `format` from multiple treads. This is going to cause erroneous results under load. You should use the `java.time` API which is thread-safe.

Then, note that `timeNow` is a constant, not something that’s evaluated in every function. As is, all the virtual users will have the same value.

Finally, it looks like your `#{}` in your request body template are not being evaluated.  
How are you passing it? `StringBody`?  
Are you using Gatling 3.8 where the `#{}` syntax was introduced?

---

<div class="post-metadata">

**Author:** ![ahope](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ahope](https://community.gatling.io/u/ahope)\
**Post date:** [November 3, 2022, 1:19pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/3 "2022-11-03T13:19:01Z")

</div>

Thank you!  
I am switch to java.time API but not I don’t see the values in the session log. Since I switched to java.time, I didn’t need to use the ‘timeNow’ constant.

I use Gatling 3.8.4. I am passing it as a string into my json request body file.

Here is how I tried to create the dateTime and pass it:

```auto
val f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
val d = LocalDateTime.now
val scnBookingManagerBooked = scenario("Booking manager Navigation Booked")
    .exec(session => {
      session.set(
        "todaysDate",
        f.format(d).toString()
      )
    })
    .exec(session => {
      d.plusDays(6)
      session.set(
        "endOfWeekDate",
        f.format(d).toString()
      )
    })
    .exec(HuddleCookie.injectIntoSession)
    .exec(navigateTo("/booking-manager"))
    .exec(BookingManagerRequests.bookedBookings) //this call uses the #{} values in json

```

Here is the request that uses the json values

````auto
val bookedBookings = exec(
    http(getGraphQLName("bookingManager/bookedBookings.json"))
      .post("/graphql")
      .headers(graphqlHeader)
      .body(
        RawFileBody("bookingManager/bookedBookings.json")
      )
      .check(noGraphQLErrors)
      .check(jsonPath("$[0].data.bookings.totalCount").ofType[Int].is(4))
  )```
````

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 3, 2022, 1:36pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/4 "2022-11-03T13:36:13Z")

</div>

DateTime is immutable.  
`d.plusDays(6)` returns a new instance, doesn’t mutate `d`.

`RawFileBody` means **RAW** = no interpolation. You want to use `ElFileBody` to enable Gatling EL (Expression Language).

---

<div class="post-metadata">

**Author:** ![ahope](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ahope](https://community.gatling.io/u/ahope)\
**Post date:** [November 3, 2022, 1:39pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/5 "2022-11-03T13:39:26Z")

</div>

Thank you so much! I knew I was making a stupid mistake! I fixed it and it is working now.  
I appreciate your help Stephane.

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [November 5, 2022, 12:54pm UTC](https://community.gatling.io/t/invalid-date-time-format-value-error/7283/6 "2022-11-05T12:54:11Z")

</div>


