我如何发布一个功能来向用户显示某个人在网站上的最后一次访问?我从 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 分钟前的内容,但用户的最后一次访问是昨天。