Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit e4b71a4

Browse files
committed
Use Kotlin way in addTimeIntervals
1 parent 2e7ef9f commit e4b71a4

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/iii_conventions/MyDateUtil.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import java.util.*
55

66
fun MyDate.nextDay() = addTimeIntervals(DAY, 1)
77

8-
fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate {
9-
val c = Calendar.getInstance()
10-
c.set(year, month, dayOfMonth)
11-
when (timeInterval) {
12-
TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number)
13-
TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number)
14-
TimeInterval.YEAR -> c.add(Calendar.YEAR, number)
15-
}
16-
return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE))
8+
fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int) = Calendar.getInstance().run {
9+
set(year, month, dayOfMonth)
10+
add(when (timeInterval) {
11+
TimeInterval.DAY -> Calendar.DAY_OF_MONTH
12+
TimeInterval.WEEK -> Calendar.WEEK_OF_MONTH
13+
TimeInterval.YEAR -> Calendar.YEAR
14+
}, number)
15+
MyDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE))
1716
}

0 commit comments

Comments
 (0)