版权声明:觉得还行的话,右上角点个赞哟。 https://blog.csdn.net/u014384314/article/details/83381262
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
import org.apache.spark.sql.Column
import org.apache.spark.sql.functions._
import org.joda.time.DateTime
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter}
/**
* 对时间进行处理
* Created by 260169 on 2017/11/27.
*/
object DateUtil {
def dateFormat(time:Column){
time.cast("timestamp")
}
//求两个时间之差
def getDiffTime(startTime:Column,endTime:Column)={
if(startTime==null || endTime == null ) lit(-1)
else {
val timeFormt="yyyy-MM-dd HH:mm:ss"
val start=date_format(startTime,timeFormt)
val end =date_format(endTime,timeFormt)
unix_timestamp(end,timeFormt)-unix_timestamp(start,timeFormt)
}
}
//求某个时间的向前或向后x秒的时间
def getAddTime(start_Time:Timestamp,interval:Int):Timestamp={
var sdf:SimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
var timeFormt="yyyy-MM-dd HH:mm:ss"
var time:Long =DateFormat(start_Time)
val addtime: Long =time+interval
val addtime1: String =sdf.format(addtime*1000)
val finaltime=Timestamp.valueOf(addtime1)
finaltime
}
//返回Timestamp前Int天的年、月、日
def getYMDHMS(SubTime: String,Day:Int):String = {
val subY = SubTime.substring(0,4)
val subM = SubTime.substring(5,7)
val subD = SubTime.substring(8,10).toLong
val subH = SubTime.substring(11,13)
val subMI = SubTime.substring(14,16)
val subS = SubTime.substring(17,19)
val orgDay= (subD - Day).toString
val dd = subY+"-" +subM+"-"+orgDay+" "+subH+":"+subMI+":"+subS
val format1: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
// println( format1.format(format1.parse(dd)))
val tt =format1.format(format1.parse(dd))
tt
}
def getDay(dateString:String,beforeDay:Int):String={
val sdf:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val temp1=sdf.parse(dateString)
val calendar= Calendar.getInstance()
calendar.setTime(temp1)
// calendar.add(Calendar.YEAR,1)// 年加1
// calendar.add(Calendar.MONTH,1)// 月加1
calendar.add(Calendar.DAY_OF_MONTH,-beforeDay)//往前beforeDay
// calendar.add(Calendar.HOUR_OF_DAY,2)//小时加2
// calendar.add(Calendar.MINUTE,2)//分钟加2
// calendar.add(Calendar.SECOND,2)//秒加2
val result = sdf.format(calendar.getTime)
result
}
def DateFormat(time:Timestamp):Long={
val sdf: SimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //定义格式,不显示毫秒
val dt: Date =sdf.parse(time.toString)
dt.getTime()/1000
}
def DateFormatday(time:Timestamp):Long={
val sdf: SimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd") //定义格式,不显示毫秒
val dt: Date =sdf.parse(time.toString)
dt.getTime()/1000
}
//获取year、month、day字段
def getYMD(time: Timestamp, minusDay: Int = 0): (Option[Int], Option[Int], Option[Int]) = {
val format: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS")
val dateTime: DateTime = DateTime.parse(time.toString, format)
val localTime1: Array[String] = dateTime.minusDays(minusDay).toString("yyyy-MM-d").split("-", 3)//去除2016-10-01中的-
val year: Int = localTime1(0).toInt
val month: Int = localTime1(1).toInt
val day: Int = localTime1(2).toInt
(Option(year), Option(month), Option(day))
}
//获取当前系统时间
def getLocalTime: Timestamp = {
val localTime1 = DateTime.now.toString("yyyy-MM-dd HH:mm:ss")
val localTime = Timestamp.valueOf(localTime1)
localTime
}
// 获取当前时间
def getNowTime: Timestamp = {
val now=new DateTime()
val fmt=DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
fmt.print(now)
}
}