Android之判断时间戳是不是今天
1 需求
判断时间戳是不是今天
2、工具代码
/**
* 获取每日0点时间
* @return
*/
fun getTodayTime(timeStamp: Long): Long {
val cal = Calendar.getInstance()
cal.timeInMillis = timeStamp
cal.set(Calendar.HOUR_OF_DAY, 0)
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MINUTE, 0)
cal.set(Calendar.MILLISECOND, 0)
return cal.timeInMillis
}
fun isToday(time: Long): Boolean {
val sdf = SimpleDateFormat("yyyy-MM-dd")
val date = Date(time)
return try {
val old = sdf.parse(sdf.format(date))
val now = sdf.parse(sdf.format(Date()))
val oldTime = old.time
val nowTime = now.time
还没有评论,来说两句吧...