lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
package com.driver.mask_talk.utils
 
import java.text.SimpleDateFormat
import java.util.*
 
/**
时间戳转换默认格式yyyy-MM-dd HH:mm:ss
 */
fun Long.toDefaultTime(): String {
//    val f = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    val f = SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.CHINA)
    return f.format(Date(this))
}
 
fun Long.toTime(format: String = "yyyy-MM-dd HH:mm:ss"): String {
    val f = SimpleDateFormat(format, Locale.CHINA)
    return f.format(Date(this))
}
 
fun Long.toMDHMTime(): String {
    val f = SimpleDateFormat("MM月dd日 HH:mm", Locale.CHINA)
    return f.format(Date(this))
}
 
/**
 * 两个时间戳间隔字符
 * @param nowTime 当前时间,默认读取系统时间
 */
fun Long.interval(nowTime: Long = System.currentTimeMillis()): String {
    val desc: String
    val d = Date(this)
    val n = Date(nowTime)
    val delay = n.time - d.time
    val secondsOfHour = (60 * 60).toLong()
    val secondsOfDay = secondsOfHour * 24
    val secondsOfTwoDay = secondsOfDay * 2
    val secondsOfThreeDay = secondsOfDay * 3
    // 相差的秒数
    val delaySeconds = delay / 1000
    desc = when {
//        delaySeconds < 10 -> "刚刚"
//        delaySeconds <= 60 -> delaySeconds.toString() + "秒前"
//        delaySeconds < secondsOfHour -> (delaySeconds / 60).toString() + "分前"
        delaySeconds < secondsOfDay ->
//            (delaySeconds / 60 / 60).toString() + "小时前"
            this.toTime("HH:mm")
//        delaySeconds < secondsOfTwoDay -> "一天前"
//        delaySeconds < secondsOfThreeDay -> "两天前"
        else -> this.toDefaultTime()
    }
    return desc
}
 
/**
 * 2个时间间隔天数
 */
fun Long.intervalTime(nowTime: Long = System.currentTimeMillis()): Int {
    val c1 = Calendar.getInstance()
    c1.timeInMillis = this
    c1.set(Calendar.HOUR_OF_DAY, 0)
    c1.set(Calendar.MINUTE, 0)
    c1.set(Calendar.SECOND, 0)
    val time1 = c1.timeInMillis
 
    val c2 = Calendar.getInstance()
    c2.timeInMillis = nowTime
    c2.set(Calendar.HOUR_OF_DAY, 0)
    c2.set(Calendar.MINUTE, 0)
    c2.set(Calendar.SECOND, 0)
    val time2 = c2.timeInMillis
    val delay = time2 - time1
    val count = delay / 1000 / 60 / 60 / 24
    return if (count > 0) count.toInt() else 0
}
 
/**
 * 间隔时间转换为分秒。
 * @param nowTime 当前时间,需要比较的时间
 */
fun Long.intervalMMSS(nowTime: Long): String {
    val d = Date(this)
    val n = Date(nowTime)
    val delay = n.time - d.time
    return delay.intervalMMSS()
}
 
/**
 * 间隔时间转换为时分秒。
 * @param nowTime 当前时间,需要比较的时间,大的那个值,不然是负数
 */
fun Long.intervalHHMMSS(nowTime: Long): String {
    val d = Date(this)
    val n = Date(nowTime)
    val delay = n.time - d.time
    return delay.intervalHHMMSS()
}
 
fun Long.intervalHHMMSS(): String {
    val desc: String
    val secondsOfHour = (60 * 60).toLong()
    val secondsOfDay = secondsOfHour * 24
    val secondsOfTwoDay = secondsOfDay * 2
    val secondsOfThreeDay = secondsOfDay * 3
    // 相差的秒数
    val delaySeconds = this / 1000
    desc = when {
        delaySeconds < 60 -> String.format(Locale.CHINA, "00:00:%02d", delaySeconds)
        delaySeconds < secondsOfHour -> String.format(
            Locale.CHINA,
            "00:%02d:%02d",
            delaySeconds / 60,
            delaySeconds % 60
        )
        delaySeconds < secondsOfDay -> String.format(
            Locale.CHINA,
            "%02d:%02d:%02d",
            delaySeconds / 60 / 60,
            delaySeconds / 60 % 60,
            delaySeconds % 60
        )
//        delaySeconds < secondsOfTwoDay -> "一天"
//        delaySeconds < secondsOfThreeDay -> "两天"
        else -> String.format(
            Locale.CHINA,
            "%02d:%02d:%02d",
            delaySeconds / 60 / 60,
            delaySeconds / 60 % 60,
            delaySeconds % 60
        )
    }
    return desc
}
 
/**
 * 间隔时间转换为分秒。
 * @return
 */
fun Long.intervalMMSS(): String {
    val desc: String
    val secondsOfHour = (60 * 60).toLong()
    val secondsOfDay = secondsOfHour * 24
    val secondsOfTwoDay = secondsOfDay * 2
    val secondsOfThreeDay = secondsOfDay * 3
    // 相差的秒数
    val delaySeconds = this / 1000
    desc = when {
        delaySeconds < 60 -> String.format(Locale.CHINA, "00:%02d", delaySeconds)
        delaySeconds < secondsOfHour -> String.format(
            Locale.CHINA,
            "%02d:%02d",
            delaySeconds / 60,
            delaySeconds % 60
        )
        delaySeconds < secondsOfDay -> (delaySeconds / 60 / 60).toString() + "小时"
        delaySeconds < secondsOfTwoDay -> "昨天"
        delaySeconds < secondsOfThreeDay -> "前天"
        else -> toMDHMTime()
    }
    return desc
}
 
/**
 * 时间戳转星期
 */
fun Long.toWeek(pre: String = "星期"): String {
    val myDate: Int
    val week: String
    val cd = Calendar.getInstance()
    cd.time = Date(this)
    myDate = cd.get(Calendar.DAY_OF_WEEK)
    // 获取指定日期转换成星期几
    week = when (myDate) {
        1 -> String.format("%s日", pre)
        2 -> String.format("%s一", pre)
        3 -> String.format("%s二", pre)
        4 -> String.format("%s三", pre)
        5 -> String.format("%s四", pre)
        6 -> String.format("%s五", pre)
        7 -> String.format("%s六", pre)
        else -> ""
    }
    return week
}
 
///**
// * yyyy-MM-dd HH:mm 型时间字符串1是否小于时间2
// */
//fun String?.isBefore(time:String?):Boolean{
//    if (this == null || time == null) {
//        return false
//    }
//    val f = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
//    try {
//        val d1 = f.parse(this)
//        val t1 = d1.time
//        val d2 = f.parse(time)
//        val t2 = d2.time
//        sysErr(""+t1+"-------"+t2)
//        return t1<t2
//    }catch (e:ParseException){
//        e.printStackTrace()
//    }
//    return false
//}
 
fun String?.parserTime(format: String = "yyyy-MM-dd HH:mm:ss"): Long {
    if (this == null) {
        return 0
    }
 
    val ft = if (format.isEmpty()) {
        "yyyy-MM-dd HH:mm:ss"
    } else {
        format
    }
    val f = SimpleDateFormat(ft, Locale.CHINA)
    return try {
        val date = f.parse(this)
        date.time
    } catch (e: Exception) {
        e.printStackTrace()
        0L
    }
}
 
fun String?.parserDate(format: String = "yyyy-MM-dd HH:mm:ss"): Date {
    if (this == null) {
        return Date()
    }
 
    val ft = if (format.isEmpty()) {
        "yyyy-MM-dd HH:mm:ss"
    } else {
        format
    }
    val f = SimpleDateFormat(ft, Locale.CHINA)
    return try {
        f.parse(this)
    } catch (e: Exception) {
        e.printStackTrace()
        Date()
    }
}
 
fun Long.timeDay(nowTime: Long = System.currentTimeMillis()): String {
    val desc: String
    val c = Calendar.getInstance()
    c.timeInMillis = nowTime
    val nowDay = c.get(Calendar.DAY_OF_MONTH)
    c.set(Calendar.DAY_OF_MONTH, nowDay + 1)
    val nextDay = c.timeInMillis
    c.set(Calendar.DAY_OF_MONTH, nowDay + 2)
    val next2Day = c.timeInMillis
    desc = when {
        this.toTime("yyyy-MM-dd") == nowTime.toTime("yyyy-MM-dd") -> "今天"
        this.toTime("yyyy-MM-dd") == nextDay.toTime("yyyy-MM-dd") -> "明天"
        this.toTime("yyyy-MM-dd") == next2Day.toTime("yyyy-MM-dd") -> "后天"
        else -> ""
    }
    return "$desc ${this.toMDHMTime()}"
}
 
fun Long.toFriendlyTime(nowTime: Long = System.currentTimeMillis()): String {
    val desc: String
    val d = Date(this)
    val n = Date(nowTime)
    val delay = n.time - d.time
    val secondsOfHour = (60 * 60).toLong()
    val secondsOfDay = secondsOfHour * 24
    // 相差的秒数
    val delaySeconds = delay / 1000
    desc = when {
        delaySeconds < 60 -> "刚刚"
        delaySeconds < secondsOfHour -> (delaySeconds / 60).toString() + "分钟前"
        delaySeconds < secondsOfDay -> (delaySeconds / 60 / 60).toString() + "小时前"
        delaySeconds < secondsOfDay * 3 -> (delaySeconds / secondsOfDay).toString() + "天前";
        else -> this.toMDHMTime()
    }
    return desc
}