时间线计算代码

发布时间:2020-01-12 发布网站:大佬教程
大佬教程收集整理的这篇文章主要介绍了时间线计算代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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,7,1))
        println timeLine(new Date(111,3,5,15))
        println timeLine(new Date(111,8))
        println timeLine(new Date(111,8,22,22))
        println timeLine(new Date(111,9,15,24))
        println timeLine(new Date(111,17,19,44))


    }
}

大佬总结

以上是大佬教程为你收集整理的时间线计算代码全部内容,希望文章能够帮你解决时间线计算代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ群:277859234,请注明来意。