0
你好,我有一個關於在常規使用TimeCategory問題。 在我的項目,我得到了計算幾個月獨特方法。我想將它添加到TimeCategory中。Groovy的擴展TimeCategory類
public Integer calcMonateAboDays(def fromDate) {
Date startDate
if (fromDate instanceof String) {
startDate = fromDate.toDate()
} else if (fromDate instanceof Date) {
startDate = fromDate
} else {
assert false: "Wrong fromDate class: " + fromDate.getClass()
}
Date endDate = null
use(TimeCategory) {
endDate = startDate + 1.month
Calendar endCalendar = Calendar.getInstance()
endCalendar.setTime(endDate)
int lastDayOfMonth = endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)
int endDayOfMonth = endCalendar.get(Calendar.DAY_OF_MONTH)
Calendar startCalendar = Calendar.getInstance()
startCalendar.setTime(startDate)
int startDayOfMonth = startCalendar.get(Calendar.DAY_OF_MONTH)
if (lastDayOfMonth != endDayOfMonth || startDayOfMonth == lastDayOfMonth) {
endDate--
}
}
return (endDate - startDate) + 1
}
我怎樣才能把它添加到現有的TimeCategory類中使用這樣的:
Date date = new Date()
use(TimeCategory) {
System.out.print(date + 1.monateAbo)
}
這樣的東西:
TimeCategory.metaClass.getMonateAbo() {
}
does not工作:(