티스토리 뷰

게시글을 올린 후 게시글 노출이 어느정도 되었는지 시간에 따라 게시글 등록 시간이 변경되게 작업하였습니다.

저는 한번에 작업하는게 좋을 것 같아 DTO에서 진행했습니다~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* DTO */


package
 sun.spring.dto;
 
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
 
public class ContactDTO {
    private int seq;
    private String title;
    private String content;
    private Timestamp con_date; //데이터베이스에서 가지고 온 게시글 저장시간
    private String dateWrite; //스트링형으로 저장시간을 변경 시간(게시판에 출력되는 "방금전", "5분전" 등)
    
    public ContactDTO() {
        super();
        // TODO Auto-generated constructor stub
    }
 
 
    public String getDateWrite() {
        /* 데이터베이스에서 가지고 온 게시글 저장시간 커스텀 후 출력 */
        long con_data = this.con_date.getTime(); // 데이터베이스에서 가지고온 시간
        long current_time = System.currentTimeMillis(); //현재시간
        long getTime = (current_time - con_data)/1000// (현재시간 - 데이터베이스에서 가지고온 시간)/1000
        if(getTime < 60) {
            return "방금 전";
        }else if(getTime < 300) {
            return "5분 이내";
        }else if(getTime < 3600) {
            return "1시간 이내";
        }else {
            this.dateWrite = new SimpleDateFormat("YYYY-MM-dd").format(con_data);
            //SimpleDateFormat은 .format에 들어간 데이터를 SimpleDateFormat("YYYY-MM-dd")패천으로 변경한다는 뜻
            return dateWrite;
        }
    }
 
 
    public void setDateWrite(String dateWrite) {
        this.dateWrite = dateWrite;
    }
 
 
    public ContactDTO(int seq, String title, String content, Timestamp con_date) {
        super();
        this.seq = seq;
        this.title = title;
        this.content = content;
        this.con_date = con_date;
    }
 
    public Timestamp getCon_date() {
        return con_date;
    }
 
    public void setCon_date(Timestamp con_date) {
        this.con_date = con_date;
    }
 
 
    public int getSeq() {
        return seq;
    }
 
 
    public void setSeq(int seq) {
        this.seq = seq;
    }
    
    public String getTitle() {
        return title;
    }
 
 
    public void setTitle(String title) {
        this.title = title;
    }
 
 
    public String getContent() {
        return content;
    }
 
 
    public void setContent(String content) {
        this.content = content;
    }
    
}
 
cs

 

댓글