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 分前などを表示しますが、ユーザーの最後の訪問は昨日です。

4

3 に答える 3

1

ログアウト関数に単純な行を追加して、その瞬間の日付/時刻を記録します ( new Date()を使用) 。Java でSimpleDateFormatを使用して必要な形式に変換し、これをデータベース (ユーザーの情報を保存する場所) に新しいフィールドとして記録します。ユーザーが再度ログインしたときにこの情報を取得して表示します。

于 2012-07-18T08:22:33.950 に答える
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"
}
于 2012-07-18T10:47:44.060 に答える
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

}

}

タグのような名前空間「所有」

私を助けてくれたみんなに感謝

于 2012-07-18T17:07:21.550 に答える