grails - Grails中的上次访问日期功能

标签 grails grails-2.0

我如何发布一个功能来向用户显示该站点中某人的最近访问?我从《 Grails in Action》一书中写了一些方法,但是它不能正常工作。

static String getNiceDate(Date date) {
    def now = new Date()
    def diff = Math.abs(now.time - date.time)
    final long second = 1000
    final long minute = second * 60
    final long hour = minute * 60
    final long day = hour * 24

    def niceTime = ""
    long calc = 0;
    calc = Math.floor(diff/day)
    if(calc) {
        niceTime += calc + " day" + (calc > 1 ? "s " : " ")
        diff %= day
    }

    calc = Math.floor(diff/hour)
    if(calc){
        niceTime += calc + " hour" + (calc > 1 ? "s " : " ")
        diff %= hour
    }

    calc = Math.floor(diff/minute)
    if(calc) {
        niceTime =+ calc + " minute" + (calc > 1 ? "s " : " ")
        diff %= minute
    }

    if(!niceTime) {
        niceTime = "Right now"
    } else {
        niceTime += (date.time > now.time) ? "from now" : "ago"
    }

    return niceTime

}

此代码有什么问题?每次显示类似20分钟前或10分钟前的内容,但用户的最后访问时间是昨天。

最佳答案

在注销功能中添加一条简单的行以记录该时刻的日期/时间。(
使用新的Date())。
使用Java中的SimpleDateFormat将其转换为所需的格式,并将其记录在数据库中(无论您存储用户信息的位置)作为新字段。
当用户重新登录并显示该信息时,请检索该信息!

关于grails - Grails中的上次访问日期功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11536604/

相关文章:

grails - Grails ACL独立 Controller Action

xml - 如何使用Grails用XML文件填充数据库

grails - Grails channel 安全性导致重定向循环

mysql - 如何在grails中保存超过2列的多个子值

session - 如何在grails中使用 session

unit-testing - Grails:在单元测试期间从服务中的数据模型对象获取IndexOutOfBoundsException

Grails 所需的验证器在 Safari 浏览器中不起作用?

multithreading - Gpars withExistingPool 错误 jsr166y.ForkJoinPool 未找到

Grails:一个数据库和一个以上的应用程序

grails - Grails Shiro LDAP用户/角色身份验证:如何/如何捕获和存储以供重用