我可以在 groovy 中的单个日期范围内调用 get(i) 或 getFrom() 吗?
我有一个需要 groovy 范围的方法,并且传递类似的内容没有问题
新日期(“01/01/1999”)..新日期(“01/01/1999”)
但我更喜欢传递单个日期(作为范围)
当我打印出来时,它看起来不错
范围范围=开始日期作为范围
这显示在控制台中
[1999 年 1 月 1 日星期五 00:00:00 CST]
但是现在当我尝试执行 .get(i) 或 .getFrom() 时,它失败了,说
groovy.lang.MissingMethodException: No signature of method: java.util.Date.getFrom() is applicable for argument types: () values: []
Possible solutions: getDate(), getDay(), getTime(), getYear(), before(java.util.Date), getAt(int)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
at org.codehaus.groovy.runtime.InvokerHelper$invokeMethod.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at Date_delegateProxy.getFr
Anybody has success attempts to Cast and use a single date as Range in groovy?
I have a method that requires a range in groovy and I've got no problem passing in something like
new Date("01/01/1999")..new Date("01/01/1999")
But I would much prefer to pass in a single date (as Range)
When I print this out it looks good
Range range = startDate as Range
This shows up in the console
[Fri Jan 01 00:00:00 CST 1999]
But now when I try to do a .get(i) or .getFrom() it fails saying
groovy.lang.MissingMethodException: No signature of method: java.util.Date.getFrom() is applicable for argument types: () values: []
Possible solutions: getDate(), getDay(), getTime(), getYear(), before(java.util.Date), getAt(int)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
at org.codehaus.groovy.runtime.InvokerHelper$invokeMethod.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at Date_delegateProxy.getFr
Anyone have success trying to cast and use a single date as Range in groovy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Groovy 中,
as
运算符使用您想要将其转换为参数的类来调用对象上的asType
方法。 DefaultGroovyMethods 提供了我认为 Groovy 附带的所有实现据我所知,它们都没有转换为范围。如果需要,您可以覆盖 asType 以支持 Range,但是我认为大多数人会认为滥用运算符重载和如此糟糕的做法,以至于我实际上不愿意提供一个示例。尽管如此,这应该可以满足您的要求。
In Groovy the
as
operator calls theasType
method on the object with the class you want to convert it to as an argument. DefaultGroovyMethods provides all the implementations that ship with Groovy that I'm aware of, none of which convert to Range.You can if needed override asType to support Range, however I think most people would consider that abuse of operator overloading and such poor practice that I'm actually hesitant to provide an example. None the less, this should do what you're asking.