package com.example.basecore.util import android.annotation.SuppressLint import java.text.SimpleDateFormat import java.util.* /** * author: dai * date : 2019-11-23 * desc: */ object DateUtils { @JvmStatic fun getCurrentDate(): String { val date = Date() return date.toString() } /** * 秒转时分秒 */ fun calDate(timeMs: Long): String { val seconds = timeMs % 60 val minutes = timeMs / 60 % 60 val hours = timeMs / 3600 return Formatter().format("%02d:%02d:%02d", hours, minutes, seconds).toString() } @SuppressLint("SimpleDateFormat") @JvmStatic fun getCurDate(): String { val sdf = SimpleDateFormat("yyyy-MM-dd") val date = Date(System.currentTimeMillis()) return sdf.format(date) } fun getCurDate(str: String): Date { return getCurDate(str, "yyyy年MM月dd日") } fun getCurDate(str: String, format: String): Date { val sdf = SimpleDateFormat(format) return sdf.parse(str) } /** * 将日期转换为时间戳 */ fun dateToStamp(str: String): String { val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒") val date = simpleDateFormat.parse(str) return date.time.toString() } /** * 将时间戳转换为时间 */ fun stampToDate(s: String, format: String?): String? { val res: String val simpleDateFormat = SimpleDateFormat(format) val lt: String = s val date = Date(lt) res = simpleDateFormat.format(date) return res } @SuppressLint("SimpleDateFormat") fun stampToDate(s: Long): String { val sdf = SimpleDateFormat("yyyy-MM-dd") val date = Date(s) return sdf.format(date) } fun getCurrentTime(): String { val sdf = SimpleDateFormat("HH:mm:ss") val date = Date(System.currentTimeMillis()) return sdf.format(date) } }