一.时间戳转化为日期格式:
val time="1622457251089" //TODO 时间转换函数:时间戳 1622457251089 def replaceTime1(tm:String) :String= { import java.text.SimpleDateFormat val sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val format: String = sf.format(new Date(Long.parseLong(time))) format } println("转化后的时间是:"+replaceTime1(time))
二.带时区的格式:2021-04-27T11:34:52+08:00
import org.apache.spark.sql.functions.udf //TODO 时间格式转换函数 2021-04-27T11:34:52+08:00 def replaceTime1(tm:String) :String= { val t1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX") val t2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val t: Date =t1.parse(tm) val newtime=t2.format(t) newtime } spark.udf.register("replaceTime1",replaceTime1(_:String))//需要注册一下 val addCol_replacegTime1 = udf(replaceTime1 _) val dfewm03=dfewm02.withColumn("inTime",addCol_replacegTime1(dfewm02("intime")))
三.类时间格式:20210703105537
import org.apache.spark.sql.functions.udf //TODO 时间转换函数:20210703105537 def replaceTime2(time:String):String={ val time1=new SimpleDateFormat("yyyyMMddHHmmss") val time2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val t: Date =time1.parse(time) val newtime=time2.format(t) newtime } spark.udf register("replaceTime2", replaceTime2(_: String))//注册 val addCol_replacegTime2 = udf(replaceTime2 _) val dfewm04=dfewm03.withColumn("payTime2",addCol_replacegTime2(dfewm03("payTime")))