2

I am in the process of upgrading grails 2.2 to grails 4.0.10.

In grails 2.2 which used java 7 this used to work

def eanalytics = EventAnalytics.createCriteria().get(){
    eq('event', raceGroup)
    lt('dateCreated', timeService.now())
    gt('dateCreated', timeService.now() - 1)
}

now for grails 4 and java 8 i am getting this exception

Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.Date.minus() is applicable for argument types: (Integer) values: [1]
Possible solutions: find(), find(groovy.lang.Closure), is(java.lang.Object), any(), print(java.io.PrintWriter), use([Ljava.lang.Objec

Seems like i cannot get a new date by just subtracting the number of days to date in java 8. What is the equivalent code for doing this ? Thanks for the help.

timeservice.now is

def now(){
        def now = new Date()
        return now
    }
kofhearts
  • 3,607
  • 8
  • 46
  • 79
  • Works for me in Grails 4.0.12, so there seems to be something strange going on. Either try with that version, or try to isolate the problem by assigning the subtraction to a variable before the criteria. Probably unrelated note: you are also using get() which relies on the query always returning a single result, I would suggest withCriteria instead. – wwwclaes Dec 09 '21 at 07:50
  • thanks i fixed it by changing to localdate then doing minus and then back to java util date – kofhearts Dec 09 '21 at 07:53
  • 1
    @wwwclaes "so there seems to be something strange going on" - What is going on is just a missing dependency. – Jeff Scott Brown Dec 16 '21 at 03:55

2 Answers2

6

The Date extension methods were moved into org.codehaus.groovy:groovy-dateutil. If you add a dependency on that library, the minus method should start working.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
0

I fixed it by first changing date to localdate and then minusing 1 and then changing back to java.util.date.

def localDate = timeService.now().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate().minusDays(1)

        def gdate2 = Date.from(localDate.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant());
kofhearts
  • 3,607
  • 8
  • 46
  • 79