tryParseLocalDate 字符串转化为日期 minusMonths 减一个月,3月15调用结果为2月15;3月31的调用结果为2月28(如果是闰年则是29) plusMonths 加一个月,对返回日期的处理同上 dayOfWeek 返回星期几,返回值为Property对象,需要调用get方法获取int值 withMinimumValue 对某个属性加工,把这个属性变成范围内最小值,返回修改之后的时间,例如:tryParseLocalDate("2020-6-30").dayOfWeek().withMinimumValue() 返回"2020-6-29",因为29号是星期一,即一周的最小值;该方法同样适用于获取月份第一天 withMaximumValue 对某个属性加工,把这个属性变成范围内最大值,返回修改之后的时间,例如:tryParseLocalDate("2020-6-30").dayOfWeek().withMaximumValue() 返回"2020-7-05",因为7月5号是星期日,即一周的最大值;该方法同样适用于获取月份最后一天 someDate.dayOfMonth().withMaximumValue() 返回someDate对应月份的最后一天
Spark 测试类
package com.xx.xxx import org.apache.spark.sql.SparkSession import org.apache.spark.sql.functions.{substring, when} object AgeGroupUtils { // def get5YearsAgeGroup(age: Integer): String = { // val ageGroup = age match { // case age if (age == null || age < 15) => SparkConfig.UNSPECIFIED // case age if (age <= 25 && age >= 15) => SparkConfig.AGE_15TO25 // case age if (age <= 35 && age >= 26) => SparkConfig.AGE_26TO35 // case age if (age <= 45 && age >= 36) => SparkConfig.AGE_36TO45 // case age if (age <= 55 && age >= 46) => SparkConfig.AGE_46TO55 // case age if (age <= 100 && age >= 56) => SparkConfig.AGE_56TO100 // case age if (age > 100) => SparkConfig.UNSPECIFIED // case _ => SparkConfig.UNSPECIFIED // } // ageGroup } def main(args: Array[String]): Unit = { val spark = SparkSession.builder .master("local") .appName("appName") .getOrCreate() import spark.implicits._ var data3 = Seq( (null, "11", "22", "null", "1"), (null, "11", "22", "2020-01-02", "2"), ("666","10010", "19219", "5666", "3") ).toDF("DRCN_BirthYear", "DRCN_AgeFrom", "DRCN_AgeTo", "DRCN_RegDate", "id") .select( $"id", $"DRCN_RegDate", substring($"DRCN_RegDate",0,4) as "tmp", substring($"DRCN_RegDate",0,4)-($"DRCN_AgeFrom"+$"DRCN_AgeTo")/2 as "Y", when($"DRCN_BirthYear".isNotNull or $"DRCN_AgeFrom".isNull or $"DRCN_AgeTo".isNull,$"DRCN_BirthYear") .otherwise((substring($"DRCN_RegDate",0,4)-($"DRCN_AgeFrom"+$"DRCN_AgeTo")/2).cast("int")) as "BirthYear" ) data3.show() var m: Integer = null; // println(get5YearsAgeGroup(m)) // println(get5YearsAgeGroup(33)) // println(get5YearsAgeGroup(66)) // println(get5YearsAgeGroup(166)) } }