This seems simple but I couldn\'t find the answer. I\'m trying to convert a column of date-of-birth in the below date format to the date format in Spark Dataframe API and th
Instead of using the deprecated getXXX methods of java.util.Date, you should rather use java.util.Calendar.
Also your solution doesn't work in all cases. If someone is born on December 31st 1976. His age will be computed as 2015-1976 = 39 even though on January, 1st 2015 he won't be 39 for almost a full year.
You should rather use a computation as shown in: http://howtodoinjava.com/2014/05/26/java-code-to-calculate-age-from-date-of-birth/ (converting the Java code to Scala shouldn't be much of a problem).
select datediff(current_date(),
TO_DATE(CAST(UNIX_TIMESTAMP(dateOfBirth,'yyyy-MM-dd') AS TIMESTAMP)))/365 as age
from <TABLE_NAME>
I was able to convert the date of birth column to age using udf with sql date format. Please see the edit for details.
LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);