package cn.flightfeather.thirdappmodule.util
|
|
import java.lang.Exception
|
import java.text.SimpleDateFormat
|
import java.util.*
|
import kotlin.math.round
|
|
/**
|
* @author riku
|
* Date: 2019/4/9
|
* 通用工具类
|
*/
|
object DateUtil {
|
|
private const val MILLIS_LIMIT = 1000.0
|
|
private const val SECONDS_LIMIT = 60 * MILLIS_LIMIT
|
|
private const val MINUTES_LIMIT = 60 * SECONDS_LIMIT
|
|
private const val HOURS_LIMIT = 24 * MINUTES_LIMIT
|
|
private const val YESTERDAY_LIMIT = 2 * HOURS_LIMIT
|
|
private const val DAYS_LIMIT = 30 * HOURS_LIMIT
|
|
private val weekDays = listOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
|
|
/**
|
* yyyy-MM-dd HH:mm:ss
|
*/
|
fun getDateStr(date: Date?, chinese: Boolean = false): String {
|
if (date == null) {
|
return ""
|
}
|
try {
|
return if (chinese) {
|
SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.getDefault()).format(date)
|
} else {
|
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date)
|
}
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* HH:mm:ss
|
*/
|
fun getTimeStr(date: Date?): String {
|
try {
|
return getDateStr(date).substring(11)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* HH:mm
|
*/
|
fun getHourMinStr(date: Date?): String {
|
try {
|
return getDateStr(date).substring(11, 16)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* YYYY
|
*/
|
fun getYearStr(date: Date?): String {
|
try {
|
return getDateStr(date).substring(0, 4)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* dd
|
*/
|
fun getDayStr(date: Date?): String {
|
try {
|
return getDateStr(date).substring(8, 10)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* yyyy-MM
|
*/
|
fun getYearMonthStr(date: Date?, chinese: Boolean = false): String {
|
try {
|
return getDateStr(date,chinese).substring(0, if (chinese) 8 else 7)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* MM-dd
|
*/
|
fun getMonthDayStr(date: Date?, chinese: Boolean = false): String {
|
try {
|
return getDateStr(date, chinese).substring(5, if (chinese) 11 else 10)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* yyyy-MM-dd
|
*/
|
fun getYearMonthDayStr(date: Date?, chinese: Boolean = false): String {
|
try {
|
return getDateStr(date, chinese).substring(0, if (chinese) 11 else 10)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* yyyy-MM-dd HH:mm
|
*/
|
fun getYearToMinStr(date: Date?): String {
|
try {
|
return getDateStr(date).substring(0, 16)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return ""
|
}
|
|
/**
|
* 获取星期
|
*/
|
fun getWeekDay(date: Date?): String {
|
val index = Calendar.getInstance(Locale.getDefault()).apply {
|
date?.let { time = it }
|
}.get(Calendar.DAY_OF_WEEK) - 1
|
return weekDays[index]
|
}
|
|
fun parseYearMonth(str: String?): Date? {
|
return try {
|
SimpleDateFormat("yyyy-MM", Locale.getDefault()).parse(str)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
null
|
}
|
}
|
|
fun parseYearToMin(str: String): Date {
|
try {
|
return SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).parse(str)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return Date()
|
}
|
|
fun parseYearToHour(str: String): Date {
|
try {
|
return SimpleDateFormat("yyyy-MM-dd HH", Locale.getDefault()).parse(str)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return Date()
|
}
|
|
/**
|
* 获取月初
|
*/
|
fun getMonthFirstDay(date: Date?): Date {
|
return Calendar.getInstance().apply { time = date }
|
.apply {
|
set(Calendar.DAY_OF_MONTH, 1)
|
set(Calendar.HOUR_OF_DAY, 0)
|
set(Calendar.MINUTE, 0)
|
set(Calendar.SECOND, 0)
|
}.time
|
}
|
|
/**
|
* 获取下月初
|
*/
|
fun getNextMonthFirstDay(date: Date?): Date {
|
return Calendar.getInstance().apply { time = date }
|
.apply {
|
set(Calendar.DAY_OF_MONTH, 1)
|
set(Calendar.HOUR_OF_DAY, 0)
|
set(Calendar.MINUTE, 0)
|
set(Calendar.SECOND, 0)
|
val m = get(Calendar.MONTH)
|
if (m == 11) {
|
add(Calendar.YEAR, 1)
|
set(Calendar.MONTH, 0)
|
} else {
|
add(Calendar.MONTH, 1)
|
}
|
}.time
|
}
|
|
/**
|
* 按给定的月份以及周期,得出当前月份所在周期的开始月(例,一年以季度分为4段时间,即周期为3月,则5月所在周期开始月为4月)
|
*/
|
fun getStartMonthByPeriod(currentMon: Int, period: Int):Int? {
|
return if (period in 1..12 && currentMon in 1..12) {
|
val m = currentMon % period
|
val r = currentMon / period
|
period * r + if (m == 0) 1 - period else 1
|
} else {
|
null
|
}
|
}
|
|
/**
|
* 按给定的月份以及周期,得出当前月份所在周期的开始月
|
*/
|
fun getEndMonthByPeriod(currentMon: Int, period: Int): Int? {
|
val s = getStartMonthByPeriod(currentMon, period)
|
return if (s != null) {
|
s + period - 1
|
} else {
|
null
|
}
|
}
|
|
/**
|
* 获取语音时间格式化 1‘23’‘
|
* @param sec 秒
|
*/
|
fun getAudioTimeStr(sec: Int):String {
|
return when (sec * 1000) {
|
in 0 until SECONDS_LIMIT.toInt() -> "$sec\""
|
in SECONDS_LIMIT.toInt() until MINUTES_LIMIT.toInt() -> "${sec / 60}\'${sec % 60}\""
|
else -> ""
|
}
|
}
|
|
/**
|
* 获取时间格式化
|
*/
|
fun getNewsTimeStr(date: Date?, chinese: Boolean = false): String {
|
if (date == null) {
|
return ""
|
}
|
val subTime = Date().time - date.time
|
|
val now = Calendar.getInstance().apply {
|
time = Date()
|
set(Calendar.HOUR_OF_DAY, 0)
|
set(Calendar.MINUTE, 0)
|
set(Calendar.SECOND, 0)
|
set(Calendar.MILLISECOND, 0)
|
}
|
val objDate = Calendar.getInstance().apply {
|
time = date
|
set(Calendar.HOUR_OF_DAY, 0)
|
set(Calendar.MINUTE, 0)
|
set(Calendar.SECOND, 0)
|
set(Calendar.MILLISECOND, 0)
|
}
|
val thisYear = now.get(Calendar.YEAR)
|
val thisMonth = now.get(Calendar.MONTH)
|
val thisDay = now.get(Calendar.DAY_OF_MONTH)
|
|
val objYear = objDate.get(Calendar.YEAR)
|
val objMonth = objDate.get(Calendar.MONTH)
|
val objDay = objDate.get(Calendar.DAY_OF_MONTH)
|
|
when {
|
now.time == objDate.time -> return when {
|
subTime < MILLIS_LIMIT -> "刚刚"
|
subTime < SECONDS_LIMIT -> round(subTime / MILLIS_LIMIT).toString() + " " + "秒前"
|
subTime < MINUTES_LIMIT -> round(subTime / SECONDS_LIMIT).toString() + " " + "分钟前"
|
subTime < HOURS_LIMIT -> round(subTime / MINUTES_LIMIT).toString() + " " + "小时前"
|
else -> getYearMonthStr(date, chinese)
|
}
|
objYear < thisYear -> return getYearMonthStr(date, chinese)
|
objMonth < thisMonth -> return getMonthDayStr(date, chinese)
|
else -> return if (thisDay - objDay == 1) {
|
"昨天"
|
} else {
|
getMonthDayStr(date, chinese)
|
}
|
}
|
}
|
|
/**
|
* 获取格式化的时间差
|
* @param from 开始时间
|
* @param to 结束时间
|
* @return 返回开始时间与结束时间的时间差,例3天、1天、24小时、50分钟等,时间单位只取 年、月、天、小时、分钟
|
*/
|
fun getTimeDiffFormat(from: Date?, to: Date?): String? {
|
if (from == null || to == null) {
|
return null
|
}
|
|
val subTime = to.time - from.time
|
|
//保证from 小于等于 to
|
if (subTime < 0) {
|
return null
|
}
|
|
val fromTime = Calendar.getInstance().apply {
|
time = from
|
}
|
val toTime = Calendar.getInstance().apply {
|
time = to
|
}
|
|
val fromYear = fromTime.get(Calendar.YEAR)
|
val fromMonth = fromTime.get(Calendar.MONTH)
|
val fromDay = fromTime.get(Calendar.DAY_OF_YEAR)
|
|
val toYear = toTime.get(Calendar.YEAR)
|
val toMonth = toTime.get(Calendar.MONTH)
|
val toDay = toTime.get(Calendar.DAY_OF_YEAR)
|
|
return when {
|
fromYear < toYear -> "${toYear - fromYear}" + "年"
|
fromMonth < toMonth -> "${toMonth - fromMonth}" + "月"
|
fromDay < toDay -> "${toDay - fromDay}" + "天"
|
fromDay == toDay -> when {
|
subTime < MINUTES_LIMIT -> round(subTime / SECONDS_LIMIT).toInt().toString() + " " + "分钟"
|
subTime < HOURS_LIMIT -> round(subTime / MINUTES_LIMIT).toInt().toString() + " " + "小时"
|
else -> throw IllegalStateException("this branch is impossible!")
|
}
|
else -> throw IllegalStateException("this branch is impossible!")
|
}
|
}
|
}
|