Skip to content

Instantly share code, notes, and snippets.

@silverbeak
Last active December 30, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silverbeak/7899570 to your computer and use it in GitHub Desktop.
Save silverbeak/7899570 to your computer and use it in GitHub Desktop.
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