13

In a previous version of grails I was able to use the groovy enhanced version of java.util.Date found here here.

After upgrading to grails 4, all those methods throw no signature of method on java.util.Date. Somehow the groovy additions aren't being picked up.

  def fdate=new Date();
  out << fdate.format("MM/dd/yyyy")
  //No signature of method: java.util.Date.format()
David Brown
  • 3,021
  • 3
  • 26
  • 46
user2782001
  • 3,380
  • 3
  • 22
  • 41
  • 2
    Do you have `org.codehaus.groovy:groovy-dateutil` in your project? – Jeff Scott Brown Aug 19 '19 at 17:01
  • Consider not using `java.util.Date`. That class is poorly designed and long outdated. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 21 '19 at 14:24

1 Answers1

23

Add a dependency to groovy-dateutil to your build.gradle:

runtime 'org.codehaus.groovy:groovy-dateutil'

The relevant extension method is defined at https://github.com/apache/groovy/blob/master/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java#L526-L528, which is in the groovy-dateutil library.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • Why it was excluded from standard build.groovy? Does this methods impair performance? – demon101 May 10 '20 at 15:16
  • 2
    "Why it was excluded from standard build.groovy?" - Most applications don't use it so we don't add it by default. "Does this methods impair performance?" - The presence of that library by itself doesn't impair performance but if it isn't being used, there is no reason to carry it around. – Jeff Scott Brown May 10 '20 at 16:47
  • Wasted time hunting this down...thanks for the solution. Still don't see why it's not part of the base Groovy.... Believe me it is useful.... what data do you have for the "Most applications don't use it"? Thx – Eric Kramer Oct 14 '20 at 15:28
  • 1
    `"Wasted time hunting this down"` - I am really sorry about that. We put it in the release notes and documentation. `"what data do you have for the ..."` - The library is specifically for manipulating and otherwise working with Dates. Many applications don't do that. – Jeff Scott Brown Oct 14 '20 at 15:56
  • @JeffScottBrown where are the release notes? I'm having trouble locating mention of this in the documentation. Thanks! – MT1 Dec 21 '20 at 19:42
  • 1
    "where are the release notes?" - The release notes for Groovy 2.5 are available at http://groovy-lang.org/releasenotes/groovy-2.5.html. "I'm having trouble locating mention of this in the documentation." - It is mentioned in the first bullet under "Breaking Changes". – Jeff Scott Brown Dec 21 '20 at 19:50