# 时间线计算代码

```class DateTimeUtil {
static def timeLine = {
def subMinute = delegate.upint(Calendar.instance.timeInMillis.minus(it.time), 1000 * 60)
def oneHour = 60
def oneDay = oneHour * 24
def oneWeek = oneDay * 7
def oneMonth = oneDay * 30
def oneYear = oneDay * 365
def inhour = 0..<oneHour
def inday = oneHour..<oneDay
def inweek = oneDay..<oneWeek
def inmonth = oneWeek..<oneMonth
def inyear = oneMonth..<oneYear
def outyear = oneYear..< Integer.MAX_VALUE

switch (subMinute) {
case inhour:
"\${subMinute}分钟前"
break
case inday:
"\${subMinute.intdiv(oneHour)}小时前"
break
case inweek:
"\${subMinute.intdiv(oneDay)}天前"
break
case inmonth:
"\${subMinute.intdiv(oneWeek)}周前"
break
case inyear:
"\${subMinute.intdiv(oneMonth)}月前"
break
case outyear:
"\${subMinute.intdiv(oneYear)}年前"
break
default:
"E=mc2"
}

}

static def upint = { A, B ->
/**
向上取整算法
http://hi.baidu.com/chinakite/blog/item/3d02f01f339ac3f1e1fe0bc0.html
UP(A/B) = int( (A+B-1)/B )
*/
A>0?A.plus(B).minus(1).intdiv(B).toInteger():1
}

static void main(args) {
println Calendar.instance.timeInMillis
println new Date().time

println timeLine(new Date(108, 6, 1))
println timeLine(new Date(110, 6, 1))
println timeLine(new Date(110, 7, 1))
println timeLine(new Date(111, 3, 1))
println timeLine(new Date(111, 5, 1))
println timeLine(new Date(111, 5, 15))
println timeLine(new Date(111, 6, 1))
println timeLine(new Date(111, 6, 8))
println timeLine(new Date(111, 6, 8, 22, 22))
println timeLine(new Date(111, 6, 9, 15, 24))
println timeLine(new Date(111, 6, 9, 17, 24))
println timeLine(new Date(111, 6, 9, 19, 44))

}
}
```

0人收藏

0

0

1. TiuVe2 发表 2018-09-04 14:15:19 按钮条件逻辑配置化的可选技术方案
2. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
3. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
4. 博主 发表 2018-04-25 06:46:00 Groovy Grape
5. 博主 发表 2018-04-26 12:23:00 Groovy Ratpack
6. 博主 发表 2018-04-26 12:23:00 Groovy Ratpack
7. 博主 发表 2018-04-25 06:46:00 Groovy Grape
8. 博主 发表 2018-02-07 09:05:17 IDEA 通过数据库生成 Nutz 实体类
9. TiuVe2 发表 2018-01-06 13:23:06 使用 yaml+groovy 实现 Java 代码可配置化
10. 博主 发表 2015-10-02 00:00:00 精通 Groovy
11. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
12. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法