How to put a fixed date into groovy code?


I took this line (which works) and am trying to put a fixed dummy date into it:

def eventPostTime = URLEncoder.encode((new Date()).format( 'MM/dd/yyyy HH:mm:ss', location.timeZone ))

I have tried countless variations, like:

def eventPostTime = URLEncoder.encode((new Date()).format( 'MM/dd/yyyy HH:mm:ss', '02/02/2002 02:02:02' ))


def eventPostTime = URLEncoder.encode((new Date( '02/02/2002 02:02:02' )).format( 'MM/dd/yyyy HH:mm:ss'))


def eventPostTime = URLEncoder.encode((new Date()).format( 'MM/dd/yyyy HH:mm:ss', '2002', '02', '02', '02', '02', '02' ))

Any dummy date at all will do.

Most every time I get an error like

error groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) values: [MM/dd/yyyy HH:mm:ss, 2002, 02, 02, 00, 00, ...] @ line 331

Google can’t help because web explanations are vague with no examples, or it’s for javascript (not groovy), or it gives an error when I do exactly what it says (but it’s never clear if the example was the right flavor of javascript/groovy/what the hell ever).

I can’t believe I lost three hours of my life (going on 4 or 5) going around in circles on something so incredibly simple as putting a fixed date into one line of code. :frowning: Groovy is such a b to learn, not the least because all the very-close-but-different languages really confuse everything.


Thanks if you can help!

Your first example looks correct assuming location.timeZone is valid (can’t tell how it’s initialized from your example).

Groovy Documentation for Date.format ->


I revised my OP to better say I already know the first line works


You mean something like this?

def dummyDate = Date.parse('MM/dd/yyyy HH:mm:ss', '02/02/2002 02:02:02'); def eventPostTime = URLEncoder.encode(dummyDate.format('MM/dd/yyyy HH:mm:ss')), java.lang.String)

1 Like

Dates are hard… Believe me. Some of the most advanced coders have trouble with Java/Groovy dates.

What you are doing wrong is you are putting the date and time where the TimeZone needs to be. So where it says “location.timezone” in your working example Groovy only understands timezones there.

What you need to do is create a string in the newDate area that is a properly formatted date string. You can see examples here. We use ISO8601 formatting.

Thanks Tim and especially Michael,

Yes Michael, that worked.

I really needed that concrete example. Not more references or links. I not only didn’t know just what text to use (yy/m/d or mm-dd-yyyy or who knows what) but there were actually about 5 other things wrong (it was actually a format method, it did not need New, it needed its own defining line, that defining line needed Parse)… Indeed, the fact that I can do this in several other languages only worked against me because I kept trying stuff simple in those ones, but simply wrong here. I neeed a concrete example.

I looked at pages and pages and pages of info on javascript and groovy dates. Most did not have any examples. Or were a long list of date and time keywords. What the heck can I do with that? (Go bananas!) If they did have examples, they were creating New dates or passing or doing math on values already in variables. So I could not find one single proper concrete example of making a date from fixed text (for groovy not javascript, etc.).

It’s wonderful you replied the second time Michael but I wondered if I was doomed by the universe to be foiled at every turn, after that first reply :stuck_out_tongue_winking_eye: May I politely request that you read posts carefully before replying?

And thank you greatly for your second reply!

Now I can go celebrate. I made a date in Groovy!

Anytime, glad I could help