Last active
December 30, 2015 23:19
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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