pull/83/head
kunfei 5 years ago
parent 660cedea68
commit 93d4bba524
  1. 30
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -41,16 +41,18 @@ object StringUtils {
//将时间转换成日期 //将时间转换成日期
fun dateConvert(time: Long, pattern: String): String { fun dateConvert(time: Long, pattern: String): String {
val date = Date(time) val date = Date(time)
@SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) @SuppressLint("SimpleDateFormat")
val format = SimpleDateFormat(pattern)
return format.format(date) return format.format(date)
} }
//将日期转换成昨天、今天、明天 //将日期转换成昨天、今天、明天
fun dateConvert(source: String, pattern: String): String { fun dateConvert(source: String, pattern: String): String {
@SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) @SuppressLint("SimpleDateFormat")
val format = SimpleDateFormat(pattern)
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
try { try {
val date = format.parse(source) val date = format.parse(source) ?: return ""
val curTime = calendar.timeInMillis val curTime = calendar.timeInMillis
calendar.time = date calendar.time = date
//将MISC 转换成 sec //将MISC 转换成 sec
@ -62,13 +64,18 @@ object StringUtils {
//如果没有时间 //如果没有时间
if (oldHour == 0) { if (oldHour == 0) {
//比日期:昨天今天和明天 //比日期:昨天今天和明天
if (difDate == 0L) { return when {
return "今天" difDate == 0L -> {
} else if (difDate < DAY_OF_YESTERDAY) { "今天"
return "昨天" }
} else { difDate < DAY_OF_YESTERDAY -> {
@SuppressLint("SimpleDateFormat") val convertFormat = SimpleDateFormat("yyyy-MM-dd") "昨天"
return convertFormat.format(date) }
else -> {
@SuppressLint("SimpleDateFormat")
val convertFormat = SimpleDateFormat("yyyy-MM-dd")
convertFormat.format(date)
}
} }
} }
@ -78,7 +85,8 @@ object StringUtils {
difHour < HOUR_OF_DAY -> difHour.toString() + "小时前" difHour < HOUR_OF_DAY -> difHour.toString() + "小时前"
difDate < DAY_OF_YESTERDAY -> "昨天" difDate < DAY_OF_YESTERDAY -> "昨天"
else -> { else -> {
@SuppressLint("SimpleDateFormat") val convertFormat = SimpleDateFormat("yyyy-MM-dd") @SuppressLint("SimpleDateFormat")
val convertFormat = SimpleDateFormat("yyyy-MM-dd")
convertFormat.format(date) convertFormat.format(date)
} }
} }

Loading…
Cancel
Save