我如何发布一个功能来向用户显示网站上某个人的最后一次访问?我从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分钟前,但用户上次访问是昨天。
答案 0 :(得分:1)
在注销功能中添加一个简单的行来记录该时刻的日期/时间。( 使用新日期())。 使用java中的SimpleDateFormat将其转换为您想要的格式,并将其作为新字段记录在数据库中(无论您将用户的信息存储在何处)。 当用户重新登录并显示它时检索此信息!
答案 1 :(得分:1)
以这种方式尝试:
/**
* Returns differnce b/w new Date and old date as Map holding difference in years, weeks, days, hrs, mins & secs
*/
public static Map getDiffernceInDates(Date oldDate, Date newDate = new Date()) {
Long difference = newDate.time - oldDate.time
Map diffMap =[:]
difference = difference / 1000
diffMap.seconds = difference % 60
difference = (difference - diffMap.seconds) / 60
diffMap.minutes = difference % 60
difference = (difference - diffMap.minutes) / 60
diffMap.hours = difference % 24
difference = (difference - diffMap.hours) / 24
diffMap.years = (difference / 365).toInteger()
if(diffMap.years)
difference = (difference) % 365
diffMap.days = difference % 7
diffMap.weeks = (difference - diffMap.days) / 7
return diffMap
}
详细显示日期差异的taglib如下:
def timeDiffInDetail = {attrs ->
Map diff = DateUtil.getDiffernceInDates(attrs.oldDate, attrs.newDate ?: new Date())
String result = diff.years ? diff.years + " years " : ""
result += diff.weeks ? diff.weeks + " weeks " : ""
result += diff.days ? diff.days + " days " : ""
result += diff.hours ? diff.hours + " hours " : ""
result += diff.minutes ? diff.minutes + " minutes ago" : ""
if (result)
out << result
else
out << " 0 minutes ago"
}
答案 2 :(得分:0)
我做了一些更改,以前的代码,它工作得很好。如果有人需要它,这是一个有效的版本:
class LastVisitTagLib {
static namespace = "own"
def lastVisit = {attrs ->
def date = attrs.date
def niceDate = getNiceDate(date)
out << niceDate
}
static String getNiceDate(Date date) {
def now = new Date()
def diff = Math.abs(now.getTime() - date.getTime())
long second = 1000
long minute = 1000 * 60
long hour = minute * 60
long day = hour * 24
def niceTime = ""
long calc = 0L;
calc = Math.floor(diff / day)
if (calc > 0) {
niceTime += calc + " day" + (calc > 1 ? "s " : " ")
diff = diff % day
}
calc = Math.floor(diff / hour)
if (calc > 0) {
niceTime += calc + " hour" + (calc > 1 ? "s " : " ")
diff = diff % hour
}
calc = Math.floor(diff / minute)
if (calc > 0) {
niceTime += calc + " minute" + (calc > 1 ? "s " : " ")
diff = diff % minute
}
if (niceTime.length() == 0) {
niceTime = "Right now"
} else {
niceTime += (date.getTime() > now.getTime()) ? "from now" : "ago"
}
return niceTime
}
}
对于像这样的标记,名称空间“拥有”
感谢所有帮助过我的人