riku
2022-01-20 5a0fff8095cd5356f57c181b7e7b820e0f7efacf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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 {
        try {
            return SimpleDateFormat("yyyy-MM", Locale.getDefault()).parse(str)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return Date()
    }
 
    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!")
        }
    }
}