导入 v0.1.0 / v0.2.0 源码:五模块首版、交互重构、重复任务、任务分布
- 源码镜像 src/main(81 个文件):首页双模式、任务、项目、统计、设置 - 补齐 README / LICENSE / CHANGELOG / CONTRIBUTING / .gitignore - 不含构建配置、签名材料、编译产物与备份数据
This commit is contained in:
125
src/main/java/com/focusbox/app/util/Dates.kt
Normal file
125
src/main/java/com/focusbox/app/util/Dates.kt
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/** 时间格式化与"自然日"边界计算,全局只此一份,避免各处口径不一。 */
|
||||
object Dates {
|
||||
|
||||
private val keyFmt = SimpleDateFormat("yyyy-MM-dd", Locale.US)
|
||||
private val mdFmt = SimpleDateFormat("M/d", Locale.CHINA)
|
||||
private val mdFmt2 = SimpleDateFormat("M月d日", Locale.CHINA)
|
||||
private val hmFmt = SimpleDateFormat("HH:mm", Locale.CHINA)
|
||||
private val hmsFmt = SimpleDateFormat("HH:mm:ss", Locale.CHINA)
|
||||
|
||||
fun dayKey(ms: Long): String = keyFmt.format(Date(ms))
|
||||
|
||||
fun dayStart(ms: Long): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.set(Calendar.HOUR_OF_DAY, 0)
|
||||
c.set(Calendar.MINUTE, 0)
|
||||
c.set(Calendar.SECOND, 0)
|
||||
c.set(Calendar.MILLISECOND, 0)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun dayStartOfKey(key: String): Long = keyFmt.parse(key)?.time ?: 0L
|
||||
|
||||
fun todayStart(): Long = dayStart(System.currentTimeMillis())
|
||||
|
||||
/** 本周一 00:00(周一为一周起点,符合国内习惯) */
|
||||
fun weekStart(): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = todayStart()
|
||||
val dow = c.get(Calendar.DAY_OF_WEEK) // 1=周日
|
||||
val back = if (dow == Calendar.SUNDAY) 6 else dow - 2
|
||||
c.add(Calendar.DAY_OF_MONTH, -back)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun monthStart(): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = todayStart()
|
||||
c.set(Calendar.DAY_OF_MONTH, 1)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun addDays(ms: Long, days: Int): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.add(Calendar.DAY_OF_MONTH, days)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
/** 月末会按当月最后一天收敛(1月31日 + 1月 = 2月28/29日),不会溢出到下个月 */
|
||||
fun addMonths(ms: Long, months: Int): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.add(Calendar.MONTH, months)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
/** 12:30 这种时钟显示;不足 10 分钟显示 09:30 两位分 */
|
||||
fun clock(ms: Long): String {
|
||||
val total = (ms / 1000).coerceAtLeast(0)
|
||||
val m = total / 60
|
||||
val s = total % 60
|
||||
return String.format(Locale.US, "%02d:%02d", m, s)
|
||||
}
|
||||
|
||||
/** 45m / 2h10m / 12.5h,统计里统一用这个口径 */
|
||||
fun duration(seconds: Int): String {
|
||||
if (seconds < 60) return "${seconds}秒"
|
||||
val minutes = seconds / 60
|
||||
if (minutes < 60) return "${minutes}分钟"
|
||||
val h = minutes / 60
|
||||
val m = minutes % 60
|
||||
return if (m == 0) "${h}小时" else "${h}小时${m}分"
|
||||
}
|
||||
|
||||
/** 图表坐标轴用的短格式:2h / 45m */
|
||||
fun shortDuration(seconds: Int): String {
|
||||
if (seconds <= 0) return "0"
|
||||
val minutes = seconds / 60
|
||||
if (minutes < 60) return "${minutes}m"
|
||||
val h = minutes / 60f
|
||||
return if (h >= 10) "${h.toInt()}h" else String.format(Locale.US, "%.1fh", h)
|
||||
}
|
||||
|
||||
fun md(ms: Long): String = mdFmt.format(Date(ms))
|
||||
fun mdLong(ms: Long): String = mdFmt2.format(Date(ms))
|
||||
fun hm(ms: Long): String = hmFmt.format(Date(ms))
|
||||
fun hms(ms: Long): String = hmsFmt.format(Date(ms))
|
||||
|
||||
/** 周五 / 周一 */
|
||||
fun weekday(ms: Long): String {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
return arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")[
|
||||
(c.get(Calendar.DAY_OF_WEEK) - 1).coerceIn(0, 6)
|
||||
]
|
||||
}
|
||||
|
||||
/** 11月27日 周五 */
|
||||
fun dateWithWeek(ms: Long): String = "${mdLong(ms)} ${weekday(ms)}"
|
||||
|
||||
/** 今天 / 明天 / 已逾期 / 11月30日 */
|
||||
fun dueLabel(due: Long): String {
|
||||
if (due <= 0) return ""
|
||||
val today = todayStart()
|
||||
val d = dayStart(due)
|
||||
return when {
|
||||
d == today -> "今天"
|
||||
d == addDays(today, 1) -> "明天"
|
||||
d == addDays(today, -1) -> "昨天"
|
||||
d < today -> md(d)
|
||||
else -> md(d)
|
||||
}
|
||||
}
|
||||
|
||||
fun weekdayShort(index: Int): String =
|
||||
arrayOf("一", "二", "三", "四", "五", "六", "日")[index.coerceIn(0, 6)]
|
||||
}
|
||||
Reference in New Issue
Block a user