2017-10-22 36 views
0

时间设置到时在格式hh:mm通过例如8:00的日期,下面的代码按预期工作:MissingMethodException当我尝试打电话给合作者方法

package demo 

import static java.util.Calendar.* 

class Helper { 

    public static final Date setTimeToDate(final Date date, final String time) { 
     Calendar calendar = date.toCalendar() 
     Integer hourOfDay = time.tokenize(':')[0].toInteger() 

     calendar.set hourOfDay: hourOfDay, minute: 0, second: 0 

     calendar.getTime() 
    } 
} 

现在我尝试提取获得一天的小时到它自己的方法的逻辑,所以我的代码适应这种要求如下:

package demo 

import static java.util.Calendar.* 

class Helper { 

    public static final Date setTimeToDate(final Date date, final String time) { 
     Calendar calendar = date.toCalendar() 
     Integer hourOfDay = getHour(time) 

     calendar.set hourOfDay: hourOfDay, minute: 0, second: 0 

     calendar.getTime() 
    } 

    private Integer getHour(final String time) { 
     time.tokenize(':')[0].toInteger() 
    } 
} 

当我尝试调用setTimeToDate方法

println Helper.setTimeToDate(new Date(), '8:00') 

我收到以下错误

groovy.lang.MissingMethodException: No signature of method: static demo.Helper.getHour() is applicable for argument types: (java.lang.String) values: [8:00] 
Possible solutions: getAt(java.lang.String) 
    at demo.Helper.setTimeToDate(Script1.groovy:9) 
    at demo.Helper$setTimeToDate.call(Unknown Source) 
    at demo.Script1.run(Script1.groovy:21) 

我特别不理解的错误消息[8:00]

回答

1

setTimeToDate是静态的,但getHour是不是其中的一部分,所以它不能没有一个实例调用。使getHour也是静态的,它将被调用。

相关问题