Skip to content

Instantly share code, notes, and snippets.

@silverbeak silverbeak/YearDiff.groovy
Last active Dec 30, 2015

Embed
What would you like to do?
There was a question on SO on how to calculate someones current age in years. It's a simple enough operation using Joda-time (http://joda.sourceforge.net/). I thought I'd turn my example into a gist, if anyone else is interested. It's easy to turn this into something that would calculate the age in days or months or whatever... Please tell me if…
import org.joda.time.DateTime
import org.joda.time.Years
def calculateAgeInYears(int year, int monthOfYear, int dayOfMonth) {
def yob = new DateTime(year, monthOfYear, dayOfMonth, 0, 0).toDateMidnight()
def now = DateTime.now().toDateMidnight()
return Years.yearsBetween(yob, now).getYears()
}
def years = calculateAgeInYears(1983, 12, 10)
println "Years: $years"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.