Convert Date of Birth into Age in Spark Dataframe API

前端 未结 4 1885
被撕碎了的回忆
被撕碎了的回忆 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).

    0 讨论(0)
  • 2021-01-16 08:36
    select datediff(current_date(),
             TO_DATE(CAST(UNIX_TIMESTAMP(dateOfBirth,'yyyy-MM-dd') AS TIMESTAMP)))/365 as age
      from <TABLE_NAME>
    
    0 讨论(0)
  • 2021-01-16 08:39

    I was able to convert the date of birth column to age using udf with sql date format. Please see the edit for details.

    0 讨论(0)
  • 2021-01-16 08:39
    LocalDate birthdate = new LocalDate (1970, 1, 20);
    LocalDate now = new LocalDate();
    Years age = Years.yearsBetween(birthdate, now);
    
    0 讨论(0)
提交回复
热议问题