Skip to content

Commit 08f84e3

Browse files
committed
增加毫秒数转字符串的扩展方法
1 parent 1f49e5a commit 08f84e3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGE_lOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### v1.2.3
44

55
+ 升级刷新库版本
6+
+ 增加毫秒数转字符串的扩展方法
67

78
### v1.2.2
89

utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
## DateExt
3737

38+
+ Long.format - 时间戳转字符串
3839
+ String.date - 字符串转时间Date
3940
+ Date.format - 时间Date转字符串
4041
+ Date.calendar - Date转Calendar

utils/src/main/java/com/catchpig/utils/ext/DateExt.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package com.catchpig.utils.ext
22

33
import java.text.SimpleDateFormat
4-
import java.util.*
4+
import java.util.Calendar
5+
import java.util.Date
56

67
const val DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"
8+
9+
/**
10+
* 毫秒数转化为时间字符串
11+
* @receiver Long
12+
* @param format String
13+
* @return String
14+
*/
15+
fun Long.format(format: String = DATE_FORMAT): String {
16+
return Date(this).format(format)
17+
}
18+
719
/**
820
* 字符串转化为时间
921
*/
10-
fun String.date(format:String=DATE_FORMAT):Date{
22+
fun String.date(format: String = DATE_FORMAT): Date {
1123
val dateFormat = SimpleDateFormat(format)
1224
return dateFormat.parse(this)!!
1325
}
@@ -22,7 +34,7 @@ fun Date.format(format: String = DATE_FORMAT): String {
2234
return dateFormat.format(this)
2335
}
2436

25-
fun Date.calendar():Calendar{
37+
fun Date.calendar(): Calendar {
2638
val calendar = Calendar.getInstance()
2739
calendar.time = this
2840
return calendar
@@ -31,7 +43,7 @@ fun Date.calendar():Calendar{
3143
/**
3244
* 获取年
3345
*/
34-
fun Date.year():Int{
46+
fun Date.year(): Int {
3547
return calendar().year()
3648
}
3749

@@ -59,13 +71,13 @@ fun Date.hour(): Int {
5971
/**
6072
* 获取分钟数
6173
*/
62-
fun Date.minute():Int{
74+
fun Date.minute(): Int {
6375
return calendar().minute()
6476
}
6577

6678
/**
6779
* 获取秒数
6880
*/
69-
fun Date.second():Int{
81+
fun Date.second(): Int {
7082
return calendar().second()
7183
}

0 commit comments

Comments
 (0)