mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-14 21:26:41 +00:00
perf(novel-front): 优化评论时间显示
This commit is contained in:
@ -106,6 +106,47 @@ public class DateUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将日期格式化成"多久之前"的格式
|
||||||
|
* */
|
||||||
|
public static String formatTimeAgo(Date date){
|
||||||
|
if (date == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
long now = new Date().getTime();
|
||||||
|
long then = date.getTime();
|
||||||
|
|
||||||
|
long diff = now - then;
|
||||||
|
|
||||||
|
if (diff < 0) {
|
||||||
|
// 未来时间
|
||||||
|
DateUtil.formatDate(date, DateUtil.DATE_TIME_PATTERN);
|
||||||
|
}
|
||||||
|
|
||||||
|
long seconds = diff / 1000;
|
||||||
|
long minutes = seconds / 60;
|
||||||
|
long hours = minutes / 60;
|
||||||
|
long days = hours / 24;
|
||||||
|
long months = days / 30;
|
||||||
|
long years = months / 12;
|
||||||
|
|
||||||
|
if (seconds < 60) {
|
||||||
|
return "刚刚";
|
||||||
|
} else if (minutes < 60) {
|
||||||
|
return minutes + "分钟前";
|
||||||
|
} else if (hours < 24) {
|
||||||
|
return hours + "小时前";
|
||||||
|
} else if (days < 30) {
|
||||||
|
return days + "天前";
|
||||||
|
} else if (months < 12) {
|
||||||
|
return months + "个月前";
|
||||||
|
} else {
|
||||||
|
return years + "年前";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(formatDate(getYesterday(),DATE_TIME_PATTERN));
|
System.out.println(formatDate(getYesterday(),DATE_TIME_PATTERN));
|
||||||
System.out.println(formatDate(getDateStartTime(getYesterday()),DATE_TIME_PATTERN));
|
System.out.println(formatDate(getDateStartTime(getYesterday()),DATE_TIME_PATTERN));
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.java2nb.novel.core.serialize;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.java2nb.novel.core.utils.DateUtil;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class TimeAgoFormatSerialize extends JsonSerializer<Date> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Date s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
if (s != null) {
|
||||||
|
jsonGenerator.writeString(DateUtil.formatTimeAgo(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,8 @@
|
|||||||
package com.java2nb.novel.vo;
|
package com.java2nb.novel.vo;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import com.java2nb.novel.core.serialize.CommentUserNameSerialize;
|
import com.java2nb.novel.core.serialize.CommentUserNameSerialize;
|
||||||
import com.java2nb.novel.entity.BookComment;
|
import com.java2nb.novel.core.serialize.TimeAgoFormatSerialize;
|
||||||
import com.java2nb.novel.entity.BookCommentReply;
|
import com.java2nb.novel.entity.BookCommentReply;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@ -20,7 +19,7 @@ public class BookCommentReplyVO extends BookCommentReply {
|
|||||||
|
|
||||||
private String createUserPhoto;
|
private String createUserPhoto;
|
||||||
|
|
||||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
@JsonSerialize(using = TimeAgoFormatSerialize.class)
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
private Long likesCount;
|
private Long likesCount;
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package com.java2nb.novel.vo;
|
package com.java2nb.novel.vo;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import com.java2nb.novel.core.serialize.CommentUserNameSerialize;
|
import com.java2nb.novel.core.serialize.CommentUserNameSerialize;
|
||||||
|
import com.java2nb.novel.core.serialize.TimeAgoFormatSerialize;
|
||||||
|
import com.java2nb.novel.core.utils.DateUtil;
|
||||||
import com.java2nb.novel.entity.BookComment;
|
import com.java2nb.novel.entity.BookComment;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@ -19,13 +20,17 @@ public class BookCommentVO extends BookComment {
|
|||||||
|
|
||||||
private String createUserPhoto;
|
private String createUserPhoto;
|
||||||
|
|
||||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
@JsonSerialize(using = TimeAgoFormatSerialize.class)
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
private Long likesCount;
|
private Long likesCount;
|
||||||
|
|
||||||
private Long unLikesCount;
|
private Long unLikesCount;
|
||||||
|
|
||||||
|
public String getCreateTimeFormat() {
|
||||||
|
return DateUtil.formatTimeAgo(getCreateTime());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString();
|
return super.toString();
|
||||||
|
@ -114,7 +114,7 @@
|
|||||||
th:text="${comment.location} + '读者'"></span></span></li>
|
th:text="${comment.location} + '读者'"></span></span></li>
|
||||||
<li class="dec" th:utext="${comment.commentContent}"></li>
|
<li class="dec" th:utext="${comment.commentContent}"></li>
|
||||||
<li class="other cf"><span class="time fl"
|
<li class="other cf"><span class="time fl"
|
||||||
th:text="${#calendars.format(comment.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span><span
|
th:text="${comment.createTimeFormat}"></span><span
|
||||||
class="fr"><a th:href="'javascript:toggleCommentUnLike(\''+${comment.id}+'\')'" onclick="javascript:;" class="zan"
|
class="fr"><a th:href="'javascript:toggleCommentUnLike(\''+${comment.id}+'\')'" onclick="javascript:;" class="zan"
|
||||||
style="padding-left: 10px">踩<i class="num" th:id="'unLikeCount'+${comment.id}">([[${comment.unLikesCount}]])</i></a></span><span
|
style="padding-left: 10px">踩<i class="num" th:id="'unLikeCount'+${comment.id}">([[${comment.unLikesCount}]])</i></a></span><span
|
||||||
class="fr"><a th:href="'javascript:toggleCommentLike(\''+${comment.id}+'\')'" class="zan"
|
class="fr"><a th:href="'javascript:toggleCommentLike(\''+${comment.id}+'\')'" class="zan"
|
||||||
|
Reference in New Issue
Block a user