时间差计算
/**
* 根据用户今日学习,关卡状态,最近学习时间来判断是否跳课
* @author yangxu
* @date 2020/9/28 5:53 下午
* @param isStudyToday 今日是否学习
* @param planStatus 关卡状态
* @param studyTime 最近学习时间
* @return java.lang.String
*/
def jumpClass(isStudyToday:String,planStatus:String,studyTime:String): String ={
val maxIntervalTime = 25*60*1000 //最大间隔为25分钟
val pattern = "yyyy-MM-dd HH:mm:ss"
val st = new SimpleDateFormat(pattern).parse(studyTime).getTime
val currentTime = Calendar.getInstance().getTimeInMillis
val intervalTime = currentTime - st
//判断是否大于25分钟
val flag = if( intervalTime > maxIntervalTime ) true else false
val tuple = Tuple3(isStudyToday, planStatus,flag)
tuple match {
case ("是","出勤",true) =>
"是"
case ("是","出勤",false) =>
"否"
case ("是","完课",_) =>
"否"
case ("否",_,_) =>
"否"
}
}
/**
* 获取距离传入时间字符串指定间隔的新时间字符串
* 如:
* date_interval(current_timestamp(),1,"hour")
* 2020-09-24T11:26:53.583+08:00 =》2020-09-24 12:26:52
*
* @author yangxu
* @date 2020/9/24 11:30 上午
* @param timeStamp 时间字符串
* @param num 间隔
* @param interval 间隔单位,hour,day,min,second
* @return java.lang.String
*/
def date_add_interval(timeStamp: String, num: Int, interval: String): String = {
val pattern = "yyyy-MM-dd HH:mm:ss"
val date = new SimpleDateFormat(pattern).parse(timeStamp)
val orgTime = date.getTime
var result: Long = 0l
try {
interval match {
case "hour" =>
result = orgTime + num * 60 * 60 * 1000
case "min" =>
result = orgTime + num * 60 * 1000
case "second" =>
result = orgTime + num * 1000
case "day" =>
result = orgTime + num * 24 * 60 * 60 * 1000
case _ =>
throw new Exception("请输入正确的时间间隔,day,min,second,hour")
}
} catch {
case exception: Exception =>
exception.printStackTrace()
}
new SimpleDateFormat(pattern).format(result)
}