1

I want to convert $departureFromDate (format:yyyy-MM-dd) to date object so that I can perform increment operations on it. I have been trying to do it the following way:

#set($departureFromDate = "{{jsonPath request.body 
'$.departureFromDate'}}")
#set($dateObj = $date.toDate('yyyy-MM-dd',"$departureFromDate"))
#set($calendar = $date.getCalendar())   
$calendar.setTime($dateObj)  
$calendar.add(6,5)

The above code works if give an actual date like:

#set($dateObj = $date.toDate('yyyy-MM-dd',"2018-09-22"))

But does not work when I try to use $departureFromDate

  • why should `{{jsonPath request.body '$.departureFromDate'}}` work? – Ori Marko Jan 25 '18 at 13:48
  • I am getting the date element out of a json request. The date in the request is in the format (yyyy-MM-dd). I am actually able to use the date for display through $departureFromDate. Only when I try to use $departureFromDate like above in order to get a date object it doesnt work. – Devashish Khattar Jan 25 '18 at 14:35

1 Answers1

1

There are several problems in your code. First, as user7294900 noted, the right value of the first assignation seems quite weird. Then, you don't need to instanciate yourself a calendar (plus, you can write $date.calendar instead of $date.getCalendar(), and you don't need double quotes around string arguments).

#set($body = '{ "departureFromDate" : "2018-03-01" }')
$json.parse($body)
#set($departureFromDate = $json.departureFromDate)
#set($dateObj = $date.toDate('yyyy-MM-dd', $departureFromDate))
#set($calendar = $date.toCalendar($dateObj))
$calendar.add(6, 5)

The above code uses a JSON parsing tool, whose parse() method renders a json wrapper, that you shall provide in your context.

As a final advise, if you hadn't already thought of it, be sure to print $obj and $obj.class.name in your context as a trivial debugging technique if you don't understand what happens.

Claude Brisson
  • 4,085
  • 1
  • 22
  • 30