Convert Date of Birth into Age in Spark Dataframe API

前端 未结 4 1893
被撕碎了的回忆
被撕碎了的回忆 2021-01-16 08:16

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

4条回答
  •  花落未央
    2021-01-16 08:31

    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).

提交回复
热议问题