git il y a 5 ans
Parent
commit
112b7a0a18

+ 35 - 0
site/src/main/java/com/mooctest/crowd/site/data/vo/TechnicalArticlesVO.java

@@ -6,6 +6,10 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 @Data
 @NoArgsConstructor
@@ -16,6 +20,7 @@ public class TechnicalArticlesVO {
     private String  articlesUrl;
     private String author;
     private String source;
+    private String time_interval;
 
     public Long getId() {
         return id;
@@ -97,5 +102,35 @@ public class TechnicalArticlesVO {
         this.source=project.getSource();
         this.reading=project.getReading();
         this.createTime=project.getCreateTime();
+        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        try {
+            Date date = df.parse(df.format(project.getCreateTime()));
+            long[] times = getDistanceTimes(date);
+            if(times[0]>0){
+                this.time_interval=times[0]+"天前发布";
+            }else if(times[1]>0){
+                this.time_interval=times[1]+"小时前发布";
+            }else if(times[2]>0){
+                this.time_interval=times[2]+"分钟前发布";
+            }else{
+                this.time_interval=times[3]+"秒前发布";
+            }
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+    }
+    public static long[] getDistanceTimes(Date date) {
+        long day = 0;
+        long hour = 0;
+        long min = 0;
+        long sec = 0;
+        long diff;
+        diff = System.currentTimeMillis() - date.getTime();
+        day = diff / (24 * 60 * 60 * 1000);
+        hour = (diff / (60 * 60 * 1000) - day * 24);
+        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
+        sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
+        long[] times = {day, hour, min, sec};
+        return times;
     }
 }