从今天开始计算Groovy的年龄 [英] Calculate age in Groovy from today
本文介绍了从今天开始计算Groovy的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def now = new Date()
def dob = new Date( '08/21/1982')
println now - dob
我们可以除以365,但这不完全准确。必须有一个简单的方法来做到这一点,但我似乎无法找到它。有任何想法吗?
解决方案
或者使用Java 8和Groovy 2.3 RC(如果您希望处于最前沿); - )
import java.time。*
LocalDate today = LocalDate.now()
LocalDate birthday = LocalDate.of(1982,Month.AUGUST,8)
Period period = between(birthday,today)
println$ period.years years,
| $ period.months months,
| $ period.days days。stripMargin()
Trying to figure out how old someone is based on their bday. The following code gives us the number of days
def now = new Date()
def dob = new Date('08/21/1982')
println now - dob
We could divide by 365 but that isn't entirely accurate. There has to be an easy way to do this but I just can't seem to find it. Any ideas?
解决方案
Or using Java 8 and Groovy 2.3 RC (if you want to be on the bleeding edge) ;-)
import java.time.*
LocalDate today = LocalDate.now()
LocalDate birthday = LocalDate.of( 1982, Month.AUGUST, 8 )
Period period = Period.between( birthday, today )
println """$period.years years,
|$period.months months,
|$period.days days""".stripMargin()
这篇关于从今天开始计算Groovy的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文