package com.common.commonlib.utils import android.annotation.SuppressLint import java.text.SimpleDateFormat import java.util.* /** * 时间工具类 */ object DateUtils { /** * 如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式 */ const val DATE_TO_STRING_DETAIL_PATTERN = "yyyy-MM-dd HH:mm:ss" /** * 年-月-日 显示格式 * */ const val DATE_TO_STRING_SHORT_PATTERN = "yyyy.MM.dd" /** * 年-月-日 显示格式 * */ const val DATE_TO_STRING_LONG_PATTERN = "yyyy_MM_dd HH:mm:ss" /** * 60秒 */ const val SECONDS = 60 /** * 60分钟 */ const val MINUS = 60 /** * 得到现在时间 * * @return */ private fun getNow(): Date { return Date() } @SuppressLint("SimpleDateFormat") fun getNowTimeFormat(format: String): String { val formatter = SimpleDateFormat(format) return formatter.format(getNow()) } /** * 将int型的数据转化成 XX:XX:XX" 形式的字符串 */ fun getDurationTimeByInt(source: Int): String { var temp = source var h = 0 var m = 0 var s = 0 var result = "" if (temp >= SECONDS * MINUS) { h = temp / (SECONDS * MINUS) temp -= h * (SECONDS * MINUS) } if (temp >= SECONDS) { m = temp / SECONDS temp -= m * SECONDS } s = temp result = when { h > 0 -> { "$h:$m:$s\"" } m > 0 -> { "$m:$s\"" } else -> { "$s\"" } } return result } }