在Groovy想出了這個,不知道這是否符合您的要求,但它給出了這個問題的結果:
def dateMalarkey(Date from, Date to) {
def val = [from, to].collect { d ->
def cal = Calendar.instance
cal.time = d
[y: cal[Calendar.YEAR], m: cal[Calendar.MONTH], d: cal[Calendar.DAY_OF_MONTH]]
}.inject { p, n ->
[y: Math.abs(p.y - n.y), m: Math.abs(p.m - n.m), d: Math.abs(p.d - n.d) + 1]
}
val.days = val.y * 365 + val.m * 30 + val.d
val
}
def inputs = [
[from: '2014-02-01', to: '2015-02-01'],
[from: '2000-02-10', to: '2003-01-18'],
[from: '2004-02-02', to: '2004-06-24']
]
inputs.each { d ->
println "$d.from - $d.to => " +
dateMalarkey(Date.parse('yyyy-MM-dd', d.from), Date.parse('yyyy-MM-dd', d.to))
}
看起來像,沒關係。謝謝! – Alex